Calculate the center of a contour/Area

蓝咒 提交于 2019-12-01 18:46:58

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.

I don't exactly know what OpenCV is, but I would suggest this:

The Selected cluster of pixels has a maximum width at one point - w - so lets say the area has w vertical columns of pixels. Now I would weight the columns according to how many pixels the column contains, and use these column-wights to determine the Horizontal Center Point.

The Same Algorithm could also work for the X Center.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!