我现在在一个程序中尝试将图片从正常更改为二进制化。所以我在 python 上使用 opencv,但是当我在家里解决我的问题时,将我的代码带到我的办公室,它出现了一个未知错误。所以我来这里寻求帮助。
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import cv2
im = Image.open('card.jpg')
img = np.array(im)
if img.ndim == 3:
img = img[:, :,0]
plt.gray()
ret, thresh1 = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
plt.subplot(222)
plt.imshow(thresh1)
plt.show()
回溯是
Traceback (most recent call last): File "D:/tensorflow/opencv.py", line 12, in ret, thresh1 = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY) TypeError: src data type = 17 is not supported
最佳答案
您可以找到 OpenCV 类型列表 here .
type = 17
表示您的图像是 CV_8SC3
,也就是 char
的 3 channel 矩阵。然而,threshold
只接受
(single-channel, 8-bit or 32-bit floating point).
这意味着类型必须是 CV_8UC1
或 CV_32FC1
。
检查img
的shape
和dtype
,并根据需要调整img
。
关于python - 类型错误 : src data type = 17 is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732836/