title: How to filter contours by bounding rect size using OpenCV and C++?
body: <p>I try to detect license plate from image using OpenCV and C++. I can find the contours of license plate.  But I want to drop only license plate. I have a idea to filter contours by bounding rect size. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;opencv\cv.h&gt;
  #include &lt;opencv\highgui.h&gt;
  #include &lt;opencv\ml.h&gt;
  #include &lt;opencv\cxcore.h&gt;
  
  #include&lt;opencv2/core/core.hpp&gt;
  #include&lt;opencv2/highgui/highgui.hpp&gt;
  #include&lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include&lt;iostream&gt;
  using namespace cv;
  using namespace std;
  cv::Mat _img;       
  cv::Mat _imgGray;       
  int main()
  {
      _img = cv::imread("bs9.jpg");           
      if (_img.empty()) {                                 
      std::cout &lt;&lt; "error: image not read from file\n\n";     
      return(0);                                              
  }
  cv::Mat src;
  medianBlur(_img, src, 9);
  
  // chuyển ảnh gốc sang ảnh xám
  cv::cvtColor(src, _imgGray, CV_BGR2GRAY);
  cv::Mat _imgGray2;
  medianBlur(_imgGray, _imgGray2, 7);
  blur(_imgGray2, _imgGray2, Size(3, 3));
  //Canny
  cv::Mat edges;
  //dalation
  
  //cv::Canny(_imgGray, edges, 100, 250);
  cv::Canny(_imgGray2, edges, 100, 200, 3);
  //contour
  vector&lt;vector&lt;Point&gt;&gt; contours;
  vector&lt;Vec4i&gt; hierarchy;
  //
  //vector&lt;Rect&gt; boundRect(contours.size());
  //CvMemStorage* stor = cvCreateMemStorage(1000);
  findContours(edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
  // vẽ đường bao các cạnh
  
  Mat drawing = Mat::zeros(edges.size(), CV_8UC3);
  for (int i = 0; i &lt; contours.size(); i++)
  {
      Scalar color = Scalar(0,255,0);
      drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());     
  }
  //filter contour
  ????
  /// show image
  cv::imshow("Goc", _img);        // show ảnh gốc
  //
  //cv::namedWindow("Anh xam", CV_WINDOW_AUTOSIZE);
  cv::imshow("Xam", _imgGray);        // show ảnh xám
  cv::imshow("edges", edges);     // show ảnh Canny
  cv::imshow("contours", drawing);
  cv::waitKey(0);                 
  return(0);
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Gs5sd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gs5sd.jpg" alt="original"></a></p>
  
  <p><a href="https://i.stack.imgur.com/q2CXh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q2CXh.jpg" alt="gray"></a></p>
  
  <p><a href="https://i.stack.imgur.com/6tv6p.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6tv6p.jpg" alt="cany_egdes"></a></p>
  
  <p><a href="https://i.stack.imgur.com/LkiuD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LkiuD.jpg" alt="contours"></a></p>