Tomazi
Tomazi

Reputation: 781

OpenCV Bounding Box

I am working on software using OpenCV in C++ environment. The objective is to detect a boxing glove and draw a bounding box around gloves contours.

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.

I was looking at some techniques to fill in the object int its whole which i believe would really help in this case.

Below I have posted the code i used to achieve the result displayed in the image:

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<Vec3f> vecCircles;               
vector<Vec3f>::iterator itrCircles;

while(1)
{
    Mat frame;
    cap >> 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<vector<Point> > contours_poly( contours.size() );
     vector<Rect> boundRect( contours.size() );
     vector<Point2f>center( contours.size() );
     vector<float>radius( contours.size() );

     for( int i = 0; i < 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< contours.size(); i++ )
     {
         Scalar color = Scalar(255,0,255);
         drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
         rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );          
     }

enter image description here

If anyone could suggest some tips or provide some source of information where i can find answers for my problem.

EDIT (quick update):

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 & dilation as well as in my findContours() function. I changed the CV_RETR_TREE to CV_RETR_EXTERNAL. There were a few other minor things i tackled but the result is good:

enter image description here

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. :)

Upvotes: 3

Views: 25858

Answers (4)

karlphillip
karlphillip

Reputation: 93410

Take a look at my other answer in this subject. Compile that code and remember to activate the code that is commented out.

Result:

enter image description here

Upvotes: 6

Abhishek Thakur
Abhishek Thakur

Reputation: 17015

as b_m mentioned, you need to apply the morphological operations. Then I'd do something like finding the largest contour in the image and draw the bounding box around that contour only. I had created the following function for a project of mine which I think will help you if used in a correct manner

CvSeq* findLargestContour(CvSeq* contours){

  CvSeq* current_contour = contours;
  double largestArea = 0;
  CvSeq* largest_contour = NULL;

  // check we at least have some contours

  if (contours == NULL){return NULL;}

  while (current_contour != NULL){

      double area = fabs(cvContourArea(current_contour));

      if(area > largestArea){
          largestArea = area;
          largest_contour = current_contour;
      }

      current_contour = current_contour->h_next;
  }

  // return pointer to largest

  return largest_contour;

}

Upvotes: 0

b_m
b_m

Reputation: 1533

Before finding the contours you should apply morphological filter like erode and dilate. After that, you can find the contours and leave out the small ones by calculating its size, or the with and height of the bounding box. Finally you can eliminate those that are inside an other contour using the hierarchy.

Upvotes: 1

HugoRune
HugoRune

Reputation: 13799

You currently draw a bounding box around each contour, and findContour will find a contour around each connected white or black component, of which there are many in your picture.

So the first thing I would do is filter all that noise with some morphological operations on the thresholded image: do some opening and closing, both of which are combinations of dilation and erosion.

In your case something like cvDilate (2 times); cvErode(4 times); cvDilate(2 times)

This should merge all white blobs into one smooth blob, but the black hole in the middle will remain. You could find the right one by size, but it is easier to call findContours with the CV_RETR_EXTERNAL instead of CV_RETR_TREE, then it will only return the outermost contours.

Upvotes: 5

Related Questions