python求形心_轮廓的质心是否始终是其几何中心? (OpenCV,Python)

I am working with OpenCV+Python and I want to find the geometrical centre of the following contour:

The OpenCV documentation suggests the following to find the centroid of a contour:

import numpy as np

import cv2 as cv

img = cv.imread('star.jpg',0)

ret,thresh = cv.threshold(img,127,255,0)

im2,contours,hierarchy = cv.findContours(thresh, 1, 2)

cnt = contours[0]

M = cv.moments(cnt)

print( M )

#Centroid

cx = int(M['m10']/M['m00'])

cy = int(M['m01']/M['m00'])

If I am right, according to this formula the centroid is calculated as the mean (or the average?) of all the points of the contour.

However, if for example fewer points are detected at the upper part of the contour than at the lower part of the contour then the centroid will be a bit higher than the actual geometrical centre of the (fully detected) contour.

Am I right?

If so, then is it better to calculate the average of the extreme points of the contour to find the geometrical centre of the contour and in this way to not depend at all on if the points of the contour are uniformly detected?

解决方案Am I right?

No. The OpenCV function moments() uses Green's theorem as mentioned in the OpenCV moments() docs. Green's theorem is indeed a correct way to find the center of mass of a shape. Green's theorem specifically relates integrals about some shape to integrals about the shape's border. As such, it doesn't at all matter how many points define the border or not.

I say center of mass specifically, because you can pass in a single-channel image array into moments() as well to compute the moments and not just a point array. However for an image array, if the array is just binary, then the center of mass is the centroid. And with an array of points (from your contours), there is no array to tell the pixel values inside, so the result is similarly still the centroid.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值