检测图像中的多个圆 [英] Detect multiple circles in an image

查看:29
本文介绍了检测图像中的多个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

代码

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)

运行此程序后,我确实看到检测到许多圆圈,但是,结果完全是乱七八糟的。 我的问题是,我如何提高这种检测能力。为了获得更高的精度,HoughCircle方法中需要特别优化哪些参数?或者,我是否应该采取这样的方法:通过边界框标注数百张相似的图像,然后在像Yolo这样成熟的CNN上对它们进行训练以执行检测?

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

推荐答案

您可以将自适应阈值作为预处理。这基本上是寻找比相邻像素相对更亮的区域,您的全局阈值会丢失一些管道,这将使它们更好一些。

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

img = cv2.imread('a2MTm.jpg')
blur_hor = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((11,1,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
blur_vert = cv2.filter2D(img[:, :, 0], cv2.CV_32F, kernel=np.ones((1,11,1), np.float32)/11.0, borderType=cv2.BORDER_CONSTANT)
mask = ((img[:,:,0]>blur_hor*1.2) | (img[:,:,0]>blur_vert*1.2)).astype(np.uint8)*255

plt.imshow(mask)

然后您可以继续执行相同的后处理步骤。


以下是一些示例处理步骤:

circles = cv2.HoughCircles(mask,
                           cv2.HOUGH_GRADIENT,
                           minDist=8,
                           dp=1,
                           param1=150,
                           param2=12,
                           minRadius=4,
                           maxRadius=10)
output = img.copy()
for (x, y, r) in circles[0, :, :]:
  cv2.circle(output, (x, y), r, (0, 255, 0), 4)

您可以根据需要调整参数,阅读参数here

这篇关于检测图像中的多个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