查看: 1345|回复: 1

OpenCV Error: Assertion failed when using COLOR_BGR2GRAY function

[复制链接]

6

主题

21

帖子

65

积分

注册会员

Rank: 2

积分
65
发表于 2018-10-12 16:30:46 | 显示全部楼层 |阅读模式
对opencv有个奇怪的问题。当我在笔记本上工作的时候,我没有问题,但是我运行Sublime时就报错:
这个错误是:
  1. 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
复制代码
代码如下:
  1. import numpy as np
  2. import cv2

  3. img = [[[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],
  4.        [[150,190,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],
  5.        [[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],
  6.        [[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]]]

  7. img = np.array(img)

  8. def grayscale(x):
  9.     # plt.imshow(gray, cmap='gray')to show on screen
  10.     # turns dim from (32,32,3) to (32,32)
  11.     return cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)

  12. img2 = grayscale(img)
复制代码






回复

使用道具 举报

3

主题

55

帖子

131

积分

注册会员

Rank: 2

积分
131
发表于 2018-10-12 16:37:03 | 显示全部楼层
你需要在创建数组的时候指定类型,我看img的类型的时候发现imgde类型为:
  1. img.dtype
  2. dtype('int32')
复制代码


初始化映像的值范围为:0-255,这将对应于数据类型uint8。所以应该改成
  1. img = np.array(img, dtype=np.uint8)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表