图片转灰度cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
报错如下:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.4) d:\a\opencv-python\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::A0x5252fd2b::Set<1,-1,-1>,struct cv::impl::A0x5252fd2b::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 1
定位到转灰度时图片要为三通道,否则报错,在转灰度前,加上如下代码将图转为三通道即可
img = cv2.merge([img,img,img])
采用CV_BGR2GRAY
,
转换公式Gray = 0.1140B + 0.5870G + 0.2989*R
所以单通道肯定会报错的