【问题标题】:Finding the center of circles in an image查找图像中的圆心
【发布时间】:2020-06-02 22:23:36
【问题描述】:

我对编程很陌生,需要编写一个程序来找到位于正方形图像每个角落的 4 个圆盘(圆)的中心。

我不知道圆盘的确切坐标,但对它们有一个很好的近似值。如何找到 4 个圆盘中每个圆盘的中心位置?

【问题讨论】:

  • 查看 OpenCV!它是为这类东西而建的。
  • 欢迎来到 Stack Overflow。这不是教程、代码编写或家庭作业服务。这是一个问答网站,特定 编程问题(通常但不总是,包括一些代码)可以获得特定 答案。请拨打tour 并仔细阅读help center 以了解有关该网站的更多信息,包括what is on-topicwhat is not,以及如何ask a good question。也请关注question checklist

标签: python image detection


【解决方案1】:

窗口最有可能使用左上角作为 X= 0, Y = 0 绘制图像。圆倾向于从中心向外绘制,因此圆的中心很可能是 (X,Y) 坐标用于绘制给定的圆。您可能会找到一种方法来请求圆坐标的值,具体取决于它们的绘制方式以及绘制它们的方式。有多种使用 python 绘制屏幕的方法,所以这取决于你是如何做的。你能提供更多细节吗?

【讨论】:

  • 圆圈已经在图像的角落,我需要为每个圆圈找到中心的位置。我在想我首先必须检测圆,然后才能计算中心坐标。但是,我不确定如何首先检测圆圈位置
  • 如果你分享你的代码,它会更容易回答。编辑您的原始答案以包含它。 :)
【解决方案2】:

您可以在 OpenCV 中使用 moments 查找形状的质心:

import cv2

# read image through command line
img = cv2.imread(args["ipimage"])
# or load it from a path
#img = cv2.imread(R"/usr/home/dinges/4c.png")

# convert the image to grayscale
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# convert the grayscale image to binary image
ret,thresh = cv2.threshold(gray_image,127,255,0)

# find contours in the binary image
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
   # calculate moments for each contour
   M = cv2.moments(c)

   # calculate x,y coordinate of center
   cX = int(M["m10"] / M["m00"])
   cY = int(M["m01"] / M["m00"])
   print('centroid: X:{}, Y:{}'.format(cX, cY)) 
   cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
   cv2.putText(img, "centroid", (cX -25, cY -25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

   # display the image
   cv2.imshow("Image", img)
   cv2.waitKey(0)

参考:Find the Center of a Blob (Centroid) using OpenCV

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2016-02-17
  • 2020-06-01
  • 1970-01-01
  • 2019-01-11
  • 2021-12-31
相关资源
最近更新 更多