计算轮廓/区域的中心
我正在研究一个图像处理链,该链通过颜色和轮廓分隔单个对象,然后计算该对象的 y 位置。
如何使用 OpenCV 计算轮廓或区域的中心?
Opencv链接:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过首先计算 时刻。 然后质心由 yc = M01 / M00 给出,其中 M01 和 M00 是 Moments 调用返回的结构中的字段。
如果您只想要边界矩形的中心,也可以使用 BoundingRect。 这将返回一个 CvRect,并且您可以只取一半的高度。
如果这还不够精确,请告诉我,我可以在某个地方为您挖掘示例代码。
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.
我不太清楚 OpenCV 是什么,但我建议这样做:
选定的像素簇在某一点具有最大宽度 - w - 所以可以说该区域有 w 个垂直像素列。 现在,我将根据列包含的像素数量对列进行加权,并使用这些列权重来确定水平中心点。
相同的算法也适用于 X 中心。
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.