gpt4 book ai didi

python - OpenCV HoughCircles未检测到图像中的所有圆圈

转载 作者:行者123 更新时间:2023-12-02 17:04:55 26 4
gpt4 key购买 nike

我有这张图片:
enter image description here

而且,我想检测其中的所有圆圈,我正在使用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)

这是运行代码后的输出图像:
enter image description here

结果很奇怪,为什么会这样呢?
如何检测其中的所有圈子?我应该更改哪些参数来实现它?

使用后:
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.5, 75)

我懂了 :
enter image description here

最佳答案

cv.imshow("output", np.hstack([image, output])),因为此代码..您正在通过np.hstack.将原始图像和输出图像堆叠在一起
只需打印cv.imshow("output",output).

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com