1 void F_Gray2Color(CvMat* gray_mat, CvMat* color_mat)
2 {
3 if(color_mat)
4 cvZero(color_mat);
5
6 int stype = CV_MAT_TYPE(gray_mat->type), dtype = CV_MAT_TYPE(color_mat->type);
7 int rows = gray_mat->rows, cols = gray_mat->cols;
8
9 // 判断输入的灰度图和输出的伪彩色图是否大小相同、格式是否符合要求
10 if (CV_ARE_SIZES_EQ(gray_mat, color_mat) && stype == CV_8UC1 && dtype == CV_8UC3)
11 {
12 CvMat* red = cvCreateMat(rows, cols, CV_8U); // 红色分量
13 CvMat* green = cvCreateMat(rows, cols, CV_8U); // 绿色分量
14 CvMat* blue = cvCreateMat(rows, cols, CV_8U); // 蓝色分量
15 CvMat* mask = cvCreateMat(rows, cols, CV_8U);
16
17 cvSubRS(gray_mat, cvScalar(255), blue); // blue = 255 - gray
18 cvCopy(gray_mat, red); // red = gray
19 cvCopy(gray_mat, green); // green = gray , if gray < 128
20 cvCmpS(green, 128, mask, CV_CMP_GE ); //
21 cvSubRS(green, cvScalar(255), green, mask); // green = 255 - gray , if gray >= 128
22 cvConvertScale(green, green, 2.0, 0.0); // green = 2 * green
23
24 // 将蓝绿红三色融合为一幅伪彩色图
25 cvMerge(blue, green, red, NULL, color_mat);
26
27 cvReleaseMat( &red );
28 cvReleaseMat( &green );
29 cvReleaseMat( &blue );
30 cvReleaseMat( &mask );
31 }
32 }