OpenCV C++(十二)----色彩空间

12.1、常见的色彩空间

12.1.1、RGB色彩空间

RGB色彩空间源于使用阴极射线管(CRT) 的彩色电视。 RGB模型使用加性色彩混合 以获知需要发出什么样的光来产生给定的色彩。 具体色彩的值用三个数值的向量来表 示, 这三个数值分别代表三种基色: Red、 Green、 Blue的亮度。 假设每种基色的数值量 化成m=2^n个数, 如同8位灰度图像一样, 将灰度量化成28 =256个数。 RGB图像的红、 绿、 蓝三个通道的图像都是一幅8位图, 因此颜色的总数为256*3 =16777216, 如(0, 0, 0) 代表黑色,(255, 255, 255)代表白色。

12.1.2、HSV色彩空间

HSV——色调(Hue) 、 饱和度(Saturation) 、 亮度值(Value) , 又称HSB——色调(Hue) 、 饱和度(Saturation) 、亮度(Brightness) ,HSV将亮度信息从彩色中分解出来, 而色调和饱和度与人类感知是相对应的, 因而使得该模型在开发图像算法中非常有用。 如果将对比度增强算法用在RGB的每个分量上, 那么人类对该图像的色彩感知就变得不够理想了; 而如果仅对HSV的亮度分量进行增强,让彩色信息不受影响, 那么效果就会或多或少地与期望相近。

12.1.3、HLS色彩空间

HLS色彩空间主要使用三个数值来描述色彩,分别是H(Hue, 色调) 、 L(Lightness/Luminance, 光亮度/明度) 、 S(Saturation, 饱和度) 。 HLS色彩空间也称 作HSI——Hue(色调) 、 Saturation(饱和度) 、 Intensity(亮度) 。 与HSV类似, HLS只是用“光亮度”替换了“亮度”, 差别在于一种纯色的亮度等于白 色的亮度, 而一种纯色的光亮度等于中度灰的光亮度。 色调用来描述一个纯色彩的基本属性, 即在日常生活中所使用的基本色彩名称, 比如蓝色、 红色等。 亮度是用来描述明亮程度的, 通过百分比的方式来表示, 以[0, 1]范围进行量化。 通过亮度的大小来衡量有多少光线从物体表面反射出来, 是帮助眼睛去感知色彩的重要属性。 显然, 当一个具有色彩的物体处于较暗或者较亮的地方时, 眼睛通常无法正确地分 辨出物体表面原本的色彩。 饱和度是用来描述纯色彩所添加的白光的程度的, 即色彩的纯度。 饱和度通过百分 比的方式来表示, 以[0, 1]范围来量化。 饱和度的数值越高, 则表示色彩的纯度越高; 反 之, 色彩的纯度越低, 比如红色, 会有深红、 浅红之分。

色彩空间的转换接口

void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );

code如下

