【问题标题】:cv2.HoughCircles when it doesn't find cyclescv2.HoughCircles 找不到循环时
【发布时间】: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好不好?或者我应该使用别的东西

【问题讨论】:

    标签: python opencv


    【解决方案1】:

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

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

    Traceback(最近一次调用最后一次): 文件“circle.py”,第 11 行,在 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, 方法, *args, **kwds) _wrapit 中的文件“/home/pi/.local/lib/python2.7/site-packages/numpy/core/fromnumeric.py”,第 47 行 结果 = getattr(asarray(obj), 方法)(*args, **kwds) AttributeError: 'NoneType' 对象没有属性 'rint'

    并且它可以使用try catch来使检测继续

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-16
      • 2013-01-14
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      相关资源
      最近更新 更多