python - 检测图像中的多个圆圈

标签 python opencv image-processing computer-vision object-detection

我正在尝试检测这张图片中水管的数量。为此,我尝试使用 OpenCV 和基于 Python 的检测。我得到的结果让我有点困惑,因为圆圈的分布太大而且不准确。

enter image description here

代码

import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(args["image"])
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#detect circles in the image
#circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, param1=40,minRadius=10,maxRadius=35)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 8.5,70,minRadius=0,maxRadius=70)

#print(len(circles[0][0]))
# 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")
    # count = count+1   

    # print(count) 

    # 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)

    # show the output image
   # cv2.imshow("output", np.hstack([output]))
    cv2.imwrite('output.jpg',np.hstack([output]),[cv2.IMWRITE_JPEG_QUALITY, 70])
    cv2.waitKey(0)

运行此命令后,我确实看到检测到很多圆圈,但是结果完全困惑。 我的问题是,如何改进这种检测。 HoughCircles 方法中具体需要优化哪些参数才能获得更高的精度?或者,我应该采取通过边界框注释数百张相似图像的方法,然后通过像 Yolo 这样的成熟 CNN 来训练它们来执行检测?

enter image description here

从此处采用答案 2 中提到的方法 Measuring the diameter pictures of holes in metal parts, photographed with telecentric, monochrome camera with opencv 。我得到了这个输出。这看起来接近执行计数,但在图像亮度转换期间错过了许多实际管道。

enter image description here

最佳答案

HoughCircles 调用最重要的参数是:

  1. param1:因为您使用的是 cv2.HOUGH_GRADIENTparam1 是边缘检测算法的较高阈值,param1/2 是下限阈值。
  2. param2:代表累加器阈值,因此值越低,返回的圆圈越多。
  3. minRadiusmaxRadius:示例中的蓝色圆圈的直径约为 20 像素,因此使用 70 像素作为 maxRadius 是算法返回这么多圆圈的原因。
  4. minDist:两个圆心之间的最小距离。

参数化定义如下:

circles = cv2.HoughCircles(gray,
                           cv2.HOUGH_GRADIENT,
                           minDist=6,
                           dp=1.1,
                           param1=150,
                           param2=15,
                           minRadius=6,
                           maxRadius=10)

返回:

enter image description here

关于python - 检测图像中的多个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59363165/

相关文章:

php - 如何在PHP中绘制交互式图像?

python正弦和余弦精度

python - Scrapy Crawler 仅提取 680 多个网址中的 19 个

c++ - Opencv C++ : Display pixel value using cursor of the image before applying colormap

c++ - Opencv 光流跟踪 : stop condition

python - 为图像中的不同生物细胞分配不同的颜色

java - 使用掩码读取 int 的未知方式

python - 这应该工作 : open() in python

Python - 如何仅将两个列表中的某些数字相乘

java - Opencv中的嘴巴检测在android中检测多个区域