标签:bsp 符号 选择 错误 大于 最大 ace IV 成员函数
Mat类是C++实现的OpenCV库的核心,表示一个N维度单通或多通道阵列,可以用来存储实数或复数值向量和数组,灰度或彩色图像,向量场,张量及直方图(当然高纬度的直方图存储在稀疏Mat类更合适),OpenCV是一个图像处理库。它包含大量的图像处理功能。为了解决计算上的挑战,大多数时候你最终会使用库的多个功能。因此,将图像传递给函数是一种常见的做法。我们不应该忘记,我们正在讨论图像处理算法,这些算法往往计算量很大。我们想要做的最后一件事是通过制作不必要的大型图像副本来进一步降低程序的速度。
为了解决这个问题,OpenCV使用了一个引用计数系统。这个想法是,每个Mat对象都有自己的头部,但是矩阵可以通过让它们的矩阵指针指向相同的地址而在它们的两个实例之间共享。而且,复制操作符只会复制标题和指向大矩阵的指针,而不是数据本身。
从上可以看出Mat基本上是一个包含两个数据部分的类:矩阵头(包含矩阵大小,用于存储的方法,存储矩阵的地址等信息)以及包含像素值(取决于选择用于存储的方法的任何维度)。矩阵头部大小是恒定的,但是矩阵本身的大小可能随着图像而变化,并且通常比数量级大。
创建一个Mat类型对象可以没有大小和数据类型,然后通过成员函数create()来分配指定,例如创建一个二维数组,可以使用create(int rows, int cols, types)备注:仅限于二维数组,第三个参数类型既指定了元素的类型又说明了通道数,这些类型都定义在矩阵头中,格式为:CV_{8U,16S,16U,32S,32F,64F}C{1,2,3},然后通过setTo()函数设定每个通道上的具体数值;
通常定义创建一个Mat类型的对象可以通过构造函数在创建矩阵时即分配内存,其中一个构造函数的参数与create()函数参数相同;
1 //演示示例:创建一个Mat对象数组 2 #include <opencv2/highgui.hpp> 3 #include <opencv2/core.hpp> 4 #include <iostream> 5 6 using namespace std; 7 using namespace cv; 8 9 int main() 10 { 11 Mat array_test; 12 array_test.create( 3, 2, CV_32FC3 ); //create array header 13 array_test.setTo(Scalar(1.0f,2.0f,3.0f)); //set array data 14 15 cout << "array_test = " << endl << array_test << endl; 16 //如下使用构造函数等同上面分布创建 17 Mat array_test_con( 3, 2, CV_32FC3, Scalar(1.0f,2.0f,3.0f) ); 18 cout << "array_test_con = " << endl << array_test_con << endl; 19 return 0; 20 }
如上叙述,为了解决图像处理繁杂计算问题,OpenCV使用了一个引用计数系统。这个想法是,针对图像的数据,每个Mat对象都有自己的头部相对应,通过让它们的矩阵指针指向相同的地址而在它们的两个实例对象之间共享。而且,复制操作符只会复制标题和指向大矩阵的指针,而不是数据本身。
1 //演示示例:矩阵头Array Header和矩阵数据Array data关系 2 #include <opencv2/highgui.hpp> 3 #include <opencv2/core.hpp> 4 #include <iostream> 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 Mat A( 2, 2, CV_8UC3, Scalar(10, 20, 30)); 11 12 cout << "A = " << endl << A << endl; 13 14 cout << endl; 15 16 Mat B(A); 17 cout << "B = " << endl << B << endl; 18 19 cout << endl; 20 21 Mat C = A; 22 cout << "C = " << endl << C << endl; 23 24 cout << endl << "After Mat algebra..." << endl; 25 26 Mat A1( 2, 2, CV_8UC3, Scalar(1, 2, 3)); 27 28 A = A +A1; 29 30 cout << "A = " << endl << A << endl; 31 cout << "B = " << endl << B << endl; 32 cout << "C = " << endl << C << endl; 33 34 return 0; 35 }
上述所有对象最后指向相同的单一数据矩阵。然而,它们的标头是不同的,并且使用它们中的任何一个进行修改也会影响所有其他标签。实际上,不同的对象只是为相同的底层数据提供不同的访问方法。不过,它们的矩阵头部分是真正相互独立的。这样,可以创建一个仅包含矩阵数据的一部分的矩阵对象。例如,要在图像中创建感兴趣的区域(ROI),您只需创建一个带有新边界的新矩阵头:
1 #include <opencv2/highgui.hpp> 2 #include <opencv2/core.hpp> 3 #include <iostream> 4 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 Mat A( 4, 4, CV_8UC3, Scalar(10, 20, 30)); 11 cout << "A = " << endl << A << endl; 12 13 cout << endl; 14 15 Mat B(A); 16 cout << "B = " << endl << B << endl; 17 18 cout << endl; 19 20 Mat C = A; 21 22 cout << "C = " << endl << C << endl; 23 24 cout << endl << "After Mat algebra..." << endl; 25 26 Mat A1( 4, 4, CV_8UC3, Scalar(1, 2, 3)); 27 28 A = A +A1; 29 30 cout << "A = " << endl << A << endl; 31 cout << "B = " << endl << B << endl; 32 cout << "C = " << endl << C << endl; 33 //在图像中创建感兴趣的区域(ROI) 34 Mat D(A, Rect(0,0,2,2)); 35 cout << "D = " << endl << D << endl; 36 37 D.setTo(Scalar(1, 2, 3)); 38 cout << "A = " << endl << A << endl; 39 40 //创建一个带有新边界的新矩阵头 41 Mat E = A(Range::all(), Range(2,3)); 42 cout << "E = " << endl << E << endl; 43 44 E.setTo(Scalar(7, 8, 9)); 45 cout << "A = " << endl << A << endl; 46 47 return 0; 48 }
当想复制矩阵本身,所以OpenCV提供了cv :: Mat :: clone()和cv :: Mat :: copyTo()函数。
1 #include <opencv2/highgui.hpp> 2 #include <opencv2/core.hpp> 3 #include <iostream> 4 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 Mat A( 4, 4, CV_8UC3, Scalar(10, 20, 30)); 11 cout << "A = " << endl << A << endl; 12 13 cout << endl; 14 15 Mat B(A); 16 cout << "B = " << endl << B << endl; 17 18 cout << endl; 19 20 Mat C = A; 21 cout << "C = " << endl << C << endl; 22 23 cout << endl << "After Mat algebra..." << endl; 24 25 Mat A1( 4, 4, CV_8UC3, Scalar(1, 2, 3)); 26 27 A = A +A1; 28 29 cout << "A = " << endl << A << endl; 30 cout << "B = " << endl << B << endl; 31 cout << "C = " << endl << C << endl; 32 33 Mat D(A, Rect(0,0,2,2)); //拷贝构造函数中Rect仅适用于二维矩阵 34 cout << "D = " << endl << D << endl; 35 36 D.setTo(Scalar(1, 2, 3)); 37 cout << "A = " << endl << A << endl; 38 39 Mat E = A(Range::all(), Range(2,3)); //拷贝构造函数中Range 仅适用于二维矩阵 40 cout << "E = " << endl << E << endl; 41 E.setTo(Scalar(7, 8, 9)); 42 cout << "A = " << endl << A << endl; 43 44 Mat F = A.clone(); 45 cout << "F = " << endl << F << endl; 46 47 Mat G; 48 A.copyTo(G); 49 cout << "G = " << endl << G << endl; 50 51 A.setTo(Scalar(10, 20, 30)); 52 cout << "A = " << endl << A << endl; 53 cout << "F = " << endl << F << endl; 54 cout << "G = " << endl << G << endl; 55 return 0; 56 }
针对稀疏矩阵,OpenCV定义了独立的数据结构SpareMat类。稀疏矩阵仅存储非零元素,避免了资源上的浪费,即将节省很多空间尤其针对元素存在许多零元素的数据, 使用稀疏矩阵的常用案例是直方图,对于直方图,大多数数据是零,存储这些零元素又是没有必要的。
1 void addWeighted( 2 InputArray src1, //第一个输入矩阵 3 double alpha, //第一个输入矩阵的权重 4 InputArray src2, //第二个输入的矩阵 5 double beta, //第二个输入矩阵的权重 6 double gamma, //权重相加的偏移量 7 OutputArray dst, //输出的结果 8 int dtype = -1) //输出结果的类型
即加权的表达式如下:
Dst = α·src1 + β·src2 + γ
该函数可以用来实现alhpa融合(线性融合),即将公式中的γ设置为0,则alpha融合的公式演变为:
Dst = α·src1 + β·src2 = α·src1 + (1-α)·src2
函数中需要两个源图像src1和src2,这两个源图像可以是任意类型的像素(灰度,彩色),但只要他们属于同一类型一致即可,所输出的图像也是与源图像的像素类型一致;
注意:源图像可以是不同尺寸,但融合的操作区域(即感兴趣区域ROI)必须尺寸统一,否则OpenCV会产生错误;
1 //alpha融合演示示例 2 #include <opencv2/highgui.hpp> 3 #include <iostream> 4 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 //读取图像imread()显示图像imshow()函数待后续 11 Mat src1 = imread("D:\\workspace-qt\\OpenCV\\LinuxLogo.jpg"); 12 if(src1.empty()) {cout << "Can‘t Load the image..." << endl; return -1;} 13 Mat src2 = imread("D:\\workspace-qt\\OpenCV\\WindowsLogo.jpg"); 14 if(src2.empty()) {cout << "Can‘t Load the image..." << endl; return -1;} 15 Mat dst; 16 addWeighted(src1,0.5,src2,0.5,0.0,dst,-1); 17 imshow("Alpha",dst); 18 waitKey(0); 19 return 0; 20 }
备注:在加权融合图像的便宜参数γ方面,函数提供了更大的灵活性,一般而言,使α和β不小于0且相加不大于1,那么γ的设定取决于加权后图像像素所要调整到的平均值或最大值。
1 void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 )
输入图像数组可以是8位数组,16位无符号类型数组,或者32位浮点类型,而输出图像数组与输入数组保持着相同的尺寸大小和深度,转换操作由参数code来实现,dstCn表示输出图像的通道数,默认设为0意味着输出数组自动由源图像src和转换规则code作用后自动获取;
1 enum ColorConversionCodes { 2 COLOR_BGR2BGRA = 0, //!< add alpha channel to RGB or BGR image 3 COLOR_RGB2RGBA = COLOR_BGR2BGRA, 4 5 COLOR_BGRA2BGR = 1, //!< remove alpha channel from RGB or BGR image 6 COLOR_RGBA2RGB = COLOR_BGRA2BGR, 7 8 COLOR_BGR2RGBA = 2, //!< convert between RGB and BGR color spaces (with or without alpha channel) 9 COLOR_RGB2BGRA = COLOR_BGR2RGBA, 10 11 COLOR_RGBA2BGR = 3, 12 COLOR_BGRA2RGB = COLOR_RGBA2BGR, 13 14 COLOR_BGR2RGB = 4, 15 COLOR_RGB2BGR = COLOR_BGR2RGB, 16 17 COLOR_BGRA2RGBA = 5, 18 COLOR_RGBA2BGRA = COLOR_BGRA2RGBA, 19 20 COLOR_BGR2GRAY = 6, //!< convert between RGB/BGR and grayscale, @ref color_convert_rgb_gray "color conversions" 21 COLOR_RGB2GRAY = 7, 22 COLOR_GRAY2BGR = 8, 23 COLOR_GRAY2RGB = COLOR_GRAY2BGR, 24 COLOR_GRAY2BGRA = 9, 25 COLOR_GRAY2RGBA = COLOR_GRAY2BGRA, 26 COLOR_BGRA2GRAY = 10, 27 COLOR_RGBA2GRAY = 11, 28 29 COLOR_BGR2BGR565 = 12, //!< convert between RGB/BGR and BGR565 (16-bit images) 30 COLOR_RGB2BGR565 = 13, 31 COLOR_BGR5652BGR = 14, 32 COLOR_BGR5652RGB = 15, 33 COLOR_BGRA2BGR565 = 16, 34 COLOR_RGBA2BGR565 = 17, 35 COLOR_BGR5652BGRA = 18, 36 COLOR_BGR5652RGBA = 19, 37 38 COLOR_GRAY2BGR565 = 20, //!< convert between grayscale to BGR565 (16-bit images) 39 COLOR_BGR5652GRAY = 21, 40 41 COLOR_BGR2BGR555 = 22, //!< convert between RGB/BGR and BGR555 (16-bit images) 42 COLOR_RGB2BGR555 = 23, 43 COLOR_BGR5552BGR = 24, 44 COLOR_BGR5552RGB = 25, 45 COLOR_BGRA2BGR555 = 26, 46 COLOR_RGBA2BGR555 = 27, 47 COLOR_BGR5552BGRA = 28, 48 COLOR_BGR5552RGBA = 29, 49 50 COLOR_GRAY2BGR555 = 30, //!< convert between grayscale and BGR555 (16-bit images) 51 COLOR_BGR5552GRAY = 31, 52 53 COLOR_BGR2XYZ = 32, //!< convert RGB/BGR to CIE XYZ, @ref color_convert_rgb_xyz "color conversions" 54 COLOR_RGB2XYZ = 33, 55 COLOR_XYZ2BGR = 34, 56 COLOR_XYZ2RGB = 35, 57 58 COLOR_BGR2YCrCb = 36, //!< convert RGB/BGR to luma-chroma (aka YCC), @ref color_convert_rgb_ycrcb "color conversions" 59 COLOR_RGB2YCrCb = 37, 60 COLOR_YCrCb2BGR = 38, 61 COLOR_YCrCb2RGB = 39, 62 63 COLOR_BGR2HSV = 40, //!< convert RGB/BGR to HSV (hue saturation value), @ref color_convert_rgb_hsv "color conversions" 64 COLOR_RGB2HSV = 41, 65 66 COLOR_BGR2Lab = 44, //!< convert RGB/BGR to CIE Lab, @ref color_convert_rgb_lab "color conversions" 67 COLOR_RGB2Lab = 45, 68 69 COLOR_BGR2Luv = 50, //!< convert RGB/BGR to CIE Luv, @ref color_convert_rgb_luv "color conversions" 70 COLOR_RGB2Luv = 51, 71 COLOR_BGR2HLS = 52, //!< convert RGB/BGR to HLS (hue lightness saturation), @ref color_convert_rgb_hls "color conversions" 72 COLOR_RGB2HLS = 53, 73 74 COLOR_HSV2BGR = 54, //!< backward conversions to RGB/BGR 75 COLOR_HSV2RGB = 55, 76 77 COLOR_Lab2BGR = 56, 78 COLOR_Lab2RGB = 57, 79 COLOR_Luv2BGR = 58, 80 COLOR_Luv2RGB = 59, 81 COLOR_HLS2BGR = 60, 82 COLOR_HLS2RGB = 61, 83 84 COLOR_BGR2HSV_FULL = 66, //!< 85 COLOR_RGB2HSV_FULL = 67, 86 COLOR_BGR2HLS_FULL = 68, 87 COLOR_RGB2HLS_FULL = 69, 88 89 COLOR_HSV2BGR_FULL = 70, 90 COLOR_HSV2RGB_FULL = 71, 91 COLOR_HLS2BGR_FULL = 72, 92 COLOR_HLS2RGB_FULL = 73, 93 94 COLOR_LBGR2Lab = 74, 95 COLOR_LRGB2Lab = 75, 96 COLOR_LBGR2Luv = 76, 97 COLOR_LRGB2Luv = 77, 98 99 COLOR_Lab2LBGR = 78, 100 COLOR_Lab2LRGB = 79, 101 COLOR_Luv2LBGR = 80, 102 COLOR_Luv2LRGB = 81, 103 104 COLOR_BGR2YUV = 82, //!< convert between RGB/BGR and YUV 105 COLOR_RGB2YUV = 83, 106 COLOR_YUV2BGR = 84, 107 COLOR_YUV2RGB = 85, 108 109 //! YUV 4:2:0 family to RGB 110 COLOR_YUV2RGB_NV12 = 90, 111 COLOR_YUV2BGR_NV12 = 91, 112 COLOR_YUV2RGB_NV21 = 92, 113 COLOR_YUV2BGR_NV21 = 93, 114 COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21, 115 COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21, 116 117 COLOR_YUV2RGBA_NV12 = 94, 118 COLOR_YUV2BGRA_NV12 = 95, 119 COLOR_YUV2RGBA_NV21 = 96, 120 COLOR_YUV2BGRA_NV21 = 97, 121 COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21, 122 COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21, 123 124 COLOR_YUV2RGB_YV12 = 98, 125 COLOR_YUV2BGR_YV12 = 99, 126 COLOR_YUV2RGB_IYUV = 100, 127 COLOR_YUV2BGR_IYUV = 101, 128 COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV, 129 COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV, 130 COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12, 131 COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12, 132 133 COLOR_YUV2RGBA_YV12 = 102, 134 COLOR_YUV2BGRA_YV12 = 103, 135 COLOR_YUV2RGBA_IYUV = 104, 136 COLOR_YUV2BGRA_IYUV = 105, 137 COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV, 138 COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV, 139 COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12, 140 COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12, 141 142 COLOR_YUV2GRAY_420 = 106, 143 COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420, 144 COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420, 145 COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420, 146 COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420, 147 COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420, 148 COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420, 149 COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420, 150 151 //! YUV 4:2:2 family to RGB 152 COLOR_YUV2RGB_UYVY = 107, 153 COLOR_YUV2BGR_UYVY = 108, 154 //COLOR_YUV2RGB_VYUY = 109, 155 //COLOR_YUV2BGR_VYUY = 110, 156 COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY, 157 COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY, 158 COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY, 159 COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY, 160 161 COLOR_YUV2RGBA_UYVY = 111, 162 COLOR_YUV2BGRA_UYVY = 112, 163 //COLOR_YUV2RGBA_VYUY = 113, 164 //COLOR_YUV2BGRA_VYUY = 114, 165 COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY, 166 COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY, 167 COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY, 168 COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY, 169 170 COLOR_YUV2RGB_YUY2 = 115, 171 COLOR_YUV2BGR_YUY2 = 116, 172 COLOR_YUV2RGB_YVYU = 117, 173 COLOR_YUV2BGR_YVYU = 118, 174 COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2, 175 COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2, 176 COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2, 177 COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2, 178 179 COLOR_YUV2RGBA_YUY2 = 119, 180 COLOR_YUV2BGRA_YUY2 = 120, 181 COLOR_YUV2RGBA_YVYU = 121, 182 COLOR_YUV2BGRA_YVYU = 122, 183 COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2, 184 COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2, 185 COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2, 186 COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2, 187 188 COLOR_YUV2GRAY_UYVY = 123, 189 COLOR_YUV2GRAY_YUY2 = 124, 190 //CV_YUV2GRAY_VYUY = CV_YUV2GRAY_UYVY, 191 COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY, 192 COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY, 193 COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2, 194 COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2, 195 COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2, 196 197 //! alpha premultiplication 198 COLOR_RGBA2mRGBA = 125, 199 COLOR_mRGBA2RGBA = 126, 200 201 //! RGB to YUV 4:2:0 family 202 COLOR_RGB2YUV_I420 = 127, 203 COLOR_BGR2YUV_I420 = 128, 204 COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420, 205 COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420, 206 207 COLOR_RGBA2YUV_I420 = 129, 208 COLOR_BGRA2YUV_I420 = 130, 209 COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420, 210 COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420, 211 COLOR_RGB2YUV_YV12 = 131, 212 COLOR_BGR2YUV_YV12 = 132, 213 COLOR_RGBA2YUV_YV12 = 133, 214 COLOR_BGRA2YUV_YV12 = 134, 215 216 //! Demosaicing 217 COLOR_BayerBG2BGR = 46, 218 COLOR_BayerGB2BGR = 47, 219 COLOR_BayerRG2BGR = 48, 220 COLOR_BayerGR2BGR = 49, 221 222 COLOR_BayerBG2RGB = COLOR_BayerRG2BGR, 223 COLOR_BayerGB2RGB = COLOR_BayerGR2BGR, 224 COLOR_BayerRG2RGB = COLOR_BayerBG2BGR, 225 COLOR_BayerGR2RGB = COLOR_BayerGB2BGR, 226 227 COLOR_BayerBG2GRAY = 86, 228 COLOR_BayerGB2GRAY = 87, 229 COLOR_BayerRG2GRAY = 88, 230 COLOR_BayerGR2GRAY = 89, 231 232 //! Demosaicing using Variable Number of Gradients 233 COLOR_BayerBG2BGR_VNG = 62, 234 COLOR_BayerGB2BGR_VNG = 63, 235 COLOR_BayerRG2BGR_VNG = 64, 236 COLOR_BayerGR2BGR_VNG = 65, 237 238 COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG, 239 COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG, 240 COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG, 241 COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG, 242 243 //! Edge-Aware Demosaicing 244 COLOR_BayerBG2BGR_EA = 135, 245 COLOR_BayerGB2BGR_EA = 136, 246 COLOR_BayerRG2BGR_EA = 137, 247 COLOR_BayerGR2BGR_EA = 138, 248 249 COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA, 250 COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA, 251 COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA, 252 COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA, 253 254 //! Demosaicing with alpha channel 255 COLOR_BayerBG2BGRA = 139, 256 COLOR_BayerGB2BGRA = 140, 257 COLOR_BayerRG2BGRA = 141, 258 COLOR_BayerGR2BGRA = 142, 259 260 COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA, 261 COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA, 262 COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA, 263 COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA, 264 265 COLOR_COLORCVT_MAX = 143 266 };
备注:色彩空间转换都用到如下约定:
1 8位图像的范围:0~255 2 16位图像的范围:0~65536 3 浮点数的范围:0.0~1.0 4 黑白图像转换位彩色图像时,最终图形与黑白图像的通道数相同 5 彩色图像转换为灰度图像时,灰度值计算使用加权公式: 6 Y= 0.299R+0.587G+0.114B
针对HSV色彩模式或者HLS色彩模式来说色调范围通常Rang(0,360);
当HSV色彩模式以8位图像形式输出时,色调应处以2,才能不出现问题;
1 //RGB转Gray演示示例 2 #include <opencv2/highgui.hpp> 3 #include <opencv2/imgproc.hpp> 4 #include <iostream> 5 6 using namespace std; 7 using namespace cv; 8 9 int main() 10 { 11 Mat src2 = imread("D:\\workspace-qt\\OpenCV\\WindowsLogo.jpg"); 12 if(src2.empty()) {cout << "Can‘t Load the image..." << endl; return -1;} 13 14 Mat dst_gray; 15 cvtColor(src2,dst_gray,CV_BGR2GRAY); 16 imshow("Alpha",dst_gray); 17 waitKey(0); 18 return 0; 19 }
void flip(InputArray src, OutputArray dst, int flipCode)
将图像绕X轴或者Y周或绕X轴Y轴上同时旋转,取决于参数flipCode的设置
1 图像绕X轴旋转:flipCode = 0; 2 图像绕Y轴旋转:flipCode = 1; 3 图像绕X轴和Y轴同时旋转:flipCode = -1;
常用于图像坐标原点在左上角和左下角的变换时使用,尤其是进行视频处理所进行的图像格式变化;
1 #include <opencv2/highgui.hpp> 2 #include <opencv2/imgproc.hpp> 3 #include <iostream> 4 5 using namespace std; 6 using namespace cv; 7 8 int main() 9 { 10 Mat src = imread("D:\\workspace-qt\\OpenCV\\WindowsLogo.jpg"); 11 if(src.empty()) {cout << "Can‘t Load the image..." << endl; return -1;} 12 13 imshow("src",src); 14 Mat dst_x, dst_y, dst_x_y; 15 flip(src,dst_x,0); 16 flip(src,dst_y,1); 17 flip(src,dst_x_y,-1); 18 imshow("flipCode = 0",dst_x); 19 imshow("flipCode = 1",dst_y); 20 imshow("flipCode = -1",dst_x_y); 21 waitKey(0); 22 return 0; 23 }
标签:bsp 符号 选择 错误 大于 最大 ace IV 成员函数
原文地址:https://www.cnblogs.com/Shuqing-cxw/p/9218419.html