adaptive thresholding ---ValueError: too many values to unpack

adaptive thresholding ---ValueError: too many values to unpack

我在图像处理方面相当业余。我可以成功地进行正常的阈值处理,但是我在自适应阈值处理中遇到了错误。 这是我的代码:

import cv2

import numpy as np

img = cv2.imread("vehicle004.jpg")

img = cv2.medianBlur(img,5)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

_,th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)

cv2.imshow("window2",th2)

cv2.waitKey(0)

cv2.destroyAllWindows()

错误信息:

line 7, in <module>
    _,th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)
ValueError: too many values to unpack

感谢任何帮助。

根据 documentationcv2.adaptiveThreshold() returns 只有 1 个值是 阈值图像 ,在这种情况下,您正在尝试从该方法接收 2 个值,这就是引发 ValueError: too many values to unpack 错误的原因。

解决问题后,代码可能如下所示:

import cv2

import numpy as np

img = cv2.imread("vehicle004.jpg")

img = cv2.medianBlur(img,5)

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

th2=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)

cv2.imshow("window2",th2)

cv2.waitKey(0)

cv2.destroyAllWindows()