/** Constants for color conversion */
enum
{
CV_BGR2BGRA    =0,
CV_RGB2RGBA    =CV_BGR2BGRA,
CV_BGRA2BGR    =1,
CV_RGBA2RGB    =CV_BGRA2BGR,

CV_BGR2RGBA    =2,
CV_RGB2BGRA    =CV_BGR2RGBA,

CV_RGBA2BGR    =3,
CV_BGRA2RGB    =CV_RGBA2BGR,

CV_BGR2RGB     =4,
CV_RGB2BGR     =CV_BGR2RGB,

CV_BGRA2RGBA   =5,
CV_RGBA2BGRA   =CV_BGRA2RGBA,

CV_BGR2GRAY    =6,
CV_RGB2GRAY    =7,
CV_GRAY2BGR    =8,
CV_GRAY2RGB    =CV_GRAY2BGR,
CV_GRAY2BGRA   =9,
CV_GRAY2RGBA   =CV_GRAY2BGRA,
CV_BGRA2GRAY   =10,
CV_RGBA2GRAY   =11,

CV_BGR2BGR565  =12,
CV_RGB2BGR565  =13,
CV_BGR5652BGR  =14,
CV_BGR5652RGB  =15,
CV_BGRA2BGR565 =16,
CV_RGBA2BGR565 =17,
CV_BGR5652BGRA =18,
CV_BGR5652RGBA =19,

CV_GRAY2BGR565 =20,
CV_BGR5652GRAY =21,

CV_BGR2BGR555  =22,
CV_RGB2BGR555  =23,
CV_BGR5552BGR  =24,
CV_BGR5552RGB  =25,
CV_BGRA2BGR555 =26,
CV_RGBA2BGR555 =27,
CV_BGR5552BGRA =28,
CV_BGR5552RGBA =29,

CV_GRAY2BGR555 =30,
CV_BGR5552GRAY =31,

CV_BGR2XYZ     =32,
CV_RGB2XYZ     =33,
CV_XYZ2BGR     =34,
CV_XYZ2RGB     =35,

CV_BGR2YCrCb   =36,
CV_RGB2YCrCb   =37,
CV_YCrCb2BGR   =38,
CV_YCrCb2RGB   =39,

CV_BGR2HSV     =40,
CV_RGB2HSV     =41,

CV_BGR2Lab     =44,
CV_RGB2Lab     =45,

CV_BayerBG2BGR =46,
CV_BayerGB2BGR =47,
CV_BayerRG2BGR =48,
CV_BayerGR2BGR =49,

CV_BayerBG2RGB =CV_BayerRG2BGR,
CV_BayerGB2RGB =CV_BayerGR2BGR,
CV_BayerRG2RGB =CV_BayerBG2BGR,
CV_BayerGR2RGB =CV_BayerGB2BGR,

CV_BGR2Luv     =50,
CV_RGB2Luv     =51,
CV_BGR2HLS     =52,
CV_RGB2HLS     =53,

CV_HSV2BGR     =54,
CV_HSV2RGB     =55,

CV_Lab2BGR     =56,
CV_Lab2RGB     =57,
CV_Luv2BGR     =58,
CV_Luv2RGB     =59,
CV_HLS2BGR     =60,
CV_HLS2RGB     =61,

CV_BayerBG2BGR_VNG =62,
CV_BayerGB2BGR_VNG =63,
CV_BayerRG2BGR_VNG =64,
CV_BayerGR2BGR_VNG =65,

CV_BayerBG2RGB_VNG =CV_BayerRG2BGR_VNG,
CV_BayerGB2RGB_VNG =CV_BayerGR2BGR_VNG,
CV_BayerRG2RGB_VNG =CV_BayerBG2BGR_VNG,
CV_BayerGR2RGB_VNG =CV_BayerGB2BGR_VNG,

CV_BGR2HSV_FULL = 66,
CV_RGB2HSV_FULL = 67,
CV_BGR2HLS_FULL = 68,
CV_RGB2HLS_FULL = 69,

CV_HSV2BGR_FULL = 70,
CV_HSV2RGB_FULL = 71,
CV_HLS2BGR_FULL = 72,
CV_HLS2RGB_FULL = 73,

CV_LBGR2Lab     = 74,
CV_LRGB2Lab     = 75,
CV_LBGR2Luv     = 76,
CV_LRGB2Luv     = 77,

CV_Lab2LBGR     = 78,
CV_Lab2LRGB     = 79,
CV_Luv2LBGR     = 80,
CV_Luv2LRGB     = 81,

CV_BGR2YUV      = 82,
CV_RGB2YUV      = 83,
CV_YUV2BGR      = 84,
CV_YUV2RGB      = 85,

CV_BayerBG2GRAY = 86,
CV_BayerGB2GRAY = 87,
CV_BayerRG2GRAY = 88,
CV_BayerGR2GRAY = 89,

//YUV 4:2:0 formats family
CV_YUV2RGB_NV12 = 90,
CV_YUV2BGR_NV12 = 91,
CV_YUV2RGB_NV21 = 92,
CV_YUV2BGR_NV21 = 93,
CV_YUV420sp2RGB = CV_YUV2RGB_NV21,
CV_YUV420sp2BGR = CV_YUV2BGR_NV21,

CV_YUV2RGBA_NV12 = 94,
CV_YUV2BGRA_NV12 = 95,
CV_YUV2RGBA_NV21 = 96,
CV_YUV2BGRA_NV21 = 97,
CV_YUV420sp2RGBA = CV_YUV2RGBA_NV21,
CV_YUV420sp2BGRA = CV_YUV2BGRA_NV21,

CV_YUV2RGB_YV12 = 98,
CV_YUV2BGR_YV12 = 99,
CV_YUV2RGB_IYUV = 100,
CV_YUV2BGR_IYUV = 101,
CV_YUV2RGB_I420 = CV_YUV2RGB_IYUV,
CV_YUV2BGR_I420 = CV_YUV2BGR_IYUV,
CV_YUV420p2RGB = CV_YUV2RGB_YV12,
CV_YUV420p2BGR = CV_YUV2BGR_YV12,

CV_YUV2RGBA_YV12 = 102,
CV_YUV2BGRA_YV12 = 103,
CV_YUV2RGBA_IYUV = 104,
CV_YUV2BGRA_IYUV = 105,
CV_YUV2RGBA_I420 = CV_YUV2RGBA_IYUV,
CV_YUV2BGRA_I420 = CV_YUV2BGRA_IYUV,
CV_YUV420p2RGBA = CV_YUV2RGBA_YV12,
CV_YUV420p2BGRA = CV_YUV2BGRA_YV12,

CV_YUV2GRAY_420 = 106,
CV_YUV2GRAY_NV21 = CV_YUV2GRAY_420,
CV_YUV2GRAY_NV12 = CV_YUV2GRAY_420,
CV_YUV2GRAY_YV12 = CV_YUV2GRAY_420,
CV_YUV2GRAY_IYUV = CV_YUV2GRAY_420,
CV_YUV2GRAY_I420 = CV_YUV2GRAY_420,
CV_YUV420sp2GRAY = CV_YUV2GRAY_420,
CV_YUV420p2GRAY = CV_YUV2GRAY_420,

//YUV 4:2:2 formats family
CV_YUV2RGB_UYVY = 107,
CV_YUV2BGR_UY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_YUY2,
CV_YUV2GRAY_YUYV = CV_YUV2GRAY_YUY2,
CV_YUV2GRAY_YUNV = CV_YUV2GRAY_YUY2,

// alpha premultiplication
CV_RGBA2mRGBA = 125,
CV_mRGBA2RGBA = 126,

CV_RGB2YUV_I420 = 127,
CV_BGR2YUV_I420 = 128,
CV_RGB2YUV_IYUV = CV_RGB2YUV_I420,
CV_BGR2YUV_IYUV = CV_BGR2YUV_I420,

CV_RGBA2YUV_I420 = 129,
CV_BGRA2YUV_I420 = 130,
CV_RGBA2YUV_IYUV = CV_RGBA2YUV_I420,
CV_BGRA2YUV_IYUV = CV_BGRA2YUV_I420,
CV_RGB2YUV_YV12  = 131,
CV_BGR2YUV_YV12  = 132,
CV_RGBA2YUV_YV12 = 133,
CV_BGRA2YUV_YV12 = 134,

// Edge-Aware Demosaicing
CV_BayerBG2BGR_EA = 135,
CV_BayerGB2BGR_EA = 136,
CV_BayerRG2BGR_EA = 137,
CV_BayerGR2BGR_EA = 138,

CV_BayerBG2RGB_EA = CV_BayerRG2BGR_EA,
CV_BayerGB2RGB_EA = CV_BayerGR2BGR_EA,
CV_BayerRG2RGB_EA = CV_BayerBG2BGR_EA,
CV_BayerGR2RGB_EA = CV_BayerGB2BGR_EA,

CV_BayerBG2BGRA =139,
CV_BayerGB2BGRA =140,
CV_BayerRG2BGRA =141,
CV_BayerGR2BGRA =142,

CV_BayerBG2RGBA =CV_BayerRG2BGRA,
CV_BayerGB2RGBA =CV_BayerGR2BGRA,
CV_BayerRG2RGBA =CV_BayerBG2BGRA,
CV_BayerGR2RGBA =CV_BayerGB2BGRA,

CV_COLORCVT_MAX  = 143
};      

12.2调整彩色图像的饱和度和亮度

因为在HLS或HSV色彩空间中都将饱和度和亮度单独分离出来, 所以首先将RGB图像转换为HLS或HSV图像, 然后调整饱和度和亮度分量, 最后将调整后的HLS或者HSV图像转换为RGB图像。

算法步骤:

  • 第一步:将RGB图像值归一化到[0,1];
  • 第二步:使用函数cvtColor进行色彩空间的转换;
  • 第三步:根据处理灰度图像对比度增强的伽马变换或者线性变换调整饱和度和亮度分量;
  • 第四步:再转换到RGB色彩空间。

你可能感兴趣的:(OpenCV C++(十二)----色彩空间)