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

2023-12-30 18:15:55

前言

前段时间,甲方提出明确需求,让把软件国产化。稍微研究了一下,那就转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不出现变化。
在这里插入图片描述

文章来源:https://blog.csdn.net/drgraph/article/details/135304651
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。