# Calculating the area of Bounding Box

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. 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() );

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 close merge delete

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?

Sort by » oldest newest most voted

well if r is your Rect there,

area  : r.width * r.height
center: (r.x + r.width/2,  r.y+r.height/2)

more

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.

more

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

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.

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

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.

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?

more

1

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

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.

Official site

GitHub

Wiki

Documentation