opencv 3.0 rc1, background subtraction mog2 example code

This is mog2 example code in opencv 3.0 rc1 version.

It will be help when you convert opencv 2.x code to 3.x opencv version.
Thank you.

This is mog2 example of 2.4.9 version.
http://study.marearts.com/2015/05/basic-mog2-example-souce-using-opencv.html

...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\videoio.hpp>  
#include < opencv2\highgui.hpp>  
//#include < opencv2\core\cuda.hpp>
#include < opencv2\core.hpp>
//#include < opencv2\bgsegm.hpp>
#include < opencv2\cudabgsegm.hpp>

#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")  
#pragma comment(lib, "opencv_cudabgsegm300d.lib")  

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")  
#pragma comment(lib, "opencv_cudabgsegm300.lib")  
#endif      

using namespace std;
using namespace cv;
int main()
{

 //
 
 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");
 //VideoCapture cap("M:\\____videoSample____\\posco\\cartype1.avi");
 
 //
 Ptr< cuda::BackgroundSubtractorMOG2 > MOG2_g = cuda::createBackgroundSubtractorMOG2(3000, 64); 
 Mat Mog_Mask;
 cuda::GpuMat Mog_Mask_g;

 //
 Mat o_frame;
 cuda::GpuMat o_frame_gpu;
 

 cap >> o_frame;
 if (o_frame.empty())
  return 0;

 /////////////////////////////////////////////////////////////////////////    


 unsigned long AAtime = 0, BBtime = 0;

 //Mat rFrame;    
 Mat showMat_r_blur;
 Mat showMat_r;

 namedWindow("origin");
 namedWindow("mog_mask");

 while (1)
 {
  /////////////////////////////////////////////////////////////////////////    
  cap >> o_frame;
  if (o_frame.empty())
   return 0;


  o_frame_gpu.upload(o_frame);
  AAtime = getTickCount();

  //
  MOG2_g->apply(o_frame_gpu, Mog_Mask_g, -1);
  //pMOG2_g.operator()(o_frame_gpu, Mog_Mask_g, -1);
  //  
  Mog_Mask_g.download(Mog_Mask);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime) / getTickFrequency();
  float fpt = 1 / pt;
  printf("gpu %.4lf / %.4lf \n", pt, fpt);



  o_frame_gpu.download(showMat_r);
  imshow("origin", showMat_r);
  imshow("mog_mask", Mog_Mask);


  /////////////////////////////////////////////////////////////////////////    

  if (waitKey(10) > 0)
   break;
 }

 MOG2_g.release();

 return 0;
}



...

Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

AMP dose run on my pc?, AMP(Accelerated Massive Parallelism)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Optical Flow sample source code using OpenCV

Real-time N camera stitching Class.

OpenCV meanShiftFiltering example source code ( cpu: pyrMeanShiftFiltering, gpu:meanShiftFiltering, gpu:meanShiftSegmentation )

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

Image warping (using opencv findHomography, warpPerspective)