繁体   English   中英

Python OpenCV:从Hough Circle Detection中获取统计信息

[英]Python OpenCV: Getting Stats out of Hough Circle Detection

我和一个同学正在通过图像处理技术在硬币柜台上工作。 我们使用两种方法将硬币识别为圆形。 一方面将组件与统计信息连接起来,另一方面进行霍夫变换。 CC w / Stats的优点是直接输出所有重要参数(例如,像素面积)。 但是,通过触摸图像中的硬币,CC的w / stats会变弱(无法正确识别硬币的中心)。 霍夫变换没有这个问题,可以轻松地正确检测每个圆。 但是,我们不知道如何在这里使用检测到的对象的数据。 那么有没有办法通过其他功能提取数据,或者甚至有办法从带有统计信息和霍夫变换的CC中生成混合代码?

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. 您可以对分水岭使用图像分割 这种方法的优点是能够在图像中定位触摸硬币,因为您可以轻松地将硬币彼此分割。 此外,分水岭使您可以获取硬币的中心,在这里可以进行其他处理。

  2. 继续使用霍夫圆变换 该函数返回各种参数,例如半径,可用于查找圆的面积。 这是一个获取半径的简单示例,您可以使用经典公式查找面积。 此方法还可以使您轻松获得圆心。

# 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. 完全旋转并使用轮廓检测​​和过滤。 可能的步骤是

    • 将图像转换为灰度
    • 高斯模糊
    • 执行Canny边缘检测阈值以获得二进制图像
    • 执行形态转换,例如cv2.dilate()cv2.erode()
    • 使用cv2.contourArea()查找轮廓,过滤并查找区域
    • 要找到硬币的中心,可以使用图像矩来找到质心

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM