Python 视频上的cv2.hough圆错误

Python 视频上的cv2.hough圆错误,python,linux,opencv,numpy,Python,Linux,Opencv,Numpy,当我运行cv2.HoughCircles()时,我得到了错误 Traceback (most recent call last): File "cv.py", line 1, in <module> import cv2,cv File "/home/jestinjoy/cv.py", line 19, in <module> circles = np.uint16(np.around(circles)) File "/usr/lib/pymo

当我运行cv2.HoughCircles()时,我得到了错误

Traceback (most recent call last):
  File "cv.py", line 1, in <module>
    import cv2,cv
  File "/home/jestinjoy/cv.py", line 19, in <module>
    circles = np.uint16(np.around(circles))
  File "/usr/lib/pymodules/python2.7/numpy/core/fromnumeric.py", line 2277, in around
    return _wrapit(a, 'round', decimals, out)
  File "/usr/lib/pymodules/python2.7/numpy/core/fromnumeric.py", line 37, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
AttributeError: rint
import cv2,cv
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")

您没有检查圆是否为“无”。如果您这样做,它会起作用:

import cv2,cv
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")
import cv2
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        cv2.waitKey(1)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        if circles is None:
                continue
        print circles
        #circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")
生成的输出:

import cv2,cv
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")
sam@tuwien:/tmp$ python cv.py 
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
[[[ 335.5         368.5          10.12422848]]]
[[[ 334.5         386.5          10.12422848]]]
[[[ 349.5         382.5          10.12422848]]]
[[[ 392.5         365.5          10.12422848]]]
[[[ 378.5         370.5          10.12422848]]]
[[[ 378.5         368.5          12.34908867]]]
[[[ 391.5         369.5          14.57738018]]]
[[[ 379.5         370.5          10.12422848]]]

山姆是正确的,但他的程序不画圆圈。这里有一个修正:

import cv2,cv
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")
import cv2
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.waitKey(1)
    rval, frame = vc.read()
    img = cv2.medianBlur(frame,5)
    imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)

    if circles is None:
        cv2.imshow("preview", frame)
        continue
    #circles = np.uint16(np.around(circles))

    for i in circles[0,:]:
       print i
       cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
       cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle

    cv2.imshow("preview", frame)

    key = cv2.waitKey(20)

    if key == 27: # exit on ESC
        break

cv2.destroyWindow("preview")

此代码需要一些更改,可能是由于相同的更新(显然是2年前提出的) 所以我用ret代替了rval,还对cv2.houghcirles的属性做了一些更改 按q退出

import cv2,cv
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
        cv2.imshow("preview", frame)
        rval, frame = vc.read()
        img = cv2.medianBlur(frame,5)
        imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
        circles = cv2.HoughCircles(imgg,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=20)
        circles = np.uint16(np.around(circles))
        for i in circles[0,:]:
               cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
               cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        key = cv2.waitKey(20)
        if key == 27: # exit on ESC
                break
cv2.destroyWindow("preview")
import cv2
import numpy as np

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
   ret, frame = vc.read()
else:
   ret = False

while True :
   ret, frame = vc.read()
   img = cv2.medianBlur(frame,15)
   imgg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
   cimg = cv2.cvtColor(imgg,cv2.COLOR_GRAY2BGR)
   circles =   cv2.HoughCircles(imgg,cv2.HOUGH_GRADIENT,1,120,param1=100,param2=30,minRadius=20,maxRadius=200)

   if circles is None:
       cv2.imshow("preview", frame)
       continue
   #circles = np.uint16(np.around(circles))

   for i in circles[0,:]:
      print i
      cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
      cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle

   cv2.imshow("preview", frame)

   if cv2.waitKey(1) & 0xFF == ord('q'):
       break

vc.release()
cv2.destroyWindow("preview")

即使我展示了一些圆圈,它也不会打印任何东西。你能发布一张不起作用的图片吗?你的一些参数可能是错误的。你应该可以看到:打印cv2.HoughCircles(img,cv2.cv.cv\u HOUGH\u GRADIENT,1,3)我认为你不应该删除”他回答。您最初的问题是由于没有检查变量而导致的错误。我发现了这个bug,现在你在声明其他东西。欢迎使用堆栈溢出!虽然此代码可能有助于解决问题,但它没有解释为什么和/或如何回答问题。提供这种额外的环境将大大提高其长期教育价值。请在回答中添加解释,包括适用的限制和假设。