想要找到轮廓 -> ValueError: not enough values to unpack (expected 3, got 2),这个错误出现了。

22 浏览
0 Comments

想要找到轮廓 -> ValueError: not enough values to unpack (expected 3, got 2),这个错误出现了。

我的简单Python代码是这样的:

import cv2
img = cv2.imread('Materials/shapes.png')
blur = cv2.GaussianBlur(img, (3,3), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
returns, thresh = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY)
contours, hierachy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    area = cv2.contourArea(cnt) # 轮廓面积
    if (area > 1220):
        cv2.drawContours(img, [cnt], -1, (0, 255, 0), 2)
        cv2.imshow('RGB', img)
        cv2.waitKey(1000)
        print(len(cnt))
import numpy as np
contours = np.array(contours)
print(contours)

这个代码之前运行得很好。但是最近,即使我没有做任何更改,却出现了以下错误:

ret, contours, hierachy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

0