首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV特定颜色提取

OpenCV特定颜色提取
EN

Stack Overflow用户
提问于 2020-09-24 02:51:15
回答 1查看 40关注 0票数 1

我正在尝试从图像中提取所有蓝色通道项,这是我的小脚本:

代码语言:javascript
复制
import cv2
import argparse

parser = 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 hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# set range for blue color
lower_range = np.array([110,50,50])
upper_range = np.array([130,255,255])

# apply mask
mask = cv2.inRange(hsv, lower_range, upper_range)

# apply color
res = 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个颜色通道。

完全错误如下:

代码语言:javascript
复制
$ python c:/Users/LenovoPC/Desktop/signature_extractor-master/signature_extractor-master/blue_img_extractor.py ./inputs/in1.jpg ./outputs/output1.png
Traceback (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

这里到底有什么问题?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-24 03:10:21

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

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

传递的0参数为cv::IMREAD_GRAYSCALE

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

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

在文档中阅读更多内容:Image file reading and writing

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64034510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档