Calculate contour center / area

I work on an image processing chain that separates one object by color and contour, and then calculates the y-position of that object.

How to calculate the center of a path or region using OpenCV?

Opencv Links:

+4
source share
2 answers

You can get the center of mass in the y direction by precomputing 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 only need the center of the bounding box, this is also easy to do with a BoundingRect . This returns CvRect, and you can just take half the height.

Let me know if this is not accurate enough, I have a code example where I can dig you.

+10
source

I don’t know exactly what OpenCV is, but I would suggest the following:

The selected cluster of pixels has a maximum width of one point - w - so let's say that there are vertical columns of pixels in the area. Now I would weight the columns according to the number of pixels contained in the column, and use these columns to determine the horizontal center point.

The same algorithm can also work for the X-center.

-2
source

Source: https://habr.com/ru/post/1285944/


All Articles