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

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

问题描述

我目前使用的是 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 ||函数 cvtColor 中的 scn == 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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