Basic MOG2 example souce using opencv

Basic MOG2 source code.




...
#include < time.h>  
#include < opencv2\opencv.hpp>  
#include < opencv2\gpu\gpu.hpp>  
#include < string>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_gpu249d.lib")  
#pragma comment(lib, "opencv_highgui249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_gpu249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#endif     


#define RWIDTH 800  
#define RHEIGHT 600  

using namespace std;  
using namespace cv;  

int main()  
{  
 /////////////////////////////////////////////////////////////////////////  
 gpu::MOG2_GPU pMOG2_g(30);  
 pMOG2_g.history = 3000; //300;  
 pMOG2_g.varThreshold =64; //128; //64; //32;//;   
 pMOG2_g.bShadowDetection = true;  
 Mat Mog_Mask;  
 gpu::GpuMat Mog_Mask_g;  
 /////////////////////////////////////////////////////////////////////////  


 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");//0);  
 /////////////////////////////////////////////////////////////////////////  
 Mat o_frame;  
 gpu::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();
  
  //  
  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;  
 }  

 return 0;  
}  

Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

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

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

Real-time N camera stitching Class.

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

Optical Flow sample source code using OpenCV

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

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write