基於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)