DrGraph原理示教 - OpenCV 4 功能 - 颜色空间

前言

前段时间,甲方提出明确需求,让把软件国产化。稍微研究了一下,那就转QT开发,顺便把以前的功能代码重写一遍。
至于在Ubuntu下折腾QT、OpenCV安装事宜,网上文章很多,照猫画虎即可。
这个过程,我发现几个好东东,使得开发效率大幅提高:

  1. QT 6支持C++ 17,里面很多C++的新功能,用起来更为舒爽。
  2. QT Creator中,代码提示更快捷,代码折叠也很准确
  3. QT Creator中,文档生成更方便,配合Doxygen之后,自己都感觉更专业了在这里插入图片描述
  4. 在这里插入图片描述
    当然,更让我高兴的是,QT直接支持最新版的OpenCV,以前还一直停留在2.3版。趁这个机会,把OpenCV的应用功能也升级起来。
    本系列文章就逐个来实现。

颜色空间

基础知识

OpenCV 中常用的颜色空间有以下几种:
RGB:以 R(Red:红)、G(Green:绿)、B(Blue:蓝)三种基本色为基础,依据人眼识别的颜色,进行不同程度的叠加,俗称三基色模式。
YUV:Y 表示亮度,U、V 表示色度。YUV 经过缩放和偏移,产生 YCbCr。Cb 表示蓝色分量,Cr 表示红色分量。YUV 具有亮度信息和色彩信息分离的特点。做图像识别时,当图像受自然光影响较大时,可以考虑选择 YUV 颜色空间。
HSV/HSB:HSV 即色相(Hue)、饱和度(Saturation)、明度(Value,或 Brightness)。色相是色彩的基本属性,就是平常说的颜色的名称,如红色、黄色等。饱和度是指色彩的纯度,越高色彩越纯,低则逐渐变灰,取 0-100%的数值。明度,字面意思,明亮的程度,即颜色深浅。

OpenCV实现

在OpenCV中,可通过cvtColor进行颜色空间转换。cvtColor函数是 OpenCV 库中的一个图像转换函数,用于将图像从一个颜色空间转换到另一个颜色空间。
它的函数原型如下:

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

