title: C++ - Background subtraction in OpenCV
body: <p>I am working on a project on background subtraction using OpenCV, but my output highlight result was not clear.</p>
  
  <p>This is my coding:</p>
  
  <pre><code>#include &lt; stdio.h&gt;
  #include &lt; iostream&gt;
  
  #include &lt; opencv2\opencv.hpp&gt;
  #include &lt; opencv2/core/core.hpp&gt;
  #include &lt; opencv2/highgui/highgui.hpp&gt;
  #include &lt; opencv2/video/background_segm.hpp&gt;
  
  
  #ifdef _DEBUG        
  #pragma comment(lib, "opencv_core2410d.lib")
  #pragma comment(lib, "opencv_imgproc2410d.lib")   //MAT processing
  #pragma comment(lib, "opencv_objdetect2410d.lib") //HOGDescriptor
  //#pragma comment(lib, "opencv_gpu2410d.lib")
  //#pragma comment(lib, "opencv_features2d2410d.lib")
  #pragma comment(lib, "opencv_highgui2410d.lib")
  #pragma comment(lib, "opencv_ml2410d.lib")
  //#pragma comment(lib, "opencv_stitching2410d.lib");
  //#pragma comment(lib, "opencv_nonfree2410d.lib");
  #pragma comment(lib, "opencv_video2410d.lib")
  #else
  #pragma comment(lib, "opencv_core2410.lib")
  #pragma comment(lib, "opencv_imgproc2410.lib")
  #pragma comment(lib, "opencv_objdetect2410.lib")
  //#pragma comment(lib, "opencv_gpu2410.lib")
  //#pragma comment(lib, "opencv_features2d2410.lib")
  #pragma comment(lib, "opencv_highgui2410.lib")
  #pragma comment(lib, "opencv_ml2410.lib")
  //#pragma comment(lib, "opencv_stitching2410.lib");
  //#pragma comment(lib, "opencv_nonfree2410.lib");
  #pragma comment(lib, "opencv_video2410d.lib")
  #endif 
  
  using namespace cv;
  using namespace std;
  
  std::vector&lt;std::vector&lt;cv::Point&gt; &gt; contours;
  
  int main()
  {
  
   //global variables
   Mat frame; //current frame
   Mat resizeF;
   Mat fgMaskMOG; //fg mask generated by MOG method
   Mat blob;
   Mat back;
  
   Ptr&lt; BackgroundSubtractor&gt; pMOG; //MOG Background subtractor
  
  
  
  
   pMOG = new BackgroundSubtractorMOG();
  
  
  
   char fileName[100] = "C:\\Users\\SamBCChan\\Desktop\\FYP Video\\demo4.mp4"; //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/2, frame.size().height/2) );
    pMOG-&gt;operator()(resizeF, fgMaskMOG);
  
    //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); 
    pMOG-&gt;getBackgroundImage(back); 
    cv::erode(fgMaskMOG,fgMaskMOG,cv::Mat());
    cv::dilate(fgMaskMOG,fgMaskMOG,cv::Mat());
    cv::findContours(fgMaskMOG,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
          cv::cvtColor(fgMaskMOG,blob,CV_GRAY2RGB);
          cv::drawContours(blob,contours,-1,cv::Scalar(255,255,255),CV_FILLED,8);
           int cmin= 500; //min connected contours
          int cmax= 10000; //max connected contours
          std::vector&lt;std::vector&lt;cv::Point&gt; &gt;::iterator itc=contours.begin();
          while (itc!=contours.end()) {
              if (itc-&gt;size() &gt; cmin || itc-&gt;size() &lt; cmax){
                  std::vector&lt;cv::Point&gt; pts = *itc;
                  cv::Mat pointsMatrix = cv::Mat(pts);
                  cv::Scalar color( 0, 255, 0 );
                  cv::Rect r0= cv::boundingRect(pointsMatrix);
                  cv::rectangle(resizeF,r0,color,2);
                  ++itc;
              }else{++itc;}
          }
  
  
    imshow("Origin", resizeF);
    imshow("MOG", fgMaskMOG);
    imshow("Blob",blob);
  
  
    if (waitKey(30) &gt;= 0)   
     break;   
   }
   return 0;
  }
  </code></pre>
  
  <p>My result came out as shown here. I need to make it draw a rectangle around the moving object properly, but it came out like this.</p>
  
  <p><a href="http://dc589.2shared.com/download/hnstQJ35/post_pic2.PNG?tsid=20150504-033417-c4d04967" rel="nofollow noreferrer">http://dc589.2shared.com/download/hnstQJ35/post_pic2.PNG?tsid=20150504-033417-c4d04967</a></p>
  
  <p>Could someone tell me how to fix this problem?</p>