0. 颜色模式

  • RGB 模式(百万种颜色)
  • CMYK 模式(四种印刷色)
  • 索引模式(256 种颜色)
  • 灰度模式(256 级灰度)
  • 位图模式(两种颜色)
0.1 灰度模式

也就是灰度图(黑白照片),每个像素只有明暗变化,用0~255共256个亮度级来表示,用8个bit来表示,所以每个像素信息用8bit储存.

0.2 位图模式(二值图像)

即只有纯黑和纯白两种亮度,没有渐变的亮度级,通常用0表示纯黑,1表示纯白,所以每个像素信息用1bit表示即可

0.3 RGB模式:基于发光体(电子产品)的色彩模式

即用RGB三原色表示所种颜色,每个像素储存RGB三个分量的亮度,一个像素储存需要8bit*3=24bit(当然如果有更高要求,则会超过24bit,因为一个亮度等级可能不止0~255,需要超过8bit来表示),常见的24bit色彩大概是1678万种,也就是常见的1600万真彩色。

#### 0.4
依次为:

  • 1 RGB 模式(百万种颜色)
  • 2 CMYK 模式(四种印刷色)
  • 3 索引模式(256 种颜色)
  • 4 灰度模式(256 级灰度)
  • 5 位图模式(两种颜色)

CMYK模式:基于印刷的色彩模式
cyan(青色),magenta(洋红),yellow(黄色),black(黑色)
CMYK与RGB的区别在于RGB想要某种颜色,RGB分量任意混合即可;但是CMYK作为印刷多次叠加颜色变深(这样更适合印刷)。

0.4 Lab模式:
  • L*代表亮度
  • a*代表从绿色到红色的分量
  • b*代表从蓝色到黄色的分量

基于人对颜色的感觉来设计的,感知均匀:Lab分量变化幅度和人眼感受的颜色变化幅度一样。该模式也容易调整:想要调整亮度,只需要调节亮度分量L,调节色彩就分别调整a和b分量
在这里插入图片描述

4.6 HSL(HSV)色彩模式

即色相(0-180)、饱和度(0-255)、亮度(0-255)
(Hue,Saturation,Lightness)
更符合人眼对颜色的识别,一眼观察颜色首先分辨出色相,同时色相、饱和度、亮度三个数据也容易想象出具体颜色,而RGB三个数据不能直观想象出
在这里插入图片描述

  • 对于HSV分量用归一化表示的话,H色相分量使用红色开始(红-黄-绿方向)到红色结束,那么归一化H=0.67大概表示蓝色色色相,实际H=0.6*180=120
  • S分量表示饱和度,当S=0时表示白色,当S=255,表示饱和度很高
  • V分量表示亮度,当V=0时表示黑色,当V=255,表示亮度很高
  • 所以我们表示蓝色范围可以定义(110,50,50)-(130,255,255)
4.7 索引图

索引图像包含一个数据矩阵data和一个调色板矩阵map,数据矩阵可以使uint8,uint16或双精度类型,而调色板矩阵则总是一个m*3的双精度矩阵,当图像转换成索引模式时,系统自动归纳包含大多数的256种颜色表。主要用于网络发布,例如双方标准化map颜色索引图,只需要传输uint8的数据矩阵,接收方显示时解析即可。

1. 颜色空间的转换

opencv包含了大量的颜色空间转换方法,但是最常用的就是BGR–Gray和BGR–HSV两种

  • cv2.cvtColor(input_image,flag):flag指定转换类型:
flag类型
cv2.COLOR_BGR2GRAYBGR转Gray
cv2.COLOR_BGR2HSVBGR转HSV

扩展:
cv2.COLOR_BGR2BGRA
cv2.COLOR_RGB2RGBA
cv2.COLOR_BGRA2BGR
cv2.COLOR_RGBA2RGB
cv2.COLOR_BGR2RGBA
cv2.COLOR_RGB2BGRA
cv2.COLOR_RGBA2BGR
cv2.COLOR_BGRA2RGB
cv2.COLOR_BGR2RGB
cv2.COLOR_RGB2BGR
cv2.COLOR_BGRA2RGBA
cv2.COLOR_RGBA2BGRA
cv2.COLOR_BGR2GRAY
cv2.COLOR_RGB2GRAY
cv2.COLOR_GRAY2BGR
cv2.COLOR_GRAY2RGB
cv2.COLOR_GRAY2BGRA
cv2.COLOR_GRAY2RGBA
cv2.COLOR_BGRA2GRAY
cv2.COLOR_RGBA2GRAY
cv2.COLOR_BGR2BGR565
cv2.COLOR_RGB2BGR565
cv2.COLOR_BGR5652BGR
cv2.COLOR_BGR5652RGB
cv2.COLOR_BGRA2BGR565
cv2.COLOR_RGBA2BGR565
cv2.COLOR_BGR5652BGRA
cv2.COLOR_BGR5652RGBA
cv2.COLOR_GRAY2BGR565
cv2.COLOR_BGR5652GRAY
cv2.COLOR_BGR2BGR555
cv2.COLOR_RGB2BGR555
cv2.COLOR_BGR5552BGR
cv2.COL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cv2.COLOR_BayerGR2BGR_VNG
cv2.COLOR_BayerBG2RGB_VNG
cv2.COLOR_BayerGB2RGB_VNG
cv2.COLOR_BayerRG2RGB_VNG
cv2.COLOR_BayerGR2RGB_VNG
cv2.COLOR_BayerBG2BGR_EA
cv2.COLOR_BayerGB2BGR_EA
cv2.COLOR_BayerRG2BGR_EA
cv2.COLOR_BayerGR2BGR_EA
cv2.COLOR_BayerBG2RGB_EA
cv2.COLOR_BayerGB2RGB_EA
cv2.COLOR_BayerRG2RGB_EA
cv2.COLOR_BayerGR2RGB_EA
cv2.COLOR_BayerBG2BGRA
cv2.COLOR_BayerGB2BGRA
cv2.COLOR_BayerRG2BGRA
cv2.COLOR_BayerGR2BGRA
cv2.COLOR_BayerBG2RGBA
cv2.COLOR_BayerGB2RGBA
cv2.COLOR_BayerRG2RGBA
cv2.COLOR_BayerGR2RGBA
cv2.COLOR_COLORCVT_MAX

2. 利用颜色来追踪某物体

比如我们使用摄像头检测某个蓝色的物体:

  • 获取帧图像
  • 将图像转换到HSV空间
  • 设置HSV阈值到蓝色范围
    为什么不直接用RGB呢?因为即使是一切浅红色前绿色都会有蓝色分量,所以其实我们不好再BGR空间定义蓝色,如果将绿色红色分量定义为0,只设置蓝色分量,其实这样的蓝色更接近纯蓝色,限制了实际蓝色的范围。
    给出常见颜色的HSV最值范围:
    在这里插入图片描述
    具体实现参考:opencv-python掩膜操作(进阶篇之颜色追踪)
    需要用到opencv掩膜操作和HSV颜色空间两个知识点
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