Showing posts with label Background subtraction. Show all posts
Showing posts with label Background subtraction. Show all posts

4/22/2019

OpenCV Simple Background Subtraction Example code


Step #1 
Simple background subtraction




code here

.



Step #2
remove noise + binary

code here

.

Step #3 & #4
Draw contour

Remove small area and draw rect



core here:

.



Thank you
☕️

4/19/2017

OpenCV Background subtraction 3.2 version example (for MOG, MOG2, GMG, KNN)

This is example for background subtraction on opencv 3.2 version

previously, I post 2.7 version example on here.
http://study.marearts.com/2014/04/opencv-study-background-subtractor-mog.html

But some usage is changed, and knn subtraction method is added.

Many people ask that example based on opencv 3.2

refer to below source code

Thank you.

< gist code >

< /gist code >


1/17/2016

OpenCV Background Subtraction sample code

Dear Peter

Here is your answer.
I hope this posting & source code help to you.
And This page also is good explain to use background subtraction.
http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html#gsc.tab=0

This is simple code for example background subtraction.

The input is cam video.
Before get video frame, source code set some options.
MOG2, ROI, and morphology

in while, blur is option for reduce noise,
And morphology is also option.
As your project purpose and camera environment, you add more pre-processing.

But I don't add labeling code, normally do labeling processing after background subtration, because blob selecting(valid size or not), blob counting, interpretation of blob.

More information
Background subtraction code.
http://study.marearts.com/search/label/Background%20subtraction
Morphology
http://study.marearts.com/search/label/Morphology
Labeling (findContours)http://study.marearts.com/search/label/Blob%20labeling

Thank you.


#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char)
{
 VideoCapture cap(0); // open the default camera
 if (!cap.isOpened()) // check if we succeeded
  return -1;

 Ptr< BackgroundSubtractorMOG2 > MOG2 = createBackgroundSubtractorMOG2(3000, 64);
 //Options
 //MOG2->setHistory(3000);
 //MOG2->setVarThreshold(128);
 //MOG2->setDetectShadows(1); //shadow detection on/off
 Mat Mog_Mask;
 Mat Mog_Mask_morpho;

 Rect roi(100, 100, 300, 300);
 
 namedWindow("Origin", 1);
 namedWindow("ROI", 1);
 Mat frame;
 Mat RoiFrame;
 Mat BackImg;

 int LearningTime=0; //300;

 Mat element;
 element = getStructuringElement(MORPH_RECT, Size(9, 9), Point(4, 4));

 for (;;)
 {
  
  cap >> frame; // get a new frame from camera
  if (frame.empty())
   break;

  //option 
  blur(frame(roi), RoiFrame, Size(3, 3));
  //RoiFrame = frame(roi);

  //Mog processing
  MOG2->apply(RoiFrame, Mog_Mask);

  if (LearningTime < 300)
  {
   LearningTime++;
   printf("background learning %d \n", LearningTime);
  }
  else
   LearningTime = 301;

  //Background Imge get
  MOG2->getBackgroundImage(BackImg);  

  //morphology 
  morphologyEx(Mog_Mask, Mog_Mask_morpho, CV_MOP_DILATE, element);
  //Binary
  threshold(Mog_Mask_morpho, Mog_Mask_morpho, 128, 255, CV_THRESH_BINARY);  

  imshow("Origin", frame);
  imshow("ROI", RoiFrame);
  imshow("MogMask", Mog_Mask);
  imshow("BackImg", BackImg);
  imshow("Morpho", Mog_Mask_morpho);

  if (waitKey(30) >= 0)
   break;
 }

 
 return 0;
}

..










6/01/2015

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;
}



...

5/04/2015

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;  
}  

4/26/2015

Background Subtraction and Blob labeling and FREAK feature extraction

This code is the source of this video.
https://www.youtube.com/watch?v=txOaulCPzSM



The source code included 2 separable routine.
One is blob labeling.
Another is FREAK feature extraction and draw.

Blob labeling is using MOG2 algorithm.
MOG2 is introduced in past on my blog.
Refer to this page.
http://study.marearts.com/search/label/MOG2
http://study.marearts.com/2014/04/opencv-study-background-subtractor-mog.html
By the way, this code included blur routine.
I thought low frequency image is more useful for background learning.
This rgb blur code on GPU mode is referenced from here.
http://study.marearts.com/2014/11/opencv-gpu-3-channel-blur-example.html

Another routine is FREAK feature extraction.
Firstly, find feature using FAST_GPU, and extract FREAK descriptor.
After extraction, 2 descriptor can compare same image or not.

This code is made for processing time check.
The process is enough to processing in real time, because image is resized and use GPU.

Hope helping to you.
Thank you.




