简体   繁体   中英

Python OpenCV: ValueError: too many values to unpack

I am trying to get through an OpenCV tutorial and I am using the provided source code. I run into this error:

File "C:\\xxx\\xxxxxxx\\Desktop\\basic-motion-detection\\motion_detector.py", line 61, in cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack.

Here is the code:

# on thresholded image
thresh = cv2.dilate(thresh, None, iterations=3)
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)`

The problem is that you are using cv2 version 3, and not version 2, the code is for version 2. To Solve your problem only change this line

(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)

for this:

(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)

findContours returns three things, and you are only specifying two to unpack into (cnts, _)

If you are only interested in the first:

# on thresholded image
thresh = cv2.dilate(thresh, None, iterations=3)
cnts, _, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM