3

画像処理に関してはかなりの素人です。通常のしきい値処理は正常に実行できましたが、適応しきい値処理でエラーが発生しています。これが私のコードです:

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

どんな助けでも大歓迎です。

4

1 に答える 1

5

As per the documentation, the cv2.adaptiveThreshold() returns only 1 value that is the threshold image and in this case you are trying to receive 2 values from that method, that is why ValueError: too many values to unpack error is raised.

After fixing the issue the code may look like:

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()
于 2015-06-16T13:42:00.803 に答える