opencv小白的错误总结:颜色空间转换出错

import numpy as np
import cv2
img=np.mat(np.zeros((300,300)))

img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
print(img.shape)
cv2.imshow("test",img)
cv2.waitKey(0)
pycharm下运行如上代码时,出现错误:

OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file D:\Build\OpenCV\opencv-3.4.0\modules\imgproc\src\color.cpp, line 11073
Traceback (most recent call last):
  File "E:/untitled1/examples1.py", line 5, in
    img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

cv2.error: D:\Build\OpenCV\opencv-3.4.0\modules\imgproc\src\color.cpp:11073: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor

错误:dtype 是 uint8,后者则为 float64

错误修改:添加一行:

img=np.mat(np.zeros((300,300)))
img=img.astype(np.float32)
img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

你可能感兴趣的:(opencv)