使用OpenCV对实时源进行Hough变换

时间:2015-10-08 20:51:43

标签: python-2.7 opencv

我一直试图找出如何在网络摄像头上应用Hough变换。但是我得到的错误是它没有合适的格式,即使我将其缩放到一个已检查的状态,看它是否在uint8中。

cap = cv2.VideoCapture(1)

while(True):
    ret, frame = cap.read()
    frame = cv2.medianBlur(frame,5)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    ###
    #HughCircles Detection TEST  
    circles = cv2.HoughCircles(frame,cv2.HOUGH_GRADIENT,1,50,
                               param1=50,param2=30,minRadius=0,maxRadius=0) 
    circles = np.uint16(np.around(circles))
    if circles is None:
        for i in circles[0,:]:
            # draw the outer circle
            cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)
    #    
    ###

    # Display the resulting frame
    cv2.imshow('Board',frame)

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

1 个答案:

答案 0 :(得分:1)

好的,看到另一篇文章,我看到圈子实际上在没有任何可见的情况下返回None,所以必须有一个if,以致它没有引用None。

cap = cv2.VideoCapture(1)

while(True):
    ret, frame = cap.read()
    frame = cv2.medianBlur(frame,5)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

###
#HughCircles Detection TEST  
    circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,50,
                          param1=50,param2=30,minRadius=0,maxRadius=0) 
    circles = np.uint16(np.around(circles))
    if circles != None:
        for i in circles[0,:]:
        # draw the outer circle
            cv2.circle(gray,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
            cv2.circle(gray,(i[0],i[1]),2,(0,0,255),3)
#    
###

# Display the resulting frame
    cv2.imshow('Board',gray)

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