cv2.HoughCircles什么时候找不到循环

时间:2018-01-10 03:00:20

标签: python opencv

我正在检测图像中的周期

the a1.png

这是我的代码:

import cv2
import cv2.cv as cv
import numpy as np

img = cv2.imread('a1.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,80,
                        param1=50,param2=20,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

当它在图像中找不到任何圆圈时会发生什么?我认为它将返回null或none返回圆圈。但我发现错误

OpenCV错误:cvGetMat中的错误标志(参数或结构字段)(无法识别或不支持的数组类型),文件/build/opencv-U1UwfN/opencv-2.4.9.1+dfsg1/modules/core/src/array.cpp,第2482行

我想让检测始终保持运行状态。所以我使用try catch:

try:
   circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,80,
                            param1=50,param2=20,minRadius=0,maxRadius=0)

except :
    print("no cars!")
    exit()
else:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

但它仍然无效。 我已经测试过是否有可以检测到的循环。它有效。

那么,如何调整参数来寻找周期?我为图像尝试了很多参数。我想继续检测程序运行的时间。里面用try catch好吗?或者我应该使用别的东西

this is the image can work

1 个答案:

答案 0 :(得分:0)

我发现了这个错误。这些是文件名中的一些空格。

如果找不到循环,它将返回:

追踪(最近一次通话):   文件“circle.py”,第11行,in     circles = np.uint16(np.around(circles))   文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第2789行,在附近     return _wrapfunc(a,'round',decimals = decimals,out = out)   在_wrapfunc中输入文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第67行     return _wrapit(obj,method,* args,** kwds)   在_wrapit中输入文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第47行     result = getattr(asarray(obj),method)(* args,** kwds) AttributeError:'NoneType'对象没有属性'rint'

它可以使用try catch使检测继续

相关问题