Python 类型错误:不支持 src 数据类型 = 17

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41732836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:31:54  来源:igfitidea点击:

TypeError: src data type = 17 is not supported

pythonopencv

提问by Law.X

I'm now in a program try to change pictures from normal to binaryzation.So i use opencv on python, but when i finish my problem in my home carry my code to my office it come up with a unknown error.So i come to here ,looking for help.

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

The traceback is

回溯是

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

回溯(最近一次调用):文件“D:/tensorflow/opencv.py”,第 12 行,在 ret,thresh1 = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY) TypeError: src data type = 17 is不支持

回答by Miki

You can find a list of OpenCV types here.

您可以在此处找到 OpenCV 类型列表。

type = 17means that your image is a CV_8SC3, aka a 3 channel matrix of char. However, thresholdaccepts only

type = 17意味着您的图像是CV_8SC3,也就是 的 3 通道矩阵char。但是,threshold只接受

(single-channel, 8-bit or 32-bit floating point).

(单通道、8 位或 32 位浮点)。

which means that the type must be either CV_8UC1or CV_32FC1.

这意味着类型必须是CV_8UC1or CV_32FC1

Check shapeand dtypeof your img, and adjust imgas required.

检查shapedtype你的img,调整img的要求。