打开cv错误:(-215)scn == 3 || scn == 4在函数cvtColor中 [英] open cv error: (-215) scn == 3 || scn == 4 in function cvtColor

查看:194
本文介绍了打开cv错误:(-215)scn == 3 || scn == 4在函数cvtColor中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Ubuntu 14.04中使用python 2.7和cv2.

I'm currently in Ubuntu 14.04, using python 2.7 and cv2.

当我运行此代码时:

import numpy as np
import cv2

img = cv2.imread('2015-05-27-191152.jpg',0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

它返回:

 File "face_detection.py", line 11, in <module>
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: /home/arthurckl/Desktop/opencv-3.0.0-rc1/modules/imgproc/src/color.cpp:7564: error: (-215) scn == 3 || scn == 4 in function cvtColor

我已经在这里搜索过,一个回答说我可能以错误的方式加载照片,因为它应该具有3个维度:行,列和深度.

I already searched here and one answer said that I could be loading my photo the wrong way, because it should have 3 dimensions: rows, columns and depth.

当我打印img.shape时,它仅返回两个数字,因此我一定做错了.但是我不知道加载照片的正确方法.

When I print the img.shape it returns only two numbers, so I must be doing it wrong. But I don't know the right way to load my photo.

推荐答案

使用正斜杠给出图像的完整路径.它为我解决了错误.

Give the full path of image with forward slash. It solved the error for me.

例如

import numpy as np
import cv2

img = cv2.imread('C:/Python34/images/2015-05-27-191152.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

此外,如果在使用cv2.imread加载图像时在第二个参数中给出0,而不需要使用cvtColor转换图像,则该图像已作为灰度图像加载.例如

Also, if you give 0 in second parameter while loading image using cv2.imread than no need to convert image using cvtColor, it is already loaded as grayscale image eg.

import numpy as np
import cv2

gray = cv2.imread('C:/Python34/images/2015-05-27-191152.jpg',0)

这篇关于打开cv错误:(-215)scn == 3 || scn == 4在函数cvtColor中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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