本文介绍了Python OpenCV Hough Circles返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在尝试将Hough Circles整合到我要编写的跟踪程序的主要代码中之前,我试图弄清楚Hough Circles,但是除了None
之外,我似乎一无所获.我将孟加拉语标志用作我的图像,因为它很简单并且很容易检测到.这是我的代码:
I'm trying to figure out Hough Circles before I incorporate it into my main code for a tracking program I'm trying to write, but I can't seem to get anything but None
out from the circles. I'm using the Bengali flag as my image, since it's simple and will be easy to detect. Here's my code:
import numpy as np
import cv2
img = cv2.imread('Capture.PNG')
grayput = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(grayput, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1 =50, param2 =10, minRadius=10, maxRadius=40)
print (circles)
# need circles
if circles is not None:
# convert the coord. 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
cv2.circle(img, (x, y), r, (0, 0, 0), 4)
cv2.imwrite("image.PNG",img)
推荐答案
以下代码将为您提供非无个圈子:
The following code will give you non-None circles:
import numpy as np
import cv2
img = cv2.imread("../images/opencv_logo.png", 0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cv2.imshow("grayscale", cimg)
cv2.waitKey(0)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
print (circles)
实际上,输出为:
[[[ 45.5 133.5 16.50757408]
[ 97.5 45.5 16.80773544]
[ 147.5 133.5 16.32482719]]]
注意:该代码段使用以下内容作为其输入图像:
Note: the snippet uses the following as its input image:
这篇关于Python OpenCV Hough Circles返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!