我正在尝试检测这张图片中水管的数量。为此,我正在尝试使用基于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)
运行后,我确实看到了许多检测到的圆,但结果完全混乱。我的问题是,如何改善这种检测。在HoughCircles方法中,哪些参数需要特别优化以实现更高的准确性?或者,我应该采取标注数百个类似图像的边界框的方法,然后通过像Yolo这样的完整CNN对它们进行训练以执行检测?
采用此处答案2中提到的方法使用OpenCV拍摄金属零件孔的直径图片,我得到了这个输出。它看起来接近于进行计数,但在图像亮度变换期间错过了许多实际的管道。
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
。你有蓝色通道,可以利用这个知识来提取灰度图像:蓝色通道比其他通道具有更大的对比度,只需使用该通道即可。 - Cris Luengo