当前位置: 主页 > 图像 >

伪彩色OpenCV转换和C++代码

时间:2019-04-19  作者:haden   点击:
【摘要】伪彩色OpenCV转换和C++代码

Value

name

score

0

COLORMAP_AUTUMN

 

1

COLORMAP_BONE

 

2

COLORMAP_JET

 

3

COLORMAP_WINTER

 

4

COLORMAP_RAINBOW

 

5

COLORMAP_OCEAN

 

6

COLORMAP_SUMMER

 

7

COLORMAP_SPRING

 

8

COLORMAP_COOL

 

9

COLORMAP_HSV

 

10

COLORMAP_PINK

 

11

COLORMAP_HOT

 


OpenCV伪彩色方法:
applyColorMap(im_gray, im_color, COLORMAP_HSV);


代码实现:


//将 CV_16UC1 深度图 转换成伪彩色图
Mat gray2rainbow(const Mat& scaledGray, int min, int max)
{
    Mat outputRainbow(scaledGray.size(), CV_8UC3); //初始化了一个outputRainbow的彩色图像
    unsigned short grayValue;
    float tempvalue;

    float par = (float)255 / (max - min);


    for (int y = 0; y < scaledGray.rows; y++)
        for (int x = 0; x < scaledGray.cols; x++)
        {

            grayValue = scaledGray.at<ushort>(y, x);
            if ((grayValue > 0) && (grayValue < min)) //可能会出现找到的min并不是真正的最小值
            {
                tempvalue = (float)min;
            }
            else if (grayValue > max) //也可能会出现找到的max并不是真正的最大值
            {
                tempvalue = 0;
            }
            else
            {
                tempvalue = (float)(grayValue - min);
            }
            tempvalue = tempvalue*par; //为了把深度值规划到(0~255之间)
            /*
            * color R G B gray
            * red 255 0 0 255
            * orange 255 127 0 204
            * yellow 255 255 0 153
            * green 0 255 0 102
            * cyan 0 255 255 51
            * blue 0 0 255 0
            */

            Vec3b& pixel = outputRainbow.at<Vec3b>(y, x);
            tempvalue = 256 - tempvalue;

            if ((tempvalue <= 0) || (tempvalue >= 255))
            {
                pixel[0] = 0;
                pixel[1] = 0;
                pixel[2] = 0;
            }
            else if (tempvalue <= 51)
            {
                pixel[0] = 255;
                pixel[1] = (unsigned char)(tempvalue * 5);
                pixel[2] = 0;
            }
            else if (tempvalue <= 102)
            {
                tempvalue -= 51;
                pixel[0] = 255 - (unsigned char)(tempvalue * 5);
                pixel[1] = 255;
                pixel[2] = 0;
            }
            else if (tempvalue <= 153)
            {
                tempvalue -= 102;
                pixel[0] = 0;
                pixel[1] = 255;
                pixel[2] = (unsigned char)(tempvalue * 5);
            }
            else if (tempvalue <= 204)
            {
                tempvalue -= 153;
                pixel[0] = 0;
                pixel[1] = 255 - static_cast<unsigned char>(tempvalue * 128.0 / 51 + 0.5);
                pixel[2] = 255;
            }
            else if (tempvalue < 255)
            {
                tempvalue -= 204;
                pixel[0] = 0;
                pixel[1] = 127 - static_cast<unsigned char>(tempvalue * 127.0 / 51 + 0.5);
                pixel[2] = 255;
            }
        }

    return outputRainbow;
}

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码: 点击我更换图片