参数说明:
src:输入图像,它可以是 Mat 类型或 vector 类型。
dst:输出图像,与输入图像具有相同的大小和深度。
code:转换代码,表示要进行的颜色空间转换。可以是以下值之一:
COLOR_BGR2GRAY:将图像从BGR 颜色空间转换为灰度空间。
COLOR_BGR2RGB:将图像从 BGR 颜色空间转换为 RGB 颜色空间。
COLOR_GRAY2BGR:将图像从灰度空间转换为 BGR 颜色空间。
COLOR_GRAY2RGB:将图像从灰度空间转换为 RGB 颜色空间。
COLOR_BGRA2BGR:将图像从 BGRA 颜色空间转换为 BGR 颜色空间。
COLOR_RGBA2RGB:将图像从 RGBA 颜色空间转换为 RGB 颜色空间。
COLOR_BGRA2RGBA:将图像从 BGRA 颜色空间转换为 RGBA 颜色空间。
COLOR_RGBA2BGRA:将图像从 RGBA 颜色空间转换为 BGRA 颜色空间。
dstCn:目标图像的通道数。如果为 0,则根据转换代码自动确定。
该函数将输入图像转换为指定的颜色空间,并将结果存储在输出图像中。注意,在进行颜色空间转换时,可能会进行数据类型转换和像素值范围的调整。
但实际上,支持的转换代码比较多。imgproc.hpp中声明为:`/** the color conversion codes
@see @ref imgproc_color_conversions
@ingroup imgproc_color_conversions
*/
enum ColorConversionCodes {
COLOR_BGR2BGRA = 0, //!< add alpha channel to RGB or BGR image
COLOR_RGB2RGBA = COLOR_BGR2BGRA,

COLOR_BGRA2BGR     = 1, //!< remove alpha channel from RGB or BGR image
COLOR_RGBA2RGB     = COLOR_BGRA2BGR,COLOR_BGR2RGBA     = 2, //!< convert between RGB and BGR color spaces (with or without alpha channel)
COLOR_RGB2BGRA     = COLOR_BGR2RGBA,COLOR_RGBA2BGR     = 3,
COLOR_BGRA2RGB     = COLOR_RGBA2BGR,COLOR_BGR2RGB      = 4,
COLOR_RGB2BGR      = COLOR_BGR2RGB,COLOR_BGRA2RGBA    = 5,
COLOR_RGBA2BGRA    = COLOR_BGRA2RGBA,COLOR_BGR2GRAY     = 6, //!< convert between RGB/BGR and grayscale, @ref color_convert_rgb_gray "color conversions"
COLOR_RGB2GRAY     = 7,
COLOR_GRAY2BGR     = 8,
COLOR_GRAY2RGB     = COLOR_GRAY2BGR,
COLOR_GRAY2BGRA    = 9,
COLOR_GRAY2RGBA    = COLOR_GRAY2BGRA,
COLOR_BGRA2GRAY    = 10,
COLOR_RGBA2GRAY    = 11,COLOR_BGR2BGR565   = 12, //!< convert between RGB/BGR and BGR565 (16-bit images)
COLOR_RGB2BGR565   = 13,
COLOR_BGR5652BGR   = 14,
COLOR_BGR5652RGB   = 15,
COLOR_BGRA2BGR565  = 16,
COLOR_RGBA2BGR565  = 17,
COLOR_BGR5652BGRA  = 18,
COLOR_BGR5652RGBA  = 19,COLOR_GRAY2BGR565  = 20, //!< convert between grayscale to BGR565 (16-bit images)
COLOR_BGR5652GRAY  = 21,COLOR_BGR2BGR555   = 22,  //!< convert between RGB/BGR and BGR555 (16-bit images)
COLOR_RGB2BGR555   = 23,
COLOR_BGR5552BGR   = 24,
COLOR_BGR5552RGB   = 25,
COLOR_BGRA2BGR555  = 26,
COLOR_RGBA2BGR555  = 27,
COLOR_BGR5552BGRA  = 28,
COLOR_BGR5552RGBA  = 29,COLOR_GRAY2BGR555  = 30, //!< convert between grayscale and BGR555 (16-bit images)
COLOR_BGR5552GRAY  = 31,COLOR_BGR2XYZ      = 32, //!< convert RGB/BGR to CIE XYZ, @ref color_convert_rgb_xyz "color conversions"
COLOR_RGB2XYZ      = 33,
COLOR_XYZ2BGR      = 34,
COLOR_XYZ2RGB      = 35,COLOR_BGR2YCrCb    = 36, //!< convert RGB/BGR to luma-chroma (aka YCC), @ref color_convert_rgb_ycrcb "color conversions"
COLOR_RGB2YCrCb    = 37,
COLOR_YCrCb2BGR    = 38,
COLOR_YCrCb2RGB    = 39,COLOR_BGR2HSV      = 40, //!< convert RGB/BGR to HSV (hue saturation value) with H range 0..180 if 8 bit image, @ref color_convert_rgb_hsv "color conversions"
COLOR_RGB2HSV      = 41,COLOR_BGR2Lab      = 44, //!< convert RGB/BGR to CIE Lab, @ref color_convert_rgb_lab "color conversions"
COLOR_RGB2Lab      = 45,COLOR_BGR2Luv      = 50, //!< convert RGB/BGR to CIE Luv, @ref color_convert_rgb_luv "color conversions"
COLOR_RGB2Luv      = 51,
COLOR_BGR2HLS      = 52, //!< convert RGB/BGR to HLS (hue lightness saturation) with H range 0..180 if 8 bit image, @ref color_convert_rgb_hls "color conversions"
COLOR_RGB2HLS      = 53,COLOR_HSV2BGR      = 54, //!< backward conversions HSV to RGB/BGR with H range 0..180 if 8 bit image
COLOR_HSV2RGB      = 55,COLOR_Lab2BGR      = 56,
COLOR_Lab2RGB      = 57,
COLOR_Luv2BGR      = 58,
COLOR_Luv2RGB      = 59,
COLOR_HLS2BGR      = 60, //!< backward conversions HLS to RGB/BGR with H range 0..180 if 8 bit image
COLOR_HLS2RGB      = 61,COLOR_BGR2HSV_FULL = 66, //!< convert RGB/BGR to HSV (hue saturation value) with H range 0..255 if 8 bit image, @ref color_convert_rgb_hsv "color conversions"
COLOR_RGB2HSV_FULL = 67,
COLOR_BGR2HLS_FULL = 68, //!< convert RGB/BGR to HLS (hue lightness saturation) with H range 0..255 if 8 bit image, @ref color_convert_rgb_hls "color conversions"
COLOR_RGB2HLS_FULL = 69,COLOR_HSV2BGR_FULL = 70, //!< backward conversions HSV to RGB/BGR with H range 0..255 if 8 bit image
COLOR_HSV2RGB_FULL = 71,
COLOR_HLS2BGR_FULL = 72, //!< backward conversions HLS to RGB/BGR with H range 0..255 if 8 bit image
COLOR_HLS2RGB_FULL = 73,COLOR_LBGR2Lab     = 74,
COLOR_LRGB2Lab     = 75,
COLOR_LBGR2Luv     = 76,
COLOR_LRGB2Luv     = 77,COLOR_Lab2LBGR     = 78,
COLOR_Lab2LRGB     = 79,
COLOR_Luv2LBGR     = 80,
COLOR_Luv2LRGB     = 81,COLOR_BGR2YUV      = 82, //!< convert between RGB/BGR and YUV
COLOR_RGB2YUV      = 83,
COLOR_YUV2BGR      = 84,
COLOR_YUV2RGB      = 85,//! YUV 4:2:0 family to RGB
COLOR_YUV2RGB_NV12  = 90,
COLOR_YUV2BGR_NV12  = 91,
COLOR_YUV2RGB_NV21  = 92,
COLOR_YUV2BGR_NV21  = 93,
COLOR_YUV420sp2RGB  = COLOR_YUV2RGB_NV21,
COLOR_YUV420sp2BGR  = COLOR_YUV2BGR_NV21,COLOR_YUV2RGBA_NV12 = 94,
COLOR_YUV2BGRA_NV12 = 95,
COLOR_YUV2RGBA_NV21 = 96,
COLOR_YUV2BGRA_NV21 = 97,
COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,COLOR_YUV2RGB_YV12  = 98,
COLOR_YUV2BGR_YV12  = 99,
COLOR_YUV2RGB_IYUV  = 100,
COLOR_YUV2BGR_IYUV  = 101,
COLOR_YUV2RGB_I420  = COLOR_YUV2RGB_IYUV,
COLOR_YUV2BGR_I420  = COLOR_YUV2BGR_IYUV,
COLOR_YUV420p2RGB   = COLOR_YUV2RGB_YV12,
COLOR_YUV420p2BGR   = COLOR_YUV2BGR_YV12,COLOR_YUV2RGBA_YV12 = 102,
COLOR_YUV2BGRA_YV12 = 103,
COLOR_YUV2RGBA_IYUV = 104,
COLOR_YUV2BGRA_IYUV = 105,
COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
COLOR_YUV420p2RGBA  = COLOR_YUV2RGBA_YV12,
COLOR_YUV420p2BGRA  = COLOR_YUV2BGRA_YV12,COLOR_YUV2GRAY_420  = 106,
COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
COLOR_YUV420p2GRAY  = COLOR_YUV2GRAY_420,//! YUV 4:2:2 family to RGB
COLOR_YUV2RGB_UYVY = 107,
COLOR_YUV2BGR_UYVY = 108,
//COLOR_YUV2RGB_VYUY = 109,
//COLOR_YUV2BGR_VYUY = 110,
COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,COLOR_YUV2RGBA_UYVY = 111,
COLOR_YUV2BGRA_UYVY = 112,
//COLOR_YUV2RGBA_VYUY = 113,
//COLOR_YUV2BGRA_VYUY = 114,
COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,COLOR_YUV2RGB_YUY2 = 115,
COLOR_YUV2BGR_YUY2 = 116,
COLOR_YUV2RGB_YVYU = 117,
COLOR_YUV2BGR_YVYU = 118,
COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,COLOR_YUV2RGBA_YUY2 = 119,
COLOR_YUV2BGRA_YUY2 = 120,
COLOR_YUV2RGBA_YVYU = 121,
COLOR_YUV2BGRA_YVYU = 122,
COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,COLOR_YUV2GRAY_UYVY = 123,
COLOR_YUV2GRAY_YUY2 = 124,
//CV_YUV2GRAY_VYUY    = CV_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,//! alpha premultiplication
COLOR_RGBA2mRGBA    = 125,
COLOR_mRGBA2RGBA    = 126,//! RGB to YUV 4:2:0 family
COLOR_RGB2YUV_I420  = 127,
COLOR_BGR2YUV_I420  = 128,
COLOR_RGB2YUV_IYUV  = COLOR_RGB2YUV_I420,
COLOR_BGR2YUV_IYUV  = COLOR_BGR2YUV_I420,COLOR_RGBA2YUV_I420 = 129,
COLOR_BGRA2YUV_I420 = 130,
COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
COLOR_RGB2YUV_YV12  = 131,
COLOR_BGR2YUV_YV12  = 132,
COLOR_RGBA2YUV_YV12 = 133,
COLOR_BGRA2YUV_YV12 = 134,//! Demosaicing, see @ref color_convert_bayer "color conversions" for additional information
COLOR_BayerBG2BGR = 46, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2BGR = 47, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2BGR = 48, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2BGR = 49, //!< equivalent to GBRG Bayer patternCOLOR_BayerRGGB2BGR = COLOR_BayerBG2BGR,
COLOR_BayerGRBG2BGR = COLOR_BayerGB2BGR,
COLOR_BayerBGGR2BGR = COLOR_BayerRG2BGR,
COLOR_BayerGBRG2BGR = COLOR_BayerGR2BGR,COLOR_BayerRGGB2RGB = COLOR_BayerBGGR2BGR,
COLOR_BayerGRBG2RGB = COLOR_BayerGBRG2BGR,
COLOR_BayerBGGR2RGB = COLOR_BayerRGGB2BGR,
COLOR_BayerGBRG2RGB = COLOR_BayerGRBG2BGR,COLOR_BayerBG2RGB = COLOR_BayerRG2BGR, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2RGB = COLOR_BayerGR2BGR, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2RGB = COLOR_BayerBG2BGR, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2RGB = COLOR_BayerGB2BGR, //!< equivalent to GBRG Bayer patternCOLOR_BayerBG2GRAY = 86, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2GRAY = 87, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2GRAY = 88, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2GRAY = 89, //!< equivalent to GBRG Bayer patternCOLOR_BayerRGGB2GRAY = COLOR_BayerBG2GRAY,
COLOR_BayerGRBG2GRAY = COLOR_BayerGB2GRAY,
COLOR_BayerBGGR2GRAY = COLOR_BayerRG2GRAY,
COLOR_BayerGBRG2GRAY = COLOR_BayerGR2GRAY,//! Demosaicing using Variable Number of Gradients
COLOR_BayerBG2BGR_VNG = 62, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2BGR_VNG = 63, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2BGR_VNG = 64, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2BGR_VNG = 65, //!< equivalent to GBRG Bayer patternCOLOR_BayerRGGB2BGR_VNG = COLOR_BayerBG2BGR_VNG,
COLOR_BayerGRBG2BGR_VNG = COLOR_BayerGB2BGR_VNG,
COLOR_BayerBGGR2BGR_VNG = COLOR_BayerRG2BGR_VNG,
COLOR_BayerGBRG2BGR_VNG = COLOR_BayerGR2BGR_VNG,COLOR_BayerRGGB2RGB_VNG = COLOR_BayerBGGR2BGR_VNG,
COLOR_BayerGRBG2RGB_VNG = COLOR_BayerGBRG2BGR_VNG,
COLOR_BayerBGGR2RGB_VNG = COLOR_BayerRGGB2BGR_VNG,
COLOR_BayerGBRG2RGB_VNG = COLOR_BayerGRBG2BGR_VNG,COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG, //!< equivalent to GBRG Bayer pattern//! Edge-Aware Demosaicing
COLOR_BayerBG2BGR_EA  = 135, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2BGR_EA  = 136, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2BGR_EA  = 137, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2BGR_EA  = 138, //!< equivalent to GBRG Bayer patternCOLOR_BayerRGGB2BGR_EA  = COLOR_BayerBG2BGR_EA,
COLOR_BayerGRBG2BGR_EA  = COLOR_BayerGB2BGR_EA,
COLOR_BayerBGGR2BGR_EA  = COLOR_BayerRG2BGR_EA,
COLOR_BayerGBRG2BGR_EA  = COLOR_BayerGR2BGR_EA,COLOR_BayerRGGB2RGB_EA  = COLOR_BayerBGGR2BGR_EA,
COLOR_BayerGRBG2RGB_EA  = COLOR_BayerGBRG2BGR_EA,
COLOR_BayerBGGR2RGB_EA  = COLOR_BayerRGGB2BGR_EA,
COLOR_BayerGBRG2RGB_EA  = COLOR_BayerGRBG2BGR_EA,COLOR_BayerBG2RGB_EA  = COLOR_BayerRG2BGR_EA, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2RGB_EA  = COLOR_BayerGR2BGR_EA, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2RGB_EA  = COLOR_BayerBG2BGR_EA, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2RGB_EA  = COLOR_BayerGB2BGR_EA, //!< equivalent to GBRG Bayer pattern//! Demosaicing with alpha channel
COLOR_BayerBG2BGRA = 139, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2BGRA = 140, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2BGRA = 141, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2BGRA = 142, //!< equivalent to GBRG Bayer patternCOLOR_BayerRGGB2BGRA = COLOR_BayerBG2BGRA,
COLOR_BayerGRBG2BGRA = COLOR_BayerGB2BGRA,
COLOR_BayerBGGR2BGRA = COLOR_BayerRG2BGRA,
COLOR_BayerGBRG2BGRA = COLOR_BayerGR2BGRA,COLOR_BayerRGGB2RGBA = COLOR_BayerBGGR2BGRA,
COLOR_BayerGRBG2RGBA = COLOR_BayerGBRG2BGRA,
COLOR_BayerBGGR2RGBA = COLOR_BayerRGGB2BGRA,
COLOR_BayerGBRG2RGBA = COLOR_BayerGRBG2BGRA,COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA, //!< equivalent to RGGB Bayer pattern
COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA, //!< equivalent to GRBG Bayer pattern
COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA, //!< equivalent to BGGR Bayer pattern
COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA, //!< equivalent to GBRG Bayer patternCOLOR_COLORCVT_MAX  = 143

};
`

