OpenCV错误:使用COLOR_BGR2GRAY函数时断言失败 [英] OpenCV Error: Assertion failed when using COLOR_BGR2GRAY function

查看:190
本文介绍了OpenCV错误:使用COLOR_BGR2GRAY函数时断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在opencv上遇到了一个奇怪的问题.在jupyter笔记本中工作时,我没有任何问题,但是在尝试运行此Sublime时却没有问题.

I'm having a weird issue with opencv. I have no issues when working in a jupyter notebook but do when trying to run this Sublime.

错误是:OpenCV错误:断言失败(深度== CV_8U ||深度== CV_16U ||深度== CV_32F)在cvtColor中,文件/Users/jenkins/miniconda/1/x64/conda-bld/work /opencv-3.1.0/modules/imgproc/src/color.cpp,第7935行

The error is: 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 = [[[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]], 
       [[150,190,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],
       [[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],
       [[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]]]

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)

推荐答案

创建数组时需要指定数据类型.

You need to specify the data type when you create the array.

当我在此处尝试此代码并检查imgdtype时,我看到以下内容:

When I try this code here, and check the dtype of img, I see the following:

>>> img.dtype
dtype('int32')

cv2.cvtColor .

用于初始化图像的值的范围似乎落在0-255之间,这对应于数据类型uint8.

The range of values you initialize your image with appears to fall into 0-255, which would correspond to data type uint8.

所以,随便

img = np.array(img, dtype=np.uint8)

这篇关于OpenCV错误:使用COLOR_BGR2GRAY函数时断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