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 pattern

COLOR_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 pattern

COLOR_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 pattern

COLOR_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 pattern

COLOR_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 pattern

COLOR_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 pattern

COLOR_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 pattern

COLOR_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)));
    } else
        THelper::Loge("不支持目标转换 %s", Q(convert));
}

最后运行调试效果:

OpenCV 4 功能 - 颜色空间

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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

金蝶云星空业务对象扩展

文章目录 金蝶云星空业务对象扩展 金蝶云星空业务对象扩展 当前对象已经存在扩展不允许再次扩展。 一般来说&#xff0c;不允许同级多次扩展。因为同级扩展会出现界面元素冲突的情况。 但是通过不同应用扩展部署到环境的&#xff0c;允许一个开发商只能扩展一次标准产品。 不过…

页面布局--Flexbox的自动边距

标题页面布局–Flexbox的自动边距 通过简单的margin:auto&#xff0c;我们就能实现元素的多种对齐方式。 假设我们在盒子模型里有四个元素&#xff1a; 先给容器使用flex布局&#xff1a; .container {display: flex;justify-content: flex-start;align-items: center;gap: 6…

{MySQL}索引事务和JDBC

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、索引1.1索引是什么1.2作用1.3代码 二、事务2.1什么是事务2.2使用 三.JDBC总结 前言 接着上次&#xff0c;继续讲下MySQL 提示&#xff1a;以下是本篇文章正…

第六课:冷战和消费主义、个人计算机革命、图形用户界面(GUI)及3D图形

第六课&#xff1a;冷战和消费主义、个人计算机革命、图形用户界面&#xff08;GUI&#xff09;及3D图形 第二十四章&#xff1a;冷战和消费主义本课概括&#xff1a;政府和消费者推动了计算机的发展 第二十五章&#xff1a;个人计算机革命本集概括&#xff1a;继续讲计算机发展…

机器学习系列11:减少过拟合——L1、L2正则化

如果我们注意到模型在训练集上的表现明显优于模型在测试集上的表现&#xff0c;那么这就是模型过拟合了&#xff0c;也称为 high variance。 产生的过拟合的原因是对于给定的训练集数据来说&#xff0c;模型太复杂了。有几种可以减少过拟合的方法&#xff1a; 收集更多的训练数…

Docker 概述以及整体架构

文章目录 一、Docker概述1.1 什么是 Docker1.2 Docker 如何工作1.3 底层技术 二、Docker架构2.1 Docker 整体架构2.2 Docker daemon2.3 Docker client2.4 Docker registries2.5 Docker objects2.6 Docker Desktop 参考资料 一、Docker概述 1.1 什么是 Docker Docker是一个用于…

快来检测一下你是否真的学会了C语言,保证你看完后收获满满!!

文章目录 每日一言1234567891011121314151617181920结语 每日一言 人生而自由&#xff0c;却无往不在枷锁中。 --社会契约论 1 以下程序段的输出结果是&#xff1f; char s[]"\\141\141abc\t"; printf("%d\n",strlen(s));A. 9 B. 12 C. 13 D. 14 正确答…

程序的编译、链接

目录 前言&#xff1a; 前置知识回顾 宏 宏定义常量 宏定义语句 宏定义函数 条件编译 应用场景 编译过程概览 预编译阶段 编译阶段 汇编阶段 链接阶段 前言&#xff1a; 在ANSI C的任何一种实现中&#xff0c;存在两种不同的环境&#xff0c;第1种是翻译环境&#x…

go module本地包导入

go module本地包导入 本文目录 go module本地包导入启用go mod主项目工作目录本地module目录发布和使用模块 golang 1.11之后加入了go mod来替代GOPATH 官方文档参考&#xff1a;https://golang.google.cn/doc/tutorial/call-module-code 启用go mod 开启 Go modules # 临时开…

一文带你了解大模型的RAG(检索增强生成) | 概念理论介绍+ 代码实操(含源码)

针对大型语言模型效果不好的问题&#xff0c;之前人们主要关注大模型再训练、大模型微调、大模型的Prompt增强&#xff0c;但对于专有、快速更新的数据却并没有较好的解决方法&#xff0c;为此检索增强生成&#xff08;RAG&#xff09;的出现&#xff0c;弥合了LLM常识和专有数…

数据治理:释放数据价值的关键