调试效果

本项功能调试,本质就是围绕cvtColor的参数进行。
可分解为【原图格式】与【目标格式】。当然,如果所有转换代码均支持的话,格式比较多。故再增加一个【常用格式】。
在这里插入图片描述
为取得所有的格式,直接把ColorConversionCodes定义内容拷贝成文件,写一下函数解析:

	UnicodeString fileName = L"D:\\t.cpp";TStringList * list = new TStringList;list->LoadFromFile(fileName);UnicodeString pattern = L"(?<=COLOR_).*?(?=[\\s,])";TStringList * froms = new TStringList;TStringList * tos = new TStringList;for(int i = 0; i < list->Count; ++i) {UnicodeString str = list->Strings[i].Trim();if(str.Pos("COLOR_") != TOption::StringPos_NotFound) {int num = THelper::String::GetRegMatchNumber(str, pattern);for(int j = 0; j < num; ++j) {UnicodeString convertString = THelper::String::GetRegMatchAt(str, pattern, j);if(convertString.Pos("2") != TOption::StringPos_NotFound) {UnicodeString from, to;if(THelper::String::GetRegMatchNumber(convertString, "2") == 1) {from = THelper::String::GetStringAt(convertString, L"2", 0);to = THelper::String::GetStringAt(convertString, L"2", 1);} else {UnicodeString spliter = THelper::String::GetRegMatchAt(convertString, L"2[A-Z]", 0);if(spliter.Length() == 0)continue;int pos = convertString.Pos(spliter);from = convertString.SubString(1, pos - 1);to = convertString.SubString(pos + 1, convertString.Length());}if(froms->IndexOf(from) == -1)froms->Add(from);if(tos->IndexOf(to) == -1)tos->Add(to);}}}}froms->Sort();tos->Sort();THelper::Logi(TTypeConvert::StringList2String(froms, L";"));THelper::Logi(TTypeConvert::StringList2String(tos, L";"));delete froms;delete tos;delete list;

取得所有的格式代码

048. 14:12:01:147 > 【主线程】 > BayerBG;BayerBGGR;BayerGB;BayerGBRG;BayerGR;BayerGRBG;BayerRG;BayerRGGB;BGR;BGR555;BGR565;BGRA;GRAY;HLS;HSV;Lab;LBGR;LRGB;Luv;mRGBA;RGB;RGBA;XYZ;YCrCb;YUV;YUV420p;YUV420sp
049. 14:12:01:204 > 【主线程】 > BGR;BGR_EA;BGR_FULL;BGR_I420;BGR_IYUV;BGR_NV12;BGR_NV21;BGR_UYNV;BGR_UYVY;BGR_VNG;BGR_VYUY;BGR_Y422;BGR_YUNV;BGR_YUY2;BGR_YUYV;BGR_YV12;BGR_YVYU;BGR555;BGR565;BGRA;BGRA_I420;BGRA_IYUV;BGRA_NV12;BGRA_NV21;BGRA_UYNV;BGRA_UYVY;BGRA_VYUY;BGRA_Y422;BGRA_YUNV;BGRA_YUY2;BGRA_YUYV;BGRA_YV12;BGRA_YVYU;GRAY;GRAY_420;GRAY_I420;GRAY_IYUV;GRAY_NV12;GRAY_NV21;GRAY_UYNV;GRAY_UYVY;GRAY_Y422;GRAY_YUNV;GRAY_YUY2;GRAY_YUYV;GRAY_YV12;GRAY_YVYU;HLS;HLS_FULL;HSV;HSV_FULL;Lab;LBGR;LRGB;Luv;mRGBA;RGB;RGB_EA;RGB_FULL;RGB_I420;RGB_IYUV;RGB_NV12;RGB_NV21;RGB_UYNV;RGB_UYVY;RGB_VNG;RGB_VYUY;RGB_Y422;RGB_YUNV;RGB_YUY2;RGB_YUYV;RGB_YV12;RGB_YVYU;RGBA;RGBA_I420;RGBA_IYUV;RGBA_NV12;RGBA_NV21;RGBA_UYNV;RGBA_UYVY;RGBA_VYUY;RGBA_Y422;RGBA_YUNV;RGBA_YUY2;RGBA_YUYV;RGBA_YV12;RGBA_YVYU;XYZ;YCrCb;YUV;YUV_I420;YUV_IYUV;YUV_YV12

