人工智能计算机视觉先锋——OpenCv 的颜色检测
ztj100 2024-11-21 00:30 16 浏览 0 评论
在计算机的世界里,只有 0 或者1,如何让计算机认识颜色是计算机视觉工作者首先需要考虑的事情,我们知道整个世界的颜色虽然五彩缤纷,但是都是3种原色彩合成的(R G B),有了(R G B)三源色,便可以通过调节不同的颜色比例来达到其他颜色的效果。
在计算机的世界里面,我们定义了(R G B)三源色的像素都在(0-255之间),通过配置这些数字,便可以显示不同的色彩
首先定义一个数组colist:
import numpy as np
import cv2
image = cv2.imread("11.jpg")
colist = [
([17, 15, 100], [50, 56, 200]),
([86, 31, 4], [220, 88, 50]),
([25, 146, 190] , [62, 174, 250]),
([103, 86, 65], [145, 133, 128])
]
在RGB色彩空间,当然opencv是在BGR的空间,定义了四种不同颜色的像素,同时代表了R G B ,gray
在这里,我们说图像中所有具有R> = 100,G> = 15和B> = 17以及R <= 200,G<= 56和B <= 50的像素将被视为 红色,
具有R> = 4,G> = 31和B> = 86以及R <= 50,G<= 88和B <= 220的像素将被视为绿色
具有R> = 190,G> = 146和B> = 25以及R <= 250,G<= 174和B <= 62的像素将被视为黄色
具有R> = 65,G> = 86和B> = 103以及R <= 128,G<= 133和B <= 145的像素将被视为灰色
有了这些颜色的定义,我们便可以使用上下限的颜色数据,对图片中的颜色进行检测,
for (lower, upper) in colist:
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)
cv2.imshow("images", np.hstack([image, output]))
cv2.waitKey(0)
首先使用cv2.inRange(image, lower, upper)函数进行图片颜色的筛选
mask = cv2.inRange(image, lower, upper)
函数参数有三个
第一个参数:image指的是原图
第二个参数:lower指的是图像中低于这个lower的值,图像值变为0
第三个参数:upper指的是图像中高于这个upper的值,图像值变为0
而在lower~upper之间的值变成255
按照红色为例:通过以上函数的筛选,不是红色区域的都被置为0,而红色区域都被置为255
经过以上步骤,mask便是一个黑白的照片,其中不是红色的区域为黑色,红色区域为白色
然后使用cv2.bitwise_and函数对图片进行与操作
cv2.bitwise_and()是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1&1=1,1&0=0,0&1=0,0&0=0
利用掩膜(mask)进行“与”操作,即掩膜图像白色区域是对需要处理图像像素的保留,黑色区域是对需要处理图像像素的剔除
这样我们便可以删除了除红色区域以外的其他颜色,保留了红色,当然其他颜色具有类似的原理
最后我们显示一下图片中的颜色
当然你也可以使用L*a*b颜色空间,或者hsv颜色空间,什么叫L*a*b颜色空间,或者hsv颜色空间
以上2篇文章主要介绍了lab空间的颜色使用
hsv颜色空间的使用,可以参考小编的专栏《打造属于自己的天眼目标追踪系统》中的颜色追踪
- 上一篇:numpy小记
- 下一篇:数据分析-numpy的拼接与交换
相关推荐
- 10条军规:电商API从数据泄露到高可用的全链路防护
-
电商API接口避坑指南:数据安全、版本兼容与成本控制的10个教训在电商行业数字化转型中,API接口已成为连接平台、商家、用户与第三方服务的核心枢纽。然而,从数据泄露到版本冲突,从成本超支到系统崩溃,A...
- Python 文件处理在实际项目中的困难与应对策略
-
在Python项目开发,文件处理是一项基础且关键的任务。然而,在实际项目中,Python文件处理往往会面临各种各样的困难和挑战,从文件格式兼容性、编码问题,到性能瓶颈、并发访问冲突等。本文将深入...
- The Future of Manufacturing with Custom CNC Parts
-
ThefutureofmanufacturingisincreasinglybeingshapedbytheintegrationofcustomCNC(ComputerNumericalContro...
- Innovative Solutions in Custom CNC Machining
-
Inrecentyears,thelandscapeofcustomCNCmachininghasevolvedrapidly,drivenbyincreasingdemandsforprecisio...
- C#.NET serilog 详解(c# repository)
-
简介Serilog是...
- Custom CNC Machining for Small Batch Production
-
Inmodernmanufacturing,producingsmallbatchesofcustomizedpartshasbecomeanincreasinglycommondemandacros...
- Custom CNC Machining for Customized Solutions
-
Thedemandforcustomizedsolutionsinmanufacturinghasgrownsignificantly,drivenbydiverseindustryneedsandt...
- Revolutionizing Manufacturing with Custom CNC Parts
-
Understandinghowmanufacturingisevolving,especiallythroughtheuseofcustomCNCparts,canseemcomplex.Thisa...
- Breaking Boundaries with Custom CNC Parts
-
BreakingboundarieswithcustomCNCpartsinvolvesexploringhowadvancedmanufacturingtechniquesaretransformi...
- Custom CNC Parts for Aerospace Industry
-
Intherealmofaerospacemanufacturing,precisionandreliabilityareparamount.Thecomponentsthatmakeupaircra...
- Cnc machining for custom parts and components
-
UnderstandingCNCmachiningforcustompartsandcomponentsinvolvesexploringitsprocesses,advantages,andcomm...
- 洞察宇宙(十八):深入理解C语言内存管理
-
分享乐趣,传播快乐,增长见识,留下美好。亲爱的您,这里是LearingYard学苑!今天小编为大家带来“深入理解C语言内存管理”...
- The Art of Crafting Custom CNC Parts
-
UnderstandingtheprocessofcreatingcustomCNCpartscanoftenbeconfusingforbeginnersandevensomeexperienced...
- Tailored Custom CNC Solutions for Automotive
-
Intheautomotiveindustry,precisionandefficiencyarecrucialforproducinghigh-qualityvehiclecomponents.Ta...
- 关于WEB服务器(.NET)一些经验累积(一)
-
以前做过技术支持,把一些遇到的问题累积保存起来,现在发出了。1.问题:未能加载文件或程序集“System.EnterpriseServices.Wrapper.dll”或它的某一个依赖项。拒绝访问。解...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 10条军规:电商API从数据泄露到高可用的全链路防护
- Python 文件处理在实际项目中的困难与应对策略
- The Future of Manufacturing with Custom CNC Parts
- Innovative Solutions in Custom CNC Machining
- C#.NET serilog 详解(c# repository)
- Custom CNC Machining for Small Batch Production
- Custom CNC Machining for Customized Solutions
- Revolutionizing Manufacturing with Custom CNC Parts
- Breaking Boundaries with Custom CNC Parts
- Custom CNC Parts for Aerospace Industry
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)