简体   繁体   English

从 BGR 到灰度的 OpenCV 颜色转换错误

[英]Error in OpenCV color conversion from BGR to grayscale

I am trying to convert an image from BGR to grayscale format using this code:我正在尝试使用以下代码将图像从 BGR 转换为灰度格式:

img = cv2.imread('path//to//image//file')
gray = cv2.cvtColor(img, 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) .这似乎工作正常:我检查了img变量的数据类型,结果是 numpy ndarray 和形状是(100,80,3) However if I give an image of a native numpy ndarray data type with same dimensions of the input of the cvtColor function, it gives me the following error:但是,如果我给出与cvtColor函数输入维度相同的本机 numpy ndarray 数据类型的图像,则会出现以下错误:

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(img, cv2.COLOR_BGR2GRAY) 

Can anyone clarify what is the reason for this error and how to rectify it?谁能澄清这个错误的原因是什么以及如何纠正它?

This is because your numpy array is not made up of the right data type.这是因为您的 numpy 数组不是由正确的数据类型组成。 By default makes an array of type np.int64 (64 bit), however, cv2.cvtColor() requires 8 bit ( np.uint8 ) or 16 bit ( np.uint16 ).默认情况下np.int64 (64 位)类型的数组,但是, cv2.cvtColor()需要 8 位( np.uint8 )或 16 位( np.uint16 )。 To correct this change your np.full() function to include the data type:要更正此更改np.full()函数以包含数据类型:

img = np.full((100,80,3), 12, np.uint8)

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() .出现错误是因为cv2.imread返回的 numpy 数组的数据类型是uint8 ,与np.full()返回的 numpy 数组的数据类型不同。 To make the data-type as uint8, add the dtype parameter-要将数据类型设为 uint8,请添加dtype参数-

img = np.full((100,80,3), 12, dtype = np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

It may be easier to initialize new numpy array with initial image as source and dtype=np.uint8 :使用初始图像作为源和dtype=np.uint8初始化新的 numpy 数组可能更容易:

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM