python - 类型错误 : src data type = 17 is not supported

标签 python opencv

我现在在一个程序中尝试将图片从正常更改为二进制化。所以我在 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_8UC1CV_32FC1

检查imgshapedtype,并根据需要调整img

关于python - 类型错误 : src data type = 17 is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732836/

相关文章:

Android 6 原生相机

python - 当我尝试向 defaultdict(str) 添加值时出现错误

python - 使用循环进行网页抓取并写入 csv

python - 具有正弦波纹的 n 阶多项式的曲线拟合

c++ - 如何在 Xcode 中使用 OpenCV 3.4.14 识别 .mp4 视频文件的编解码器?

c - 将 OpenCV Mat 图像与英特尔 IPP 一起使用?

python - Python3 中的列表索引是如何工作的?

python - FFmpeg 使用 python 缩放

python - getLayerNames() 和 getUnconnecteddOutLayers() 函数的工作原理和输出是什么?

c++ - 矩阵(dis)分配