OpenCV Error: Assertion failed when using COLOR_BGR2GRAY function
对opencv有个奇怪的问题。当我在笔记本上工作的时候,我没有问题,但是我运行Sublime时就报错:这个错误是:
OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /Users/jenkins/miniconda/1/x64/conda-bld/work/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7935代码如下:
import numpy as np
import cv2
img = [[, , , ],
[, , , ],
[, , , ],
[, , , ]]
img = np.array(img)
def grayscale(x):
# plt.imshow(gray, cmap='gray')to show on screen
# turns dim from (32,32,3) to (32,32)
return cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
img2 = grayscale(img)
你需要在创建数组的时候指定类型,我看img的类型的时候发现imgde类型为:
img.dtype
dtype('int32')
初始化映像的值范围为:0-255,这将对应于数据类型uint8。所以应该改成
img = np.array(img, dtype=np.uint8)
页:
[1]