title: How to filter out small contours in OpenCV 2.4.10
body: <p>I need to make a program that can detect different objects from a quadcopter, only problem is I'm getting tons of little random shapes appearing. I borrowed the code from here: <a href="http://opencv-srf.blogspot.com/2011/09/object-detection-tracking-using-contours.html" rel="nofollow noreferrer">http://opencv-srf.blogspot.com/2011/09/object-detection-tracking-using-contours.html</a> and tried it it on some images taken from the quad copter. Here is the result of the program: <a href="https://imgur.com/IqzNVVr" rel="nofollow noreferrer">http://imgur.com/IqzNVVr</a>. It should've only seen the silverish box near the top, but there's all the little shapes it sees. I know logically what to do; just not draw lines around the shape if its area is below a certain pixel amount... but I have no clue how to do it. I altered the coded slightly and included it below. How would I go about doing this?</p>
  <p>Code:</p>
  <pre><code>#include &quot;opencv2/core/core.hpp&quot;
  #include &quot;opencv2/flann/miniflann.hpp&quot;
  #include &quot;opencv2/imgproc/imgproc.hpp&quot;
  #include &quot;opencv2/photo/photo.hpp&quot;
  #include &quot;opencv2/video/video.hpp&quot;
  #include &quot;opencv2/features2d/features2d.hpp&quot;
  #include &quot;opencv2/objdetect/objdetect.hpp&quot;
  #include &quot;opencv2/calib3d/calib3d.hpp&quot;
  #include &quot;opencv2/ml/ml.hpp&quot;
  #include &quot;opencv2/highgui/highgui.hpp&quot;
  #include &quot;opencv2/contrib/contrib.hpp&quot;
  #include &quot;opencv2/core/core_c.h&quot;
  #include &quot;opencv2/highgui/highgui_c.h&quot;
  #include &quot;opencv2/imgproc/imgproc_c.h&quot;
  
  using namespace cv;
  using namespace std;
  
  int main()
  {
  
  IplImage* img = cvLoadImage(&quot;C:/Users/wyndr_000/Documents/Visual Studio 2013/Projects/OpenCV2410Test2/OpenCV2410Test2/Testpic2.png&quot;);
  
  //show the original image
  cvNamedWindow(&quot;Raw&quot;);
  cvShowImage(&quot;Raw&quot;, img);
  
  //converting the original image into grayscale
  IplImage* imgGrayScale = cvCreateImage(cvGetSize(img), 8, 1);
  cvCvtColor(img, imgGrayScale, CV_BGR2GRAY);
  
  //thresholding the grayscale image to get better results
  cvThreshold(imgGrayScale, imgGrayScale, 128, 255, CV_THRESH_BINARY);
  
  CvSeq* contours;  //hold the pointer to a contour in the memory block
  CvSeq* result;   //hold sequence of points of a contour
  CvMemStorage *storage = cvCreateMemStorage(0); //storage area for all contours
  
  //finding all contours in the image
  cvFindContours(imgGrayScale, storage, &amp;contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  
  //iterating through each contour
  while (contours)
  {
      //obtain a sequence of points of contour, pointed by the variable 'contour'
      result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
  
  
          //if there are 3  vertices  in the contour(It should be a triangle)
          if (result-&gt;total == 3)
          {
              //iterating through each point
              CvPoint *pt[3];
              for (int i = 0; i &lt; 3; i++){
                  pt[i] = (CvPoint*)cvGetSeqElem(result, i);
              }
  
              //drawing lines around the triangle
              cvLine(img, *pt[0], *pt[1], cvScalar(255, 0, 0), 4);
              cvLine(img, *pt[1], *pt[2], cvScalar(255, 0, 0), 4);
              cvLine(img, *pt[2], *pt[0], cvScalar(255, 0, 0), 4);
  
          }
  
          //if there are 4 vertices in the contour(It should be a quadrilateral)
          else if (result-&gt;total == 4)
          {
              //iterating through each point
              CvPoint *pt[4];
              for (int i = 0; i &lt; 4; i++){
                  pt[i] = (CvPoint*)cvGetSeqElem(result, i);
              }
  
              //drawing lines around the quadrilateral
              cvLine(img, *pt[0], *pt[1], cvScalar(0, 255, 0), 4);
              cvLine(img, *pt[1], *pt[2], cvScalar(0, 255, 0), 4);
              cvLine(img, *pt[2], *pt[3], cvScalar(0, 255, 0), 4);
              cvLine(img, *pt[3], *pt[0], cvScalar(0, 255, 0), 4);
          }
  
          //if there are 7  vertices  in the contour(It should be a heptagon)
          else if (result-&gt;total == 7)
          {
              //iterating through each point
              CvPoint *pt[7];
              for (int i = 0; i &lt; 7; i++){
                  pt[i] = (CvPoint*)cvGetSeqElem(result, i);
              }
  
              //drawing lines around the heptagon
              cvLine(img, *pt[0], *pt[1], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[1], *pt[2], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[2], *pt[3], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[3], *pt[4], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[4], *pt[5], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[5], *pt[6], cvScalar(0, 0, 255), 4);
              cvLine(img, *pt[6], *pt[0], cvScalar(0, 0, 255), 4);
          }
  
      //obtain the next contour
      contours = contours-&gt;h_next;
  }
  
  
  //show the image in which identified shapes are marked   
  cvNamedWindow(&quot;Tracked&quot;);
  cvShowImage(&quot;Tracked&quot;, img);
  
  cvWaitKey(0); //wait for a key press
  
  //cleaning up
  cvDestroyAllWindows();
  cvReleaseMemStorage(&amp;storage);
  cvReleaseImage(&amp;img);
  cvReleaseImage(&amp;imgGrayScale);
  
  return 0;
  } 
  </code></pre>