title: how to track foreground in video and draw rectangle on it
body: <p>I'm working in a motion detection script that tracking people.
  I used foreground function MOG2 and it does what I want to do. In the next step, I want to draw a rectangle around moving people but I get an error when I run it.</p>
  <p>Are there any ideas on how to fix it?</p>
  <p>The error:</p>
  <pre><code>OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/shar/opencv/modules/core/src/array.cpp, line 2494
  terminate called after throwing an instance of 'cv::Exception'
    what():  /home/shar/opencv/modules/core/src/array.cpp:2494: error: (-206) Unrecognized or unsupported array type in function cvGetMat
  
  Aborted
  </code></pre>
  <p>This is my code:</p>
  <pre class="lang-cpp prettyprint-override"><code>    #include &lt;opencv2/opencv.hpp&gt;
      #include &lt;opencv2/core/core.hpp&gt;
      #include &lt;opencv2/highgui/highgui.hpp&gt;
      #include &lt;vector&gt;
      #include &lt;iostream&gt;
      #include &lt;sstream&gt;
      #include &lt;opencv2/video/background_segm.hpp&gt;  
      #include &lt;opencv2/video/background_segm.hpp&gt;
      using namespace std;
      using namespace cv;
      
      
      int main()
      {
         //Openthevideofile
         cv::VideoCapture capture(0);
         cv::Mat frame;
         Mat colored;  
         //foregroundbinaryimage
         cv::Mat foreground;
         int largest_area=0;
         int largest_contour_index=0;
         Rect bounding_rect;
         cv::namedWindow(&quot;ExtractedForeground&quot;);
         vector&lt;vector&lt;Point&gt; &gt; contours; // Vector for storing contour
         vector&lt;Vec4i&gt; hierarchy;
         findContours( frame, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
         //checkifvideosuccessfullyopened
         if (!capture.isOpened())
           return 0;
         //currentvideoframe
      
         //TheMixtureofGaussianobject
         //used with all default parameters
         cv::Ptr&lt;cv::BackgroundSubtractorMOG2&gt; pMOG2 = cv::createBackgroundSubtractorMOG2();
        
         bool stop(false);
              //  testing the bounding    box 
      
         //forallframesinvideo
         while(!stop){
        //readnextframeifany
          if(!capture.read(frame))
            break;
         //updatethebackground
         //andreturntheforeground
          float learningRate = 0.01; // or whatever
          cv::Mat foreground; 
          pMOG2-&gt;apply(frame, foreground, learningRate);
        //learningrate
        //Complementtheimage
          cv::threshold(foreground,foreground,128,255,cv::THRESH_BINARY_INV);
        //showforeground
          for( int i = 0; i&lt; contours.size(); i++ )
          {
              //  Find the area of contour
              double a=contourArea( contours[i],false); 
              if(a&gt;largest_area){
                  largest_area=a;cout&lt;&lt;i&lt;&lt;&quot; area  &quot;&lt;&lt;a&lt;&lt;endl;
                  // Store the index of largest contour
                  largest_contour_index=i;               
                  // Find the bounding rectangle for biggest contour
                  bounding_rect=boundingRect(contours[i]);
              }
           }
      
          Scalar color( 255,255,255);  // color of the contour in the
          //Draw the contour and rectangle
          drawContours( frame, contours,largest_contour_index, color, CV_FILLED,8,hierarchy);
          rectangle(frame, bounding_rect,  Scalar(0,255,0),2, 8,0);
      
      
          cv::imshow(&quot;ExtractedForeground&quot;,foreground);
          cv::imshow(&quot;colord&quot;,colored);
      
        //introduceadelay
        //orpresskeytostop
          if(cv::waitKey(10)&gt;=0)
          stop=true;
        }
       
      
      }
  </code></pre>