【发布时间】:2020-09-05 18:57:09
【问题描述】:
而且,我想检测其中的所有圆圈,我正在使用this 教程。
代码如下:
import argparse
import cv2 as cv
import numpy as np
# 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 = cv.imread(args["image"])
output = image.copy()
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.2, 75)
# 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
cv.circle(output, (x, y), r, (0, 255, 0), 4)
cv.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
cv.imshow("output", np.hstack([image, output]))
cv.waitKey(0)
结果很奇怪,为什么会这样? 如何检测其中的所有圆圈?我应该更改哪些参数来实现它?
使用后:
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.5, 75)
【问题讨论】:
-
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.5, 75)似乎找到了所有。 -
我试过了,好像是加了黄色圆圈,但是为什么它在图像的右侧重复图像?你得到的结果和我一样吗?我的意思是它在右侧重复?
-
那是因为
np.hstack([image, output])在调用cv.imshow("output", ...时。替换为cv.imshow("output", output)仅显示输出帧。 -
你能告诉我应该如何使用这些参数吗?您怎么能猜到将其更改为 1.5 会使其正常工作? @Jeppe
-
我不知道它是如何工作的,这就是为什么我将它添加为评论而不是答案。但是尝试阅读this - 本质上,通过增加
dpparameter,我认为我们会降低投票箱的分辨率,从而允许更低的精度或更多的噪音。我认为这里需要这样做,因为黄色、绿色和蓝色圆圈之间的边缘是重叠的。
标签: python opencv hough-transform