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.mzph.cn/news/586808.shtml

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

相关文章

据报道,微软的下一代 Surface 笔记本电脑将是其首款真正的“人工智能 PC”

明年&#xff0c;微软计划推出 Surface Laptop 6和 Surface Pro 10&#xff0c;这两款设备将提供 Arm 和 Intel 两种处理器选项。不愿意透露姓名的不透露姓名人士透露&#xff0c;这些新设备将引入先进的人工智能功能&#xff0c;包括配备下一代神经处理单元 (NPU)。据悉&#…

MCS-51单片机的中断源

目录 MCS-51中断源&#xff1a; 中断控制&#xff1a; 1.定时控制寄存器&#xff08;TCON&#xff09; 2.串行口控制寄存器&#xff08;SCON&#xff09; 3.中断允许寄存器(IE) 4.中断优先级控制寄存器(IP) 中断处理: 中断采样&#xff1a; 中断查询&#xff1a; 中断…

.FileZilla的使用和主动模式被动模式介绍

FileZilla的使用和主动模式被动模式介绍 1.FileZilla的使用和主动模式被动模式介绍1.安装下载2.新建组和用户2.1打开后出现如下界面2.2点击编辑打开组这个选项2.3点击添加组以后&#xff0c;点击确认2.4输入组的名称&#xff0c;列如我输入的niyin2.5点击用户选项2.6像上面一样…

【Vue2+3入门到实战】(15)VUE路由入门声明式导航的基本使用与详细代码示例

目录 一、声明式导航-导航链接1.需求2.解决方案3.通过router-link自带的两个样式进行高亮4.总结 二、声明式导航-两个类名1.router-link-active2.router-link-exact-active3.在地址栏中输入二级路由查看类名的添加4.总结 三、声明式导航-自定义类名&#xff08;了解&#xff09…

中间人攻击是什么,会产生哪些危害,如何有效防止中间人攻击

简介 中间人攻击&#xff08;Man-in-the-Middle Attack&#xff0c;简称MITM攻击&#xff09;是一种网络攻击&#xff0c;其原理是攻击者通过各种技术手段将受攻击者控制的一台计算机虚拟放置在网络连接中的两台通信计算机之间&#xff0c;这台计算机称为“中间人”。在攻击过…

【设计模式】状态模式

文章目录 引例状态模式理论状态模式代码优化结合享元模式并发问题解决 策略模式 VS 状态模式 引例 交通信号灯系统的设计与实现 方案一 传统设计方案 定义交通灯颜色的枚举 public enum LightColor { Green,Red,Yellow }交通灯类TrafficLight&#xff0c;处理颜色转换等业务…

安全配置审计概念、应用场景、常用基线及扫描工具

软件安装完成后都会有默认的配置&#xff0c;但默认配置仅保证了服务正常运行&#xff0c;却很少考虑到安全防护问题&#xff0c;攻击者往往利用这些默认配置产生的脆弱点发起攻击。虽然安全人员已经意识到正确配置软件的重要性&#xff0c;但面对复杂的业务系统和网络结构、网…

【数学建模美赛M奖速成系列】Matplotlib绘图技巧(一)

Matplotlib图像基础 写在前面1 基本绘图实例&#xff1a;sin、cos函数图2 plot()函数详解**kwargs参数&#xff1a; 3 matplotlib中绘图的默认配置4 设置图的横纵坐标的上下界5 设置横纵坐标上的记号6 调整图像的脊柱7 添加图例8 给一些特殊点加注释9 子图最后 写在前面 前面我…

CentOS7安装部署Zookeeper

文章目录 CentOS7安装部署Zookeeper一、前言1.简介2.架构3.集群角色4.特点5.环境 二、正文1.部署服务器2.基础环境1&#xff09;主机名2&#xff09;Hosts文件3&#xff09;关闭防火墙4&#xff09;JDK 安装部署 3.单机部署1&#xff09;下载和解压2&#xff09;配置文件3&…

第一讲:BeanFactory和ApplicationContext