随着数字化时代的到来&#xff0c;数据已成为组织和企业最重要的资产之一。然而&#xff0c;数据的快速增长和复杂性也给数据管理带来了巨大的挑战。为了确保数据的质量、安全性和合规性&#xff0c;数据治理已成为组织和企业必须面对的重要问题。数据治理是数据要素市场建设的…

自动驾驶学习笔记(二十三)——车辆控制模型

#Apollo开发者# 学习课程的传送门如下&#xff0c;当您也准备学习自动驾驶时&#xff0c;可以和我一同前往&#xff1a; 《自动驾驶新人之旅》免费课程—> 传送门 《Apollo开放平台9.0专项技术公开课》免费报名—>传送门 文章目录 前言 运动学模型 动力学模型 总结…

Java进阶(第八期): Java中递归的的使用和递归解决一些算法问题 Java中的异常机制、异常的处理逻辑 自定义异常

文章目录 一、递归1.1 递归的介绍1.2 递归的简单练习1.3 图解递归执行流程&#xff1a;1.4 使用递归完成悲波那契数列1.5 猴子吃桃子问题 二、异常三 、异常的处理逻辑3.1 try catch 捕获异常3.2 throws抛出异常 四、自定义异常 Java进阶&#xff08;第八期&#xff09; 一、递…

如何安装、配置、启动及访问Nacos

准备 JDK 17.0&#xff1a;https://www.bilibili.com/video/BV1ig4y1k7Bq?p2 MySQL 8.0&#xff1a;https://www.bilibili.com/video/BV1QU4y117Vn Navicat Premium&#xff1a;https://www.bilibili.com/video/BV1F94y1A7nC 1、安装Nacos a、地址 网址&#xff1a;http…

ElasticSearch 架构设计

介绍 ElasticSearchMySQLIndexTableDocumentRowFieldColumnMappingSchemaQuery DSLSQLaggregationsgroup by&#xff0c;avg&#xff0c;sumcardinality去重 distinctreindex数据迁移 ElasticSearch 中的一个索引由一个或多个分片组成 每个分片包含多个 segment&#xff08;分…

用 Node.js 写一个爬虫

自己设计一个网站&#xff0c;然后去爬取别人家页面的数据来做一个自己的网站。哈哈哈&#xff0c;如果自己写着玩可能没啥事&#xff0c;但如果用这个网站来获利&#xff0c;你可能就要被寄律师函了&#xff0c;毕竟这有点‘刑’。这篇文章呢&#xff0c;就带大家爬取豆瓣TOP2…

HDMI2.1输入转4Port MIPI/LVDS输出,嵌入式SPI闪存固件存储,VR和AR应用首选国产芯片方案-LT6911GXC

描述 LT6911GXC是一款高性能的HDMI2.1到MIPI或LVDS芯片&#xff0c;用于VR/显示应用。 HDCP RX作为HDCP中继器的上游&#xff0c;可配合其他芯片的HDCPTX实现中继器功能。 对于HDMI2.1输入&#xff0c;LT6911GXC可以配置为3/4通道。自适应均衡使其适合于长电缆应用&#xff0c;…

申请虚拟VISA卡Fomepay教程

fomepay 用下面的注册链接直达 https://gpt.fomepay.com/#/pages/login/index?dS21BA1 或者扫描下面图片的二维码直达注册 注册后尽量随用随充值不建议放大量现金在里面。

【论文解读】用于概念标定的逻辑强化大模型LEFT(NeurIPS 2023)

来源&#xff1a;投稿 作者&#xff1a;橡皮 编辑&#xff1a;学姐 论文链接&#xff1a;https://arxiv.org/abs/2310.16035 开源代码&#xff1a;https://github.com/joyhsu0504/LEFT 摘要&#xff1a; VisProg 和 ViperGPT 等最新研究成果巧妙地组成了视觉推理的基础模型-…

vscode配置python环境,步骤以及 chatgpt和csdn AI创作助手回答对比

1解决步骤 参考地址 解决步骤 vscode 安装 python插件&#xff0c;并重启vscode&#xff0c;前提是电脑已经配置了python环境&#xff0c;我的电脑已经安装了anaconda 也有python3 新建文件夹 pythonTst &#xff0c;vscode中菜单栏 File --Add folder toWordSpace — 弹出框…