title: OpenCV Bounding Box
body: <p>I am working on software using OpenCV in C++ environment. <strong>The objective is to detect a boxing glove and draw a bounding box around gloves contours</strong>. </p>
  
  <p>The problem I am running into is that the bounding box is drown more than once in fact multiple boxes are drawn. What I was trying to do over the past few days is to somehow eliminate the number of boxes drawn and have only one big bounding box drawn. </p>
  
  <p>I was looking at some techniques to fill in the object int its whole which i believe would really help in this case.</p>
  
  <p>Below I have posted the code i used to achieve the result displayed in the image:</p>
  
  <pre><code>vector&lt;vector&lt;Point&gt; &gt; contours;
  vector&lt;Vec4i&gt; hierarchy;
  vector&lt;Vec3f&gt; vecCircles;               
  vector&lt;Vec3f&gt;::iterator itrCircles;
  
  while(1)
  {
      Mat frame;
      cap &gt;&gt; frame; // get a new frame from camera
      /////////////////////
      Mat imgHSV;
      cvtColor( frame, imgHSV, CV_BGR2HSV );
      ////////////////////
      Mat blur_out;
      GaussianBlur(imgHSV, blur_out, Size(1,1),2.0,2.0);
      ////////////////////
      Mat range_out;
      inRange(blur_out, Scalar(100, 100, 100), Scalar(120, 255, 255), range_out);
      ////////////////////
      findContours(range_out, contours, hierarchy, CV_RETR_TREE,  CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
  
       /// Approximate contours to polygons + get bounding rects and circles
       vector&lt;vector&lt;Point&gt; &gt; contours_poly( contours.size() );
       vector&lt;Rect&gt; boundRect( contours.size() );
       vector&lt;Point2f&gt;center( contours.size() );
       vector&lt;float&gt;radius( contours.size() );
  
       for( int i = 0; i &lt; contours.size(); i++ )
       { 
           approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
           boundRect[i] = boundingRect( Mat(contours_poly[i]) );
       }
  
       /// Draw polygonal contour + bonding rects
       Mat drawing = Mat::zeros( range_out.size(), CV_8UC3 );
       for( int i = 0; i&lt; contours.size(); i++ )
       {
           Scalar color = Scalar(255,0,255);
           drawContours( drawing, contours_poly, i, color, 1, 8, vector&lt;Vec4i&gt;(), 0, Point() );
           rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );          
       }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ZBeoa.jpg" alt="enter image description here"></p>
  
  <p>If anyone could suggest some tips or provide some source of information where i can find answers for my problem. </p>
  
  <p><strong>EDIT</strong> (quick update): </p>
  
  <p>I managed to improve the output image quiet gradually to a point am quiet happy with the result. The Key was the usage of erode &amp; dilation as well as in my <code>findContours()</code> function. I changed the <code>CV_RETR_TREE</code> to  <code>CV_RETR_EXTERNAL</code>. There were a few other minor things i tackled but the result is good:</p>
  
  <p><img src="https://i.stack.imgur.com/4A0Ye.jpg" alt="enter image description here"> </p>
  
  <p>Dont know if i should write this here or open new thread....But now I need some help with component labeling and extracting parameters such as center points and area. :)</p>