Ask Your Question
0

Calculating the area of Bounding Box

asked 2013-02-08 13:46:03 -0500

Tomazi gravatar image

updated 2013-02-08 14:04:32 -0500

I have created an application that Segments an image on the basis of a predefined color using inRange function. I then draw bounding box around the detected object.

My question here is how do I determine region properties such as: area, size, height and with, center point.

image description

How should i approach to retrieve region properties of these bounding boxes or any other bounding boxes that get drown.......?

Here is my code responsible for drawing the Boxes:

vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(mBlur, contours, hierarchy, CV_RETR_EXTERNAL,  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 );

Regards

edit retag flag offensive close merge delete

Comments

hi i hv a same problem as you...! is it possible to get area using boundRect[i].tl(), boundRect[i].br() these points? or plz suggest some another method you come across?

sujay gravatar imagesujay ( 2014-03-10 00:41:00 -0500 )edit

3 answers

Sort by ยป oldest newest most voted
1

answered 2013-02-09 08:58:34 -0500

Siegfried gravatar image

Hi Tomazi,

if you want the properties of the rectangle, berak gave you the right answer.

But if you want the properties of the segmented regions (area, center of mass, ...) you can compute the Moments of the detected contours. Examples can be found here and here here.

edit flag offensive delete link more

Comments

Hey ye well @break is 100% right but the question remains how do i find the Box in the image and then compute the properties of the rectangle

regards

Tomazi gravatar imageTomazi ( 2013-02-09 11:38:12 -0500 )edit

Hi Tomazi, i am not sure if i got you right. In your code your already have the bounding box of every detected contour. For further analysis you only have to evaluate the rectangles stored in vector<Rect> boundRect.

Siegfried gravatar imageSiegfried ( 2013-02-12 08:32:20 -0500 )edit

Yes but i dont know how to do it....I looked at the contourArea() function but it doesn't work well at all also looked at moments() function but cant get this one to work

Tomazi gravatar imageTomazi ( 2013-02-12 15:38:01 -0500 )edit

Hi, please take a look at the OpenCV tutorial "Image Moments" (http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/moments/moments.html). The program extracts the edges in the loaded image and evaluate (area, mass center and arc length) the contours of the detected edges.

Siegfried gravatar imageSiegfried ( 2013-02-12 19:07:45 -0500 )edit
1

answered 2013-02-08 13:55:59 -0500

berak gravatar image

well if r is your Rect there,

area  : r.width * r.height  
center: (r.x + r.width/2,  r.y+r.height/2)
edit flag offensive delete link more
0

answered 2013-07-01 08:50:52 -0500

lenteken gravatar image

Hello Tomazi

I am currently doing same as your problem. I am stuck in this problem almost 3 days and didn't know how to solve it. Can you give me some solutions or samples?

Thanks in advance!

edit flag offensive delete link more

Comments

1

... And what is your problem? You can't compute the center or size of a bounding box?

Notas gravatar imageNotas ( 2013-07-01 09:28:13 -0500 )edit

No, I can't compute the height and width. Please help me.

lenteken gravatar imagelenteken ( 2013-07-01 09:51:23 -0500 )edit
1

If you have the position of your bbox in the image, the width is just the difference between the highest and lowest x-coordinate. And the height the y-coordinate difference.

Notas gravatar imageNotas ( 2013-07-01 10:41:19 -0500 )edit

Do you have any sample codes?

lenteken gravatar imagelenteken ( 2013-07-01 10:52:06 -0500 )edit

Do you have any tutorials for this application?

lenteken gravatar imagelenteken ( 2013-07-03 07:47:09 -0500 )edit

Let's continue this in your topic.

Notas gravatar imageNotas ( 2013-07-03 08:23:34 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-02-08 13:46:03 -0500

Seen: 17,478 times

Last updated: Jul 01 '13