opencv-python报错Invalid number of channels in input image
说明拾起一份许久未碰的代码,准备丢到一台win10上跑,发现报了一个Invalid number of channels in input image,仔细一看,图像通道无效,以下是解决过程。过程debug后发现,是进行灰度处理这个地方报出的错误cv2.cvtColor(self.image, cv2.BGR2GRAY)详细的报错信息:OpenCV(4.1.0) c:\projects\openc
文章共631字 · 阅读需要大约3分钟
一键AI生成摘要,助你高效阅读
问答
·
说明
拾起一份许久未碰的代码,准备丢到一台win10上跑,发现报了一个Invalid number of channels in input image,仔细一看,图像通道无效,以下是解决过程。
过程
- debug后发现,是进行灰度处理这个地方报出的错误
cv2.cvtColor(self.image, cv2.BGR2GRAY)
- 详细的报错信息:
OpenCV(4.1.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<1,-1,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 2
- 可以看到,既然是输入的图片通道无效,而传入的图片self.image并没有做任何处理,是直接调用self.cap摄像头的read()函数读取到的,理论上应该是通道数为3。
- 于是在灰度处理之前插装了一句print
print(self.image)
# 输出结果:(640, 480, 2)
- 默认的读取方式应该是以RGB三通道进行读取,于是检查了摄像头初始化的代码。发现了当时在在linux上运行时更改了摄像头的编码格式,而两台电脑opencv-python的版本并不一致(代码如下)
self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y', 'U', 'Y', 'V'))
- 把代码注释后发现,图像能够正常的以BGR三通道进行读取
更多推荐
已为社区贡献2条内容
所有评论(0)