我正在检测图像中的循环
这是我的代码:
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好不好?或者我应该用别的东西
最佳答案
我发现了错误。这些是文件名中的一些空格。
如果没有找到循环,它将返回:
追溯(最近的调用最后): 文件“circle.py”,第 11 行,位于 圆圈 = np.uint16(np.around(圆圈)) 文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第 2789 行,在 return _wrapfunc(a, 'round', decimals=decimals, out=out) 文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第 67 行,在 _wrapfunc 返回 _wrapit(obj,方法,*args,**kwds) 文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第 47 行,在 _wrapit 结果 = getattr(asarray(obj), 方法)(*args, **kwds) AttributeError: 'NoneType' 对象没有属性 'rint'
并且可以使用try catch让检测继续进行
关于python - cv2.HoughCircles 找不到循环时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179663/