灰度图像伪彩色
在三维图像显示中,需要将视差图像(灰度空间)变成伪彩色图像。
opencv代码:
void DispColorMap(const Mat &img, Mat &img_color) { img_color = Mat(img.rows, img.cols, CV_8UC3);//RGB图像 #define IMG_B(img,y,x) img.at<Vec3b>(y,x)[0] #define IMG_G(img,y,x) img.at<Vec3b>(y,x)[1] #define IMG_R(img,y,x) img.at<Vec3b>(y,x)[2] uchar tmp2=0; cout <<"color" << endl; for (int y=0;y<img.rows;y++)//转为彩虹图的具体算法,主要思路是把灰度图对应的0~255的数值分别转换成彩虹色:红、橙、黄、绿、青、蓝。 { for (int x=0;x<img.cols;x++) { tmp2 = img.at<uchar>(y,x); if (tmp2 <= 51) { IMG_B(img_color,y,x) = 255; IMG_G(img_color,y,x) = 0; IMG_R(img_color,y,x) = 0; } else if (tmp2 <= 102) { tmp2-=51; IMG_B(img_color,y,x) = 255-tmp2*5; IMG_G(img_color,y,x) = 255; IMG_R(img_color,y,x) = 0; } else if (tmp2 <= 153) { tmp2-=102; IMG_B(img_color,y,x) = 0; IMG_G(img_color,y,x) = 255; IMG_R(img_color,y,x) = tmp2*5; } else if (tmp2 <= 204) { tmp2-=153; IMG_B(img_color,y,x) = 0; IMG_G(img_color,y,x) = 255-uchar(128.0*tmp2/51.0+0.5); IMG_R(img_color,y,x) = 255; } else { IMG_B(img_color,y,x) = 0; IMG_G(img_color,y,x) = 255; IMG_R(img_color,y,x) = 0; } } } }