11/05/2014

Opencv gpu MOG2_GPU example source code (background subtraction)



refer to example source code
I also have introduced other background subtraction method in here.
http://feelmare.blogspot.kr/2014/04/opencv-study-background-subtractor-mog.html

..
#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("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;

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

  
  o_frame_gpu.upload(o_frame);
  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);
  //
  Mog_Mask_g.download(Mog_Mask);

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

  
  r_frame_gpu.download(showMat_r);
  //rg_frame_gpu.download(showMat_rg);
  r_frame_blur_gpu.download(showMat_r_blur);
  imshow("origin", showMat_r);
  //imshow("gray", showMat_rg);
  imshow("blur", showMat_r_blur);
  imshow("mog_mask", Mog_Mask);
  
  
  /////////////////////////////////////////////////////////////////////////

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

 return 0;
}
..

1 comment:

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

    ReplyDelete