Opencv gpu MOG2_GPU example source code (background subtraction)

refer to example source code
I also have introduced other background subtraction method in here.

#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")
#pragma comment(lib, "opencv_core249.lib")
#pragma comment(lib, "opencv_imgproc249.lib")
#pragma comment(lib, "opencv_gpu249.lib")
#pragma comment(lib, "opencv_highgui249.lib")

#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("C:\\videoSample\\tracking\\sample.avi");//0);
 Mat o_frame;
 gpu::GpuMat o_frame_gpu;
 gpu::GpuMat r_frame_gpu;
 gpu::GpuMat rg_frame_gpu;
 gpu::GpuMat r_frame_blur_gpu;

 cap >> o_frame;
 if( o_frame.empty() )
   return 0; 
 vector< gpu::GpuMat> gpurgb(3);
 vector< gpu::GpuMat> gpurgb2(3);

 unsigned long AAtime=0, BBtime=0;

 //Mat rFrame;
 Mat showMat_r_blur;
 Mat showMat_r;

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

  gpu::resize(o_frame_gpu, r_frame_gpu, Size(RWIDTH, RHEIGHT) );
  AAtime = getTickCount();

  gpu::split(r_frame_gpu, gpurgb);
  gpu::blur(gpurgb[0], gpurgb2[0], Size(3,3) );
  gpu::blur(gpurgb[1], gpurgb2[1], Size(3,3) );
  gpu::blur(gpurgb[2], gpurgb2[2], Size(3,3) );
  gpu::merge(gpurgb2, r_frame_blur_gpu);
  pMOG2_g.operator()(r_frame_blur_gpu, Mog_Mask_g,-1);

  BBtime = getTickCount(); 
  float pt = (BBtime - AAtime)/getTickFrequency(); 
  float fpt = 1/pt;
  printf("gpu %.4lf / %.4lf \n",  pt, fpt );;
  imshow("origin", showMat_r);
  //imshow("gray", showMat_rg);
  imshow("blur", showMat_r_blur);
  imshow("mog_mask", Mog_Mask);

  if( waitKey(10) > 0)

 return 0;


  1. how to compile and run this program on opeen cv
    ...plzz suggest commands


Post a Comment

Popular posts from this blog

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

OpenCV Stitching example (Stitcher class, Panorama)

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.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

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

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

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

Image warping (using opencv findHomography, warpPerspective)