基于opencv-python 执行 cv2.findContours()函数出现以下错误提示:
Traceback (most recent call last):
contours, hierarchy = cv2.findContours(gray_temp, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack (expected 2)
代码如下:
# find connected domain in image
def findContours(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_temp = gray.copy()
contours, hierarchy = cv2.findContours(gray_temp, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contoursSize = np.size(contours)
return contours, hierarchy, contoursSize
#print("contoursSize is ", contoursSize)
原因:
基于opncv4.1.0的 findContours 是可以接受两个返回值的
但是基于opencv3.4.15的 findContours是需要接受三个返回值的
修改为:binary, contours, hierarchy = cv2.findContours(gray_temp, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)