Python OpenCV:从Hough圆检测中获取统计信息

Python OpenCV:从Hough圆检测中获取统计信息,python,opencv,geometry,detection,hough-transform,Python,Opencv,Geometry,Detection,Hough Transform,我和一位同学正在通过图像处理来制作硬币计数器。我们使用了两种方法来识别圆形的硬币。一方面用统计数据连接组件,另一方面用霍夫变换连接组件。CC w/Stats的优点是直接输出所有重要参数(例如像素面积)。但是,CC在触摸图像中的硬币时,w/stats减弱(硬币的中心无法正确识别)。Hough变换没有这个问题,可以轻松地正确检测每个圆。然而,我们不知道如何在这里使用检测到的对象的数据。那么,有没有一种方法可以用另一个函数来获取数据,或者甚至有没有一种方法可以从CC w/Stats和Hough变换生成

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

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)


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

  • 你可以用。这种方法的优点是能够在图像中定位触摸硬币,因为您可以轻松地将硬币彼此分割。此外,分水岭允许您获得硬币的中心,在那里您可以进行其他处理

  • 继续使用。该函数返回各种参数,如可用于查找圆面积的半径。下面是一个获取半径的快速示例,您可以使用该半径来使用查找区域。此方法还允许您轻松获得圆心

  • 完全旋转并使用带过滤的轮廓检测。可能采取的步骤是:

    • 将图像转换为灰度
    • 高斯模糊
    • 执行或获取二进制图像
    • 执行诸如
      cv2.deflate()
      cv2.decore()
    • 使用
      cv2.contourArea()
    • 要找到硬币的中心,可以使用查找质心
  • # 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
            ...