Python OpenCV Hough Circles returns None

时间:2015-06-30 14:00:05

标签: python opencv image-processing hough-transform

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)

1 个答案:

答案 0 :(得分:2)

以下代码将为您提供非无圈子:

calcAreaObj

确实,输出是:

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)


注意:该代码段使用以下内容作为输入图像:

enter image description here