...code start...

...code end...

4/25/2014

(OpenCV Study) Background subtraction and Draw blob to red rectangle (example source code)

I use MOG2 algorithm to background subtraction.

The process is
resize to small for more fast processing
to blur for avoid noise affection
morphology make blob and remove noise
findContour to draw blob rectangle

Example movie of result.
more detail refer to source code..
#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{

 //global variables
 Mat frame; //current frame
 Mat resize_blur_Img;
 Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
 Mat binaryImg;
 //Mat TestImg;
 Mat ContourImg; //fg mask fg mask generated by MOG2 method
 Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
 
 pMOG2 = new BackgroundSubtractorMOG2(300,32,true);//300,0.0);
 
 char fileName[100] = "mm2.avi"; //video\\mm2.avi"; //mm2.avi"; //cctv 2.mov"; //mm2.avi"; //";//_p1.avi";
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera   

 //morphology element
 Mat element = getStructuringElement(MORPH_RECT, Size(7, 7), Point(3,3) );   

 //unconditional loop   
 while (true) {   
  Mat cameraFrame;   
  if(!(stream1.read(frame))) //get one frame form video   
   break;
  
  //Resize
  resize(frame, resize_blur_Img, Size(frame.size().width/3, frame.size().height/3) );
  //Blur
  blur(resize_blur_Img, resize_blur_Img, Size(4,4) );
  //Background subtraction
  pMOG2->operator()(resize_blur_Img, fgMaskMOG2, -1);//,-0.5);
  
  ///////////////////////////////////////////////////////////////////
  //pre procesing
  //1 point delete
  //morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_ERODE, element);
  morphologyEx(fgMaskMOG2, binaryImg, CV_MOP_CLOSE, element);
  //morphologyEx(fgMaskMOG2, testImg, CV_MOP_OPEN, element);

  //Shadow delete
  //Binary
  threshold(binaryImg, binaryImg, 128, 255, CV_THRESH_BINARY);

  //Find contour
  ContourImg = binaryImg.clone();
  //less blob delete
  vector< vector< Point> > contours;
  findContours(ContourImg,
            contours, // a vector of contours
            CV_RETR_EXTERNAL, // retrieve the external contours
            CV_CHAIN_APPROX_NONE); // all pixels of each contours

  vector< Rect > output;
  vector< vector< Point> >::iterator itc= contours.begin();
  while (itc!=contours.end()) {

   //Create bounding rect of object
   //rect draw on origin image
   Rect mr= boundingRect(Mat(*itc));
   rectangle(resize_blur_Img, mr, CV_RGB(255,0,0));
   ++itc;
  }
  

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

  //Display
  imshow("Shadow_Removed", binaryImg);
  imshow("Blur_Resize", resize_blur_Img);
  imshow("MOG2", fgMaskMOG2);

  if (waitKey(5) >= 0)   
   break;   
 }

}



4/24/2014

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

Background subtractor example souce code.

OpenCV support about 3 types subtraction algorithm.

Those are MOG, MOG2, GMG algorithms.

Detailed algorithm explain is, please refer to opencv documnet.

In this post, just introduce source code and result of background subtraction.

firstly, see the results of 3 algorithms.

origin image

MOG result

MOG2 result

CMG result




Especially, in MOG2 result, we can see the gray pixel color, that means shadow.
This is result video of the source code.
source code is here->  

#include < stdio.h>
#include < iostream>

#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif 

using namespace cv;
using namespace std;



int main()
{

 //global variables
 Mat frame; //current frame
 Mat resizeF;
 Mat fgMaskMOG; //fg mask generated by MOG method
 Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
 Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method
 

 Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
 Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
 Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor
 


 pMOG = new BackgroundSubtractorMOG();
 pMOG2 = new BackgroundSubtractorMOG2();
 pGMG = new BackgroundSubtractorGMG();
 

 char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi";
 VideoCapture stream1(fileName);   //0 is the id of video device.0 if you have only one camera   

 Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );   

 //unconditional loop   
 while (true) {   
  Mat cameraFrame;   
  if(!(stream1.read(frame))) //get one frame form video   
   break;
  
  resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
  pMOG->operator()(resizeF, fgMaskMOG);
  pMOG2->operator()(resizeF, fgMaskMOG2);
  pGMG->operator()(resizeF, fgMaskGMG);
  //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); 

 


  imshow("Origin", resizeF);
  imshow("MOG", fgMaskMOG);
  imshow("MOG2", fgMaskMOG2);
  imshow("GMG", fgMaskGMG);
  

  if (waitKey(30) >= 0)   
   break;   
 }

}




..


refer to this url that is 3.2 version example.
http://study.marearts.com/2017/04/opencv-background-subtraction-32.html