【问题标题】:Python OpenCV Hough Circles returns NonePython OpenCV 霍​​夫圆返回无
【发布时间】:2015-06-30 14:00:05
【问题描述】:

在将 Hough Circles 合并到我正在尝试编写的跟踪程序的主代码中之前,我试图弄清楚 Hough Circles,但除了 None 之外,我似乎什么也得不到。我使用孟加拉语标志作为我的图像,因为它很简单并且很容易检测到。这是我的代码:

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)

【问题讨论】:

    标签: python opencv image-processing hough-transform


    【解决方案1】:

    下面的代码会给你non-None圈子:

    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]]]
    


    注意:sn-p 使用以下内容作为其输入图像:

    【讨论】:

    • 投反对票:这个答案没有解释为什么 None 可能会返回,或者为什么给定的代码不会返回 None。
    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多