【发布时间】:2018-01-10 13:13:52
【问题描述】:
我正在检测图像中的循环
这是我的代码:
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好不好?或者我应该使用别的东西
【问题讨论】: