---------------------------------------------------------------------------
ValueError跟踪(最近一次调用)在1 im = image_list[4] 2 ret,thresh = cv2.threshold(im,127,255,0) ----> 3 image, contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE) 4 image = cv2.drawContours(image, contours, -1, (0,255,0), 3) 5
中
ValueError:没有足够的值来解包(预期3,got 2)
发布于 2020-06-08 23:21:33
不同版本的OpenCV返回来自cv2.findContos的不同数量的项目。
OpenCV 4和OpenCV 2具有类似的行为,返回两个项,而OpenCV 3返回三个项。
你的版本显然只需要两个项目。所以试试
contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
或者,如果您想要某种版本无关的内容,那么如果您需要使用层次结构
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hierarchy = contours[1] if len(contours) == 2 else contours[2]
contours = contours[0] if len(contours) == 2 else contours[1]
或者如果你只想要轮廓
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
https://stackoverflow.com/questions/62272551
复制相似问题