为了账号安全,请及时绑定邮箱和手机立即绑定

OpenCV特定颜色提取

OpenCV特定颜色提取

翻过高山走不出你 2023-09-05 15:28:43
我正在尝试从图像中提取所有蓝色通道项目,这是我的小脚本:import cv2import argparseparser = argparse.ArgumentParser(description='Process image signatures.')parser.add_argument('inputfile', metavar='InFile', type=str, nargs=1,                   help='input image file with signatures')parser.add_argument('outputfile', metavar='OutFile', type=str, nargs=1,                   help='output image file with signatures')args = parser.parse_args()img = cv2.imread(args.inputfile[0], 0)# Convert color to hsvhsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)# set range for blue colorlower_range = np.array([110,50,50])upper_range = np.array([130,255,255])# apply maskmask = cv2.inRange(hsv, lower_range, upper_range)# apply colorres = cv2.bitwise_and(img,img, mask= mask)cv2.imshow('res', res)cv2.imwrite(args.outputfile[0], res)现在我发现Invalid number of channels in input image这张 jpg 图像至少有 3 个颜色通道。完整错误如下:$ python c:/Users/LenovoPC/Desktop/signature_extractor-master/signature_extractor-master/blue_img_extractor.py ./inputs/in1.jpg ./outputs/output1.pngTraceback (most recent call last):  File "c:/Users/LenovoPC/Desktop/signature_extractor-master/signature_extractor-master/blue_img_extractor.py", line 16, in <module>    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)cv2.error: OpenCV(4.4.0) c:\users\appveyor\appdata\local\temp\1\pip-req-build-cff9bdsm\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__thiscall cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0x5406ba9d::Set<3,-1,-1>,struct cv::impl::A0x5406ba9d::Set<0,5,-1>,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这里到底有什么问题?谢谢。
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

这是因为此行在出现错误的行之前:

img = cv2.imread(args.inputfile[0], 0)

参数0已通过,即cv::IMREAD_GRAYSCALE

删除零,或使用 1 强制颜色:

img = cv2.imread(args.inputfile[0])


查看完整回答
反对 回复 2023-09-05
  • 1 回答
  • 0 关注
  • 172 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信