Ask Your Question

Confused even after studying contours. Need more than 2 values to unpack

asked 2017-04-06 12:29:20 -0500

chrisbr gravatar image

line 30, in <module> (_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) ValueError: need more than 2 values to unpack, I've studied the functions can't seem to find where my problem is can someone help with my code so I can try to learn by example and see where the problem is?

import cv2
import numpy as np cap=cv2.VideoCapture(0)

while(1): _, img =

#converting frame(img i.e BGR) to HSV (hue-saturation-value)


#definig the range of red color

#finding the range of red,blue and yellow color in the image
red=cv2.inRange(hsv, red_lower, red_upper)

#Morphological transformation, Dilation     
kernal = np.ones((5 ,5), "uint8")

    red=cv2.dilate(red, kernal)
res=cv2.bitwise_and(img, img, mask = red)

#Tracking the Red Color
for pic, contour in enumerate(contours):
    area = cv2.contourArea(contour)
        x,y,w,h = cv2.boundingRect(contour) 
        img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        cv2.putText(img,"RED color",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))

    cv2.imshow("Color Tracking",img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-06 16:49:23 -0500

berak gravatar image

the opencv2.4 version of findContours() only returns 2 args, not 3, like in thr opencv3 version.

 (contours, hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

should do the trick for you.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-04-06 12:29:20 -0500

Seen: 2,650 times

Last updated: Apr 06 '17