Problem description

The findContours function is called in python from the import cv2 package as follows:

Bimg, contours, hier = cv2.findContours(gray, cv2.retr_external, cv2.chain_approx_simple) 12Copy the code

The following problems occur:

Traceback (most recent call last):
  File "C:\Users\Y\Desktop\transformation_demo.py", line 9, in <module>
    bimg, contours, hier = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)
1234
Copy the code

Problem analysis

Due to a problem with the opencV version, the old version of OpencV calls findContours and returns three values. The new version calls findContours and returns two values. The findContours function passes two values instead of three.

So if your version of OpencV is too high and passes three values, an error will be reported.

Problem solving

Change bimg, contours, hier = cv2.findContours(gray, cv2.retr_external, cv2.chain_approx_simple) to contours, Hier = cv2.findContours(gray, cv2.retr_external, cv2.chain_approx_simple).