Tomazi
Tomazi

Reputation: 791

OpenCV Drawing Bounding Box CenterPoint

I am trying to draw a Dot in Bounding Box that will represent the Center Point of that box. I have computed the center Point but it is only outputted in CMD and I wont this Point to be visible on a image.

I am working with OpenCV2.4.3 on Visual Studio 2010 C++

 for(int i= 0; i < boundRect.size(); i++ )
       {
            //BoundingBox Area
            boundingBoxArea.clear();
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y));
            boundingBoxArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height));
            boundingBoxArea.push_back(Point2f(boundRect[i].x, boundRect[i].y + boundRect[i].height));

            double area0 = contourArea(boundingBoxArea);

            cout << "area of bounding box no." << i << " = " << area0 << endl;

            //Bounding Box Centroid
            area0 = (boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2;

            cout<<"Rectangle " <<i<< " Centroid possition is at: " "=" <<area0<<endl;
            cout<<""<<endl;
            cout<<""<<endl;
     }

The above is the code that i use well only a small part but a part that is responsible for calculations on Bounding Boxes

Upvotes: 2

Views: 18028

Answers (3)

berak
berak

Reputation: 39796

oh, you already calculated the area, and now you're trying to assing the center(Point) to that ? oh, no. replace your last lines with:

//Bounding Box Centroid
Point center = Point((boundRect[i].x + boundRect[i].width)/2, (boundRect[i].y + boundRect[i].height)/2);

// print it:
cout<<"Rectangle " <<i<< " Centroid position is at: " << center.x << " " << center.y << endl;

also, your boundingBoxArea is wrong. take the original boundingRect[i] instead (for calculating the area), please!

Upvotes: 3

Hey StackExchange
Hey StackExchange

Reputation: 2125

Alternative

Using Moments, your code may also look as follow (java, not tested):

..
MatOfPoint contour = new MatOfPoint();
Rect box = boundRect[i];
//Points order doesn't matter
contour.fromArray(new Point[]{new Point(box.x, box.y), //top-left
                  new Point(box.x + box.width, box.y), // top-right
                  new Point(box.x,  box.y + box.height)}); //bottom-left
                  new Point(box.x + box.width, box.y + box.height), //bottom right
int Cx = (int)Math.round(M.get_m10()/M.get_m00());
int Cy = (int)Math.round(M.get_m01()/M.get_m00());
..
double area0 = Imgproc.contourArea(contour);
..

Background

Image moments help you to calculate some features like center of mass of the object, area of the object etc. Check out the wikipedia page on Image Moments

import cv2
import numpy as np

img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print M

Centroid is given by the relations, Cx=M10/M00 and Cy=M01/M00.

Cx = int(M['m10']/M['m00'])
Cy = int(M['m01']/M['m00'])

See OpenCV tutorial here.

Upvotes: 1

Tomazi
Tomazi

Reputation: 791

OK guys I managed to solve my own problem on my own makes me feel proud hehe :D

I released that the equation it self was wrong, because i was dividing both x & width and y & with which was wrong the offset given by x & y was wrong. So i changed the code around so that i only divide width/2 and height/2

The final ingredient for the solution was to use the cv::circle(); function which i used to draw the center point.

HOPE THIS MIGHT HELP SOME PEOPLE SOME DAY :D

thx to @berak

Final Result:

enter image description here

Upvotes: 0

Related Questions