BeanFactory和ApplicationContext 什么是BeanFactory 它是ApplicationContext的父接口它才是Spring的核心容器&#xff0c;主要的ApplicationContext实现都组合了它的功能 BeanFactory能做什么? 表面上看BeanFactory的主要方法只有getBean()&#xff0c;实际上控制反转、基…

路由器IP地址及直连实验

实验目的&#xff1a; &#xff08;1&#xff09;理解IP地址&#xff1b; &#xff08;2&#xff09;掌握路由器端口IP地址的配置方法&#xff1b; &#xff08;3&#xff09;理解路由器的直连网络。 实验器材&#xff1a; Cisco packet 实验内容&#xff1a; 实验步骤&a…

【ArcGIS微课1000例】0083:地震灾害图件制作之土壤类型分布图

本文基于1:400万矢量土壤图,制作甘肃积石山6.2级地震100km范围内土壤类型分布图。 文章目录 一、土壤分布图预览二、数据集来源及简介三、土壤分布图制作一、土壤分布图预览 二、数据集来源及简介 1. 数据来源 数据集为1:400万中国土壤图,1:400万中国土壤图(2000)由中国科…

大创项目推荐 深度学习交通车辆流量分析 - 目标检测与跟踪 - python opencv

文章目录 0 前言1 课题背景2 实现效果3 DeepSORT车辆跟踪3.1 Deep SORT多目标跟踪算法3.2 算法流程 4 YOLOV5算法4.1 网络架构图4.2 输入端4.3 基准网络4.4 Neck网络4.5 Head输出层 5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; *…

模式识别与机器学习-集成学习

集成学习 集成学习思想过拟合与欠拟合判断方法 K折交叉验证BootstrapBagging随机森林的特点和工作原理&#xff1a; BoostingAdaBoost工作原理&#xff1a;AdaBoost的特点和优点&#xff1a;AdaBoost的缺点&#xff1a; Gradient Boosting工作原理&#xff1a;Gradient Boostin…

android 13.0 Launcher3长按app弹窗设置为圆角背景功能实现一

1.前言 在13.0的系统ROM定制化开发中,在进行一些Launcher3的定制化开发中,在使用app的弹窗的功能时,会弹出应用信息和微件之类的内容,所以在定制需求中,需要默认设置为圆角背景,接下来就来分析下相关功能的实现 如图: 2.Launcher3长按app弹窗设置为圆角背景功能实现的核…

【MYSQL】-函数

&#x1f496;作者&#xff1a;小树苗渴望变成参天大树&#x1f388; &#x1f389;作者宣言&#xff1a;认真写好每一篇博客&#x1f4a4; &#x1f38a;作者gitee:gitee✨ &#x1f49e;作者专栏&#xff1a;C语言,数据结构初阶,Linux,C 动态规划算法&#x1f384; 如 果 你 …

AcWing 1076. 迷宫问题(最短路模型)

题目链接 活动 - AcWing本课程系统讲解常用算法与数据结构的应用方式与技巧。https://www.acwing.com/problem/content/description/1078/ 来源 《信息学奥赛一本通》, kuangbin专题 , POJ3984 代码 #include <cstring> #include <iostream> #include <alg…

【网络安全常用术语解读】SCAP详解

本文主要介绍什么是SCAP&#xff0c;SCAP的产生背景是怎样的&#xff0c;SCAP有什么用途&#xff0c;有哪些组件&#xff0c;各个组件的用途是什么&#xff1f; SCAP产生背景 由于计算机和网络技术的快速发展&#xff0c;越来越多的软件和系统被应用到企业和机构中&#xff0c…

迭代归并:归并排序非递归实现解析

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《数据结构&算法》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! &#x1f4cb; 前言 归并排序的思想上我们已经全部介绍完了&#xff0c;但是同时也面临和快速排序一样的问题那就是递…

通信原理课设(gec6818) 007:语音识别

目录 1、去科大讯飞官网下载对应的sdk 2、科大讯飞文件夹的意思 3、配置ARM的录音环境 4、编程实现语音识别 我们的需求是将一个语音文件从客户端传到服务器&#xff0c;因此我们最好是选用tcp 现在市面上面常用的语音识别解决方案为&#xff1a;科大讯飞c和百度c 离…