Ask Your Question
0

OpenCV Bounding Box

asked 2013-02-06 09:51:47 -0600

Tomazi gravatar image

Hello everybody 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 drown. What I was trying to do over the past few days is to somehow eliminate the number of boxes drown and have only one big bounding box drown.

I was looking at some techniques to fill in the object whole which i belief 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 );

     }

image description

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

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2013-02-06 11:22:37 -0600

karlphillip gravatar image
edit flag offensive delete link more
0

answered 2013-02-06 10:39:51 -0600

berak gravatar image
  Mat contour( contours[ idBiggest ] );

  /// Draw contour + bounding rect
  Mat drawing = Mat::zeros( range_out.size(), CV_8UC3 );

  Scalar color = Scalar(255,0,255);
  int nc = contour.size();
  const Point * pc = &(contour[0]);
  polylines( drawing , &pc, &nc, 1, 0, cv::Scalar(255) );

  Rect bounds =  boundingRect( contour );
  rectangle( drawing, bounds.tl(), bounds.br(), color, 2, 8, 0 );

edit flag offensive delete link more
0

answered 2013-02-06 10:08:41 -0600

berak gravatar image

updated 2013-02-06 10:10:47 -0600

so, you get several contours there, i'd just take the largest one ( the one with the most points ), and skip all the others.

int numBiggest = 0;
int idBiggest = 0;
for( int i = 0; i < contours.size(); i++ ) { 
    if ( contours[i].size() >  numBiggest ) {
        numBiggest = contours[i].size();
        idBiggest = i;
    }
}

// contours[ idBiggest ] should be the one you want
edit flag offensive delete link more

Comments

Firstly thx for your reply, Now i like the idea of picking out the largest contour in the frame but am not to sure how to implement your suggested solution... regards

Tomazi gravatar imageTomazi ( 2013-02-06 10:25:51 -0600 )edit

Question Tools

Stats

Asked: 2013-02-06 09:51:47 -0600

Seen: 4,868 times

Last updated: Feb 06 '13