OpenCV错误-cv2.cvtcolor [英] OpenCV error - cv2.cvtcolor
问题描述
我是openCV的新手,但我一直无法解决此错误.我正在尝试使用此代码将图像从BGR转换为灰度格式-
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-
img = cv2.imread('path//to//image//file')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
这似乎工作正常.我检查了img
变量的数据类型,该数据类型原来是numpy ndarray,形状是(100,80,3)
.但是,如果给出的图像已经存在于numpy ndarray数据类型的代码中,并且尺寸与cvtColor
函数的输入相同,它将给我以下错误-
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-
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
第二种情况的代码是(在此处创建自定义np.ndarray)-
The code for the second case is (making a custom np.ndarray over here)-
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?
推荐答案
这是因为您的numpy数组不是由正确的数据类型组成的.默认情况下,数组类型为np.int64
(64位),但是cv2.cvtColor()
需要8位(np.uint8
)或16位(np.uint16
).要解决此问题,请更改您的np.full()
函数以包括数据类型:
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:
img = np.full((100,80,3), 12, np.uint8)
这篇关于OpenCV错误-cv2.cvtcolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!