1

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)`
Tyler Canine
  • 31
  • 1
  • 4
  • Possible duplicate of [OpenCV python: ValueError: too many values to unpack](https://stackoverflow.com/questions/25504964/opencv-python-valueerror-too-many-values-to-unpack) – Dan Mašek Oct 09 '17 at 19:16
  • Looks like you're using OpenCV 3.x (next time, please specify the version), yet writing code intended for 2.x. Some of the API has changed. If in doubt, you can always use [`help`](https://docs.python.org/2/library/functions.html#help). – Dan Mašek Oct 09 '17 at 19:23
  • 1
    Also see https://stackoverflow.com/questions/20851365/opencv-contours-need-more-than-2-values-to-unpack, where the user had the opposite problem. – Warren Weckesser Oct 09 '17 at 20:30

1 Answers1

3

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)
roccolocko
  • 562
  • 5
  • 17