Python OpenCV : Getting Stats out of Hough Circle Detection

标签 python opencv geometry detection hough-transform

我和一个同学正在通过图像处理来制作硬币点算机。我们使用两种方法将硬币识别为圆圈。一方面使用 Stats 连接组件,另一方面 Hough 变换。 CC w/Stats 的优点是所有重要参数(例如像素区域)的直接输出。然而,CC 的 w/stats 会随着图像中触摸硬币而减弱(硬币中心无法正确识别)。霍夫变换没有这个问题,可以轻松正确地检测到每个圆。但是,我们不知道如何在这里使用检测到的对象的数据。那么有没有一种方法可以用另一个函数获取数据,或者有没有一种方法可以从 CC w/Stats 和 Hough Transformation 生成混合代码?

import cv2
import numpy as np
import matplotlib.pyplot as plt

image='17.png'
img=cv2.imread(image,1)
img_orig=img.copy()
img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

img=cv2.GaussianBlur(img,(21,21),cv2.BORDER_DEFAULT)


all_circs=cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,1,500,param1=110,param2=35,minRadius=200,maxRadius=600)
all_circs_rounded=np.uint32(np.around(all_circs))


count = 1
for i in all_circs_rounded[0, :]:
    cv2.circle(img_orig,(i[0],i[1],),i[2],(255,0,0),3)
    cv2.circle(img_orig,(i[0],i[1],),2,(255,0,0),3)
    cv2.putText(img_orig,"Coin"+str(count),(i[0]-70,i[1]+30),cv2.FONT_HERSHEY_SIMPLEX,1.1,(255,0,0),2)
    count+=1

print (all_circs_rounded)
print (all_circs_rounded.shape)
print ('I have found ' + str(all_circs_rounded.shape[1]) + ' coins')

plt.rcParams["figure.figsize"]=(16,9)
plt.imshow(img_orig)

最佳答案

这个问题有几种可能的解决方案

  1. 你可以使用 image segmentation with watershed .这种方法的优点是能够在图像中找到接触的硬币,因为您可以轻松地将硬币彼此分开。此外,分水岭可以让你获得硬币的中心,在那里你可以做额外的处理。

  2. 继续使用Hough Circle Transform .该函数返回各种参数,例如可用于计算圆面积的半径。这是一个获取半径的简单示例,您可以使用 classic formula 查找区域。 .这种方法还可以让您轻松获得圆心。

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")

    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

        # calculate area here
        ...
  1. 完全旋转并使用带过滤的轮廓检测。可能的步骤是

关于Python OpenCV : Getting Stats out of Hough Circle Detection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56797429/

相关文章:

python - 有序 = True 不受尊重。 python +棉花糖+ flask

python - 使用 Pillow 和 Python 3 从 RGB 列表创建图像

c++ - 使用 fprintf 连续写入字符串

c++ - 使用 opencv 和 cURL 从 url 加载图像

geometry - (可能是无界的)凸多边形与半平面的交集

javascript - 如何在 JavaScript 中将鼠标点对齐到一个 Angular

python - 使用 xpath 按值查找没有 value 属性的输入字段

python - 我是否需要安装 Hadoop 才能使用 Pyspark 的所有功能?

ios - opencv2.framework 无法使用链接器标志 -ObjC 进行编译

algorithm - 最小面积外接矩形包含凸包的最小距离轨迹