如何获得所有轮廓坐标的平均值并将其显示为 openCV 中的一个点?
cv2.erode(binary_img, M, iterations=100)
edges = cv2.Canny(binary_img, 128, 256)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours :
approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
# draws boundary of contours.
cv2.drawContours(output_img, [approx], 0, (0, 0, 255), 5)
# Used to flatted the array containing
# the co-ordinates of the vertices.
n = approx.ravel()
i = 0
for j in n :
if(i % 2 == 0):
x = n[i]
y = n[i + 1]
i = i + 1
我可以获得每个轮廓的 x 和 y 坐标,但我不确定如何找到所有轮廓的平均值并将其转换为一个点。
cv2.erode(binary_img, M, iterations=100)
edges = cv2.Canny(binary_img, 128, 256)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours :
approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
# draws boundary of contours.
cv2.drawContours(output_img, [approx], 0, (0, 0, 255), 5)
# Used to flatted the array containing
# the co-ordinates of the vertices.
n = approx.ravel()
i = 0
for j in n :
if(i % 2 == 0):
x = n[i]
y = n[i + 1]
i = i + 1
I can get the x and y coordinate for each contour but I'm unsure of how to find the average of all of them and turn it into a point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是
sum(all_x)/len(all_x)
那么您可以添加值x += ...
、y += ...
和count
点(或获取count = len(n)/2
),然后获取x/count
和y/count
If you mean
sum(all_x)/len(all_x)
then you could add valuesx += ...
,y += ...
andcount
points (or getcount = len(n)/2
) and later getx/count
andy/count