Python OpenCV 错误 - cv2.cvtcolor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50319617/
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
OpenCV error - cv2.cvtcolor
提问by Shalabh Singh
I am a newbie to openCV and I am stuck at this error with no resolution. I am trying to convert an image from BGR to grayscale format using this code-
我是 openCV 的新手,我被这个错误困住了,没有解决办法。我正在尝试使用此代码将图像从 BGR 转换为灰度格式-
img = cv2.imread('path//to//image//file')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
This seems to be working fine. I checked the data type of the img
variable which turns out to be numpy ndarray and shape to be (100,80,3)
. However if I give an an image already present in code of numpy ndarray data type and of same dimensions as input to the cvtColor
function, it gives me the following error-
这似乎工作正常。我检查了img
变量的数据类型,结果是 numpy ndarray 和 shape 是(100,80,3)
. 但是,如果我给出一个图像已经存在于 numpy ndarray 数据类型的代码中并且与cvtColor
函数的输入具有相同的维度,它会给我以下错误 -
Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file D:\Build\OpenCV\opencv-3.4.1\modules\imgproc\src\color.cpp, line 11109
cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\imgproc\src\color.cpp:11109: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
The code for the second case is (making a custom np.ndarray over here)-
第二种情况的代码是(在此处制作自定义 np.ndarray)-
img = np.full((100,80,3), 12)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Can anyone clarify what is the reason for this error and how to rectify it?
谁能澄清这个错误的原因是什么以及如何纠正它?
回答by D.Griffiths
This is because your numpy array is not made up of the right data type. By default makes an array of type np.int64
(64 bit), however, cv2.cvtColor()
requires 8 bit (np.uint8
) or 16 bit (np.uint16
). To correct this change your np.full()
function to include the data type:
这是因为您的 numpy 数组不是由正确的数据类型组成。默认情况下,创建类型为np.int64
(64 位)的数组,但是,cv2.cvtColor()
需要 8 位 ( np.uint8
) 或 16 位 ( np.uint16
)。要更正此更改,请更改您的np.full()
函数以包含数据类型:
img = np.full((100,80,3), 12, np.uint8)
img = np.full((100,80,3), 12, np.uint8)
回答by Shalabh Singh
The error occured because the datatype of numpy array returned by cv2.imread
is uint8
, which is different from the datatype of numpy array returned by np.full()
. To make the data-type as uint8, add the dtype
parameter-
发生错误是因为 返回的 numpy 数组的数据类型cv2.imread
是uint8
,与 返回的 numpy 数组的数据类型不同np.full()
。要将数据类型设为 uint8,请添加dtype
参数-
img = np.full((100,80,3), 12, dtype = np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
回答by A.Ametov
It may be easier to initialize new numpy array with initial image as source and dtype=np.uint8
:
使用初始图像作为源初始化新的 numpy 数组可能更容易,并且dtype=np.uint8
:
import numpy as np
img = cv2.imread('path//to//image//file')
img = np.array(img, dtype=np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
回答by Khyar Ali
imagine you have a function called preprocessing() that preprocess your images with cv2, if you try to apply it as:
想象一下,如果您尝试将其应用为:
data = np.array(list(map(preprocessing,data)))
it won't work and that because np.array creates int64and you are trying to assign np.uint8 to it, what you should do instead is adding dtype parameter as follow:
它不会工作,因为 np.array 创建 int64 并且您试图将 np.uint8 分配给它,您应该做的是添加 dtype 参数如下:
data = np.array(list(map(preprocessing,data)), dtype = np.uint8)