剩下简单处理一下即可

// 「基本 - 色彩空间」类 - 用户设置「常用格式」后,重新更新配置界面 - 「原图格式」与「目标格式」内容相应匹配
void TFilter_Base_ColorSpace::RefreshUI() {int commonFlag = GetParamValue(0);QString fromColorSpaces = "BayerBG;BayerBGGR;BayerGB;BayerGBRG;BayerGR;BayerGRBG;BayerRG;BayerRGGB;BGR;BGR555;BGR565;BGRA;GRAY;HLS;HSV;Lab;LBGR;LRGB;Luv;mRGBA;RGB;RGBA;XYZ;YCrCb;YUV;YUV420p;YUV420sp";QString toColorSpaces = "BGR;BGR_EA;BGR_FULL;BGR_I420;BGR_IYUV;BGR_NV12;BGR_NV21;BGR_UYNV;BGR_UYVY;BGR_VNG;BGR_VYUY;BGR_Y422;BGR_YUNV;BGR_YUY2;BGR_YUYV;BGR_YV12;BGR_YVYU;BGR555;BGR565;BGRA;BGRA_I420;BGRA_IYUV;BGRA_NV12;BGRA_NV21;BGRA_UYNV;BGRA_UYVY;BGRA_VYUY;BGRA_Y422;BGRA_YUNV;BGRA_YUY2;BGRA_YUYV;BGRA_YV12;BGRA_YVYU;GRAY;GRAY_420;GRAY_I420;GRAY_IYUV;GRAY_NV12;GRAY_NV21;GRAY_UYNV;GRAY_UYVY;GRAY_Y422;GRAY_YUNV;GRAY_YUY2;GRAY_YUYV;GRAY_YV12;GRAY_YVYU;HLS;HLS_FULL;HSV;HSV_FULL;Lab;LBGR;LRGB;Luv;mRGBA;RGB;RGB_EA;RGB_FULL;RGB_I420;RGB_IYUV;RGB_NV12;RGB_NV21;RGB_UYNV;RGB_UYVY;RGB_VNG;RGB_VYUY;RGB_Y422;RGB_YUNV;RGB_YUY2;RGB_YUYV;RGB_YV12;RGB_YVYU;RGBA;RGBA_I420;RGBA_IYUV;RGBA_NV12;RGBA_NV21;RGBA_UYNV;RGBA_UYVY;RGBA_VYUY;RGBA_Y422;RGBA_YUNV;RGBA_YUY2;RGBA_YUYV;RGBA_YV12;RGBA_YVYU;XYZ;YCrCb;YUV;YUV_I420;YUV_IYUV;YUV_YV12";if(commonFlag) {fromColorSpaces = "BGR;BGRA;GRAY;HLS;HSV;Lab;LBGR;LRGB;Luv;RGB;RGBA;XYZ;YCrCb;YUV";toColorSpaces = fromColorSpaces;}// 「原图格式」内容相应匹配this->GetParam(1)->optionStrings = fromColorSpaces;if(Q_DYNA_CAST(ComboBox, GetParam(1)->uiControl)) {QString selected = ComboBox->currentText();ComboBox->clear();QStringList values = GetParam(1)->optionStrings.split(";");int index = -1;ITER(values) {ComboBox->addItem(*it);if(*it == selected)index = it - values.begin();}ComboBox->setCurrentIndex(index);}// 「目标格式」内容相应匹配this->GetParam(2)->optionStrings = toColorSpaces;if(Q_DYNA_CAST(ComboBox, GetParam(2)->uiControl)) {QString selected = ComboBox->currentText();ComboBox->clear();QStringList values = GetParam(2)->optionStrings.split(";");int index = -1;ITER(values) {ComboBox->addItem(*it);if(*it == selected)index = it - values.begin();}ComboBox->setCurrentIndex(index);}
}
// 「基本 - 色彩空间」类 - 滤镜处理 - 对dstMat进行本滤镜逻辑变换处理
void TFilter_Base_ColorSpace::FilterProcess(cv::Mat& dstMat)
{ // [基本 - 色彩空间] 类 - 滤镜处理int paramIndex = 0;int isCommon = int(GetParamValue(paramIndex++));            // 0: 常用格式QString from = THelper::String::GetStringAt(GetParam(1)->optionStrings, ";", int(GetParamValue(paramIndex++)));    // 1: 原图格式QString to = THelper::String::GetStringAt(GetParam(2)->optionStrings, ";", int(GetParamValue(paramIndex++)));    // 2: 目标格式if(from == to)return;if(from == "GRAY")dstMat = CvHelper::ChangeMatDim(dstMat, 1);else if(from == "RGB" || from == "BGR")dstMat = CvHelper::ChangeMatDim(dstMat, 3);else if(from == "RGBA" || from == "BGRA")dstMat = CvHelper::ChangeMatDim(dstMat, 4);QString convert = FORMAT("COLOR_%s2%s", Q(from), Q(to));TEnums_Filter::TColorConversionCodes cvt = StringToEnum<TEnums_Filter::TColorConversionCodes>(convert);if(IS_IN_RANGE(cvt, TEnums_Filter::COLOR_BGR2BGRA, TEnums_Filter::COLOR_COLORCVT_MAX)) {THelper::Logi("颜色空间转换 > %s", Q(convert));cvtColor(dstMat, dstMat, ColorConversionCodes(int(cvt)));} elseTHelper::Loge("不支持目标转换 %s", Q(convert));
}

