Grayscale conversion to color

Source: Internet
Author: User

Minor modifications to the https://blog.csdn.net/huixingshao/article/details/42706699

The principle of pseudo-color processing for grayscale images is as follows:

according to the color study results, the grayscale image corresponds to red, green, blue three channels, and finally the color values of three channels are synthesized into the RGB color values that need to be displayed.

the mapping between grayscale values and three channels is shown in Figure 3, figure 4, and Figure 5:

                       

Figure 1: Grayscale and red channel mapping relationship

Figure 2: Grayscale and green channel mapping relationship

[CPP] View Plain copy print? Vec3b greytocolormix (int val)    {       int r,g,b;           //red       if  (val<128)         {           r = 0;        }       else if  (val<192)         {           r = 255/ 64* (val-128);       }       else        {           r=255;        }          //green       if   (val<64)        {           g = 255/64*val;       }        else if  (val<192)        {            g = 255;       }        else       {            g= -255/63* (val - 192) +255;       }           //blue       if  (val<64)         {           b = 255;       }       else if  (val<128)         {           b = -255/63* (Val  - 192) +255;       }       else        {           b=0;        }       Vec3b rgb;       rgb[0]  = b;       rgb[1] = g;        rgb[2] = r;       return rgb;  }  

vec3b greytocolormix (int val)
{
    int r,g,b;

    Red
    if (val<128)
    {
        r = 0;
    }
    else if (val<192)
    {
        r = 255/64* (val-128);
    }
    else
    {
        r=255;
    }

    Green
    if (val<64)
    {
        g = 255/64*val;
    }
    else if (val<192)
    {
        g = 255;
    }
    else
    {
        g= -255/64* (val-192) +255;
    }

    Blue
    if (val<64)
    {
        b = 255;
    }
    else if (val<128)
    {
        b = -255/64* (val-128);
    }
    else
    {
        b=0;
    }
    VEC3B RGB;
    Rgb[0] = b;
    Rgb[1] = g;
    RGB[2] = r;
    return RGB;
}

Figure 3: Grayscale and blue channel mapping relationship









        </div>
            </div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.