如何获得所有轮廓坐标的平均值并将其显示为 openCV 中的一个点?

发布于 2025-01-15 16:20:08 字数 640 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2025-01-22 16:20:08

如果您的意思是 sum(all_x)/len(all_x) 那么您可以添加值 x += ...y += ...count 点(或获取 count = len(n)/2),然后获取 x/county/count

  count = 0
  sum_x = 0
  sum_y = 0

  for j in n : 
     if i % 2 == 0: 
        sum_x += n[i] 
        sum_y += n[i + 1] 
        count += 1

  avg_x = sum_x / count
  avg_y = sum_y / count

If you mean sum(all_x)/len(all_x) then you could add values x += ... , y += ... and count points (or get count = len(n)/2) and later get x/count and y/count

  count = 0
  sum_x = 0
  sum_y = 0

  for j in n : 
     if i % 2 == 0: 
        sum_x += n[i] 
        sum_y += n[i + 1] 
        count += 1

  avg_x = sum_x / count
  avg_y = sum_y / count
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文