最后运行调试效果:

OpenCV 4 功能 - 颜色空间

不知道这算不算是OpenCV的小BUG:颜色空间转换 RGB2RGBA/GBR2GBRA,结果居然出现颜色变化(BR通道交换),但RGB2BGRA/BGR2RGBA不出现变化。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/227463.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javaweb基础2.0 (持续更新中)

Day 10 : Responbody 将响应的实体类转为json发送给前端。RequestBody将request的json转为实体类给后端 修改未成功的可能是因为根据id 查询的时候&#xff0c;只查询了name字段&#xff0c;因为后面需要根据id 修改&#xff0c;所以还需查询id &#xff0c;不然前端不知道id也…

交互式笔记Jupyter Notebook本地部署并实现公网远程访问内网服务器

最近&#xff0c;我发现了一个超级强大的人工智能学习网站。它以通俗易懂的方式呈现复杂的概念&#xff0c;而且内容风趣幽默。我觉得它对大家可能会有所帮助&#xff0c;所以我在此分享。点击这里跳转到网站。 文章目录 1.前言2.Jupyter Notebook的安装2.1 Jupyter Notebook下…

记一次JSF异步调用引起的接口可用率降低 | 京东云技术团队

前言 本文记录了由于JSF异步调用超时引起的接口可用率降低问题的排查过程&#xff0c;主要介绍了排查思路和JSF异步调用的流程&#xff0c;希望可以帮助大家了解JSF的异步调用原理以及提供一些问题排查思路。本文分析的JSF源码是基于JSF 1,7.5-HOTFIX-T6版本。 起因 问题背景…

