Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the center of a contour/Area

I'm working on a Image-processing chain that seperates a single object by color and contour and then calculates the y-position of this object.

How do I calculate the center of a contour or area with OpenCV?

Opencv links:

  • http://opencv.willowgarage.com/wiki/
  • http://en.wikipedia.org/wiki/OpenCV
like image 625
Janusz Avatar asked Mar 01 '23 15:03

Janusz


1 Answers

You can get the center of mass in the y direction by first calculating the Moments. Then the center of mass is given by yc = M01 / M00, where M01 and M00 are fields in the structure returned by the Moments call.

If you just want the center of the bounding rectangle, that is also easy to do with BoundingRect. This returns you a CvRect and you can just take half of the height.

Let me know if this isn't precise enough, I have sample code somewhere I can dig up for you.

like image 72
Kiv Avatar answered Mar 12 '23 12:03

Kiv