ValueError: need more than 2 values to unpack - When reading GRAYSCALE

ValueError: need more than 2 values to unpack - When reading GRAYSCALE

我正在从这样的字符串中读取图像。我知道图像是灰度的。

nparr = np.fromstring(image_string, np.uint8)
roi = cv2.imdecode(nparr,cv2.IMREAD_GRAYSCALE)

但是当我获取图像的 shape 时出现错误

h,w,d = roi.shape #gives error 

我得到的错误是:

    h, w, d = roi.shape
ValueError: need more than 2 values to unpack

如果我将 cv2.IMREAD_GRAYSCALE 更改为 cv2.IMREAD_COLOR 那么我不会收到错误,但我相信这样做会稍微改变我的图像,因为当我对其进行进一步处理时会得到不同的结果。

roi.shape 只有两个值。

nparr = np.fromstring(image_string, np.uint8)
roi = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
h, w = roi.shape

External reference