EBU7140 Security and Authentication(一)常见加密算法

前言 主要根据 EBU7140 课程内容整理&#xff0c;比较偏向应试~ Block1&#xff1a;介绍课程&#xff0c;传统加密方式。 Block2&#xff1a;公钥加密的原理和应用。 Block3&#xff1a;一些特定安全协议技术&#xff08;如防火墙 Kerberos身份验证协议等&#xff09;。 B…

AppWeb认证绕过漏洞(CVE-2018-8715)

一、环境搭建 二、影响版本 三、构造payload Authorization: Digest usernameadmin 四、抓包获取sesion 五、修改数据包、认证头 记得设置用户名 六、漏洞存在特征&#xff08;Gigest&#xff09;

日志框架简介-Slf4j+Logback入门实践 | 京东云技术团队

前言 随着互联网和大数据的迅猛发展&#xff0c;分布式日志系统和日志分析系统已广泛应用&#xff0c;几乎所有应用程序都使用各种日志框架记录程序运行信息。因此&#xff0c;作为工程师&#xff0c;了解主流的日志记录框架非常重要。虽然应用程序的运行结果不受日志的有无影…

【Matlab】BP 神经网络时序预测算法

资源下载&#xff1a; https://download.csdn.net/download/vvoennvv/88681507 一&#xff0c;概述 BP 神经网络是一种常见的人工神经网络&#xff0c;也是一种有监督学习的神经网络。其全称为“Back Propagation”&#xff0c;即反向传播算法。BP 神经网络主要由输入层、隐藏层…

如何恢复 iPhone 上永久删除的照片?

2007年&#xff0c;苹果公司推出了一款惊天动地的智能手机&#xff0c;也就是后来的iPhone。你会惊讶地发现&#xff0c;迄今为止&#xff0c;苹果公司已经售出了 7 亿部 iPhone 设备。根据最新一项调查数据&#xff0c;智能手机利润的 95% 都进了苹果公司的腰包。 如此受欢迎…

Android实验:contentprovider 实验+SQLite 数据库的实现

目录 SQLite实验目的实验内容实验要求项目结构代码实现结果展示 SQLite SQLite 是一个开源的嵌入式关系数据库&#xff0c;实现了自给自足的、无服务器的、配置无需的、事务性的 SQL 数据库引擎。它是一个零配置的数据库&#xff0c;这意味着与其他数据库系统不同&#xff0c;…

