使用Opencv imshow时出现Invalid number of channels in input image:

问题描述:

在使用imshow时,出现以下报错

cv.imshow('image', target)
cv2.error: OpenCV(4.5.2) c:\users\runneradmin\appdata\local\temp\pip-req-build-vi271kac\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::A0x0d84c244::Set<3,4,-1>,struct cv::impl::A0x0d84c244::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 120
 
 

原因分析:

报错显示是通道数不同,因为opencv imshow函数默认的通道,应该是第三维才是通道数。

我们可以打印一下图片的shape来看看

我这里是(3, 60, 120),只需要用转置函数,把第一维放到最后一维就好了


解决方案:

target = target.transpose((1, 2, 0))

おすすめ

転載: blog.csdn.net/lzzzzzzm/article/details/119985849