Python successfully solved the problem when calling cv2: ValueError: not enough values to unpack (expected 3, got 2)

Problem Description

In python, call the findContours function in the import cv2 package, and the call is as follows:

# 寻找轮廓
bimg, contours, hier = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

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)

problem analysis

Because opencv version of the problem, the legacy of the opencvcall findContoursreturned when the function is three values, the new version of the opencvcall findContoursis returned when the function is two values. The passing value of the findContours function changes from three values ​​to two values.

So if your opencv version is too high and you pass three values, then an error will be reported.

problem solved

Will be bimg, contours, hier = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)changed to contours, hier = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE), the problem is successfully solved.

Guess you like

Origin blog.csdn.net/ywsydwsbn/article/details/108441948