0
votes

Hi I'm new with python and OpenCV. With the code below I'm trying to stream from a web camera and detect circles. I'm trying to extract the center of the circle and the radius in the for loop and then draw the circle. Every time I do that I get the following error.

ValueError: too many values to unpack (expected 3)

Any help about why the code dosnt work would be greatly appreciated. Thanks

import cv2
import numpy as np
import sys
cap = cv2.VideoCapture(1)
while(True):
    gray = cv2.medianBlur(cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY),5)
    circ = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,
                              minRadius=0,maxRadius=0)
    for(x,y,r) in circ:
        cv2.circle(gray,int(x),int(y),int(r),(0,255,0),2)
    cv2.imshow('video',gray)
    if cv2.waitKey(1)==27:# esc Key
        break
cap.release()
cv2.destroyAllWindows()

Edit: The Traceback

Traceback (most recent call last): File "C:/Documents/pythonproj/webcameratest.py", line 11, in for(x,y,r) in circ: TypeError: 'NoneType' object is not iterable

1
I don't think that cv2.circ is a valid OpenCV function in Python API. Also it would be helpful if you paste the full error traceback, which includes the line number where the error is happening.ZdaR
@ZdaR I meant to use cv2.circle in the code to dray a circle around where it detected one. but it is still making the same error. Also a added the full trackback.Joe
So if you see the error closely it states that: for(x,y,r) in circ: TypeError: 'NoneType' object is not iterable, which clearly means that cv2.HoughCircles() is simply returning None.ZdaR

1 Answers

0
votes

Try this:

circ = np.uint16(np.around(circ))
for i in circ[0,:]:
    cv2.circle(gray, (i[0], i[1]), i[2], (0, 255, 0), 2)

instead of:

for(x,y,r) in circ:
    cv2.circle(gray,int(x),int(y),int(r),(0,255,0),2)