findContours,ValueError: too many values to unpack (expected 2)报错
程序员文章站
2024-01-20 16:56:10
...
源代码发生报错:
contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS
ValueError: too many values to unpack (expected 2)
contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS
原因是findContours函数输出三个元素,源码只有两个元素来承接,所以要增加一个元素来承接。
修改成:
binary, contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
binary, contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
这样就不会报错了