python image processing - solve the error ValueError: not enough values to unpack (expected 3, got 2)

Solve the error ValueError: not enough values ​​to unpack (expected 3, got 2)

Error message:

Traceback (most recent call last):
  File "E:/workspace/code/detect/ShapeDetect.py", line 86, in <module>
    ld.analysis(src)
  File "E:/workspace/code/detect/ShapeDetect.py", line 18, in analysis
    out_binary, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)  #
ValueError: not enough values to unpack (expected 3, got 2)


进程已结束,退出代码为 1

When using opencv for key point recognition and edge contour extraction, the above error is prompted. The reason for the error is that the number of return values ​​defined by the function is inconsistent with the number of return values ​​actually assigned when calling the function. This problem is actually caused by the inconsistency of the opencv version. The new version of opencv (opencv4) only needs two parameters when calling findContours. You need to delete the first parameter (out_binary in the wrong line of code in this article), and only need to delete one parameter. It can be solved perfectly; opencv3 needs three parameters, modify the operating environment, and use opencv3.6 can also be solved.

Workaround
method 1

删除第一个参数,即代码改为:
contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

Method 2

修改opencv版本,降至opencv3

Original link:
https://blog.csdn.net/qq_31460605/article/details/127858911?ops_request_misc=&request_id=&biz_id=102&utm_term=python%20opencv%20ValueError:%20not%20&utm_medium=distribute.pc_search_res ult.none -task-blog- 2 all sobaiduweb~default-0-127858911.142 v86 insert_down1,239 v2 insert_chatgpt&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/weixin_44598554/article/details/130575489