安全生产知识竞赛活动方案

为进一步普及安全生产法律法规知识&#xff0c;增强安全意识&#xff0c;提高安全技能&#xff0c;经研究&#xff0c;决定举办以“加强安全法治、保障安全生产”为主题的新修订《安全生产法》知识竞赛活动&#xff0c;现将有关事项通知如下&#xff1a; 一、活动时间&#xf…

【教学类-43-03】20231229 N宫格数独3.0(n=1、2、3、4、6、8、9) (ChatGPT AI对话大师生成 回溯算法)

作品展示&#xff1a; 背景需求&#xff1a; 大4班20号说&#xff1a;我不会做这种&#xff08;九宫格&#xff09;&#xff0c;我做的是小格子的&#xff0c; 他把手工纸翻过来&#xff0c;在反面自己画了矩阵格子。向我展示&#xff1a;“我会做这种&#xff01;” 原来他会…

分类预测 | Matlab实现SCSO-SVM基于沙猫群优化算法优化支持向量机的多变量分类预测【23年新算法】

分类预测 | Matlab实现SCSO-SVM基于沙猫群优化算法优化支持向量机的多变量分类预测【23年新算法】 目录 分类预测 | Matlab实现SCSO-SVM基于沙猫群优化算法优化支持向量机的多变量分类预测【23年新算法】分类效果基本描述程序设计参考资料 分类效果 基本描述 1.Matlab实现SCSO-…

2023十大编程语言及未来展望

2023十大编程语言及未来展望 1. 2023年十大编程语言排行榜2. 十大编程语言未来展望PythonCCJavaC#JavaScriptPHPVisual BasicSQLAssembly language 1. 2023年十大编程语言排行榜 TIOBE排行榜是根据互联网上有经验的程序员、课程和第三方厂商的数量&#xff0c;并使用搜索引擎&a…

Peter算法小课堂—浮点数危机

大家先想想下面这个代码运行结果&#xff1a; #include <bits/stdc.h> using namespace std; int main(){double x5.2;double y4.11.1;cout<<(x<y)<<endl;cout<<x-y<<endl;return 0; } 最终发现&#xff0c; &#xff1f;&#xff1f;&…

css 用多个阴影做出光斑投影的效果 box-shadow

css 用多个阴影做出光斑投影的效果 box-shadow 你首先需要知道的一点是 box-shadow 可以接收多个值&#xff0c;也就是可以设置多个阴影&#xff0c;这样就可以做一个类似光斑投影的效果。 一、效果 二、代码 里面用到了我一些 scss 工具方法&#xff0c;不过不影响&#xf…

【音视频 ffmpeg 学习】 RTMP推流 mp4文件

1.RTMP(实时消息传输协议)是Adobe 公司开发的一个基于TCP的应用层协议。 2.RTMP协议中基本的数据单元称为消息&#xff08;Message&#xff09;。 3.当RTMP协议在互联网中传输数据的时候&#xff0c;消息会被拆分成更小的单元&#xff0c;称为消息块&#xff08;Chunk&#xff…

二叉树的中序遍历,力扣

目录 题目地址&#xff1a; 题目&#xff1a; 解题方法&#xff1a; 解题分析&#xff1a; 解题思路&#xff1a; 代码实现&#xff1a; 注&#xff1a; 代码实现&#xff08;递归&#xff09;&#xff1a; 代码实现&#xff08;迭代&#xff09;&#xff1a; 题目地址&#xf…

【目标检测】yolov8结构及代码分析

yolov8代码:https://github.com/ultralytics/ultralytics yolov8的整体结构如下图&#xff08;来自mmyolo&#xff09;&#xff1a; yolov8的配置文件&#xff1a; # Ultralytics YOLO &#x1f680;, AGPL-3.0 license # YOLOv8 object detection model with P3-P5 outputs.…

跨境电商迎来综合竞争力比拼时代 五大趋势解读跨境2024

过去几年&#xff0c;跨境电商成为外贸出口增长的一大亮点&#xff0c;随着年底国务院办公厅《关于加快内外贸一体化发展的若干措施》的发布&#xff0c;跨境电商在促进经济发展、助力内外贸一体化发展方面的价值更加凸显。 这是跨境电商变化最快的时代&#xff0c;也是跨境电…

GrayLog日志平台的基本使用-ssh之Email报警

1、首先编辑并添加邮件配置到server.conf&#xff08;注意&#xff1a;是添加&#xff09; vim /etc/graylog/server/server.conf # Email transport transport_email_enabled true transport_email_hostname smtp.qq.com transport_email_port 465 transport_email_use_a…