Click here to Skip to main content
6,822,613 members and growing! (21,216 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Bitmaps     Intermediate License: The Code Project Open License (CPOL)

Image Transformation: Grayscale to Color

By Maxim_Barsuk

Algorithm for easy transformation images
C#2.0, C#3.0, Windows (WinXP), .NET (.NET2.0), Visual-Studio (VS2005), GDI, Dev
Revision:4 (See All)
Posted:20 Nov 2008
Updated:6 May 2009
Views:23,111
Bookmarked:48 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 4.54 Rating: 3.86 out of 5
1 vote, 6.7%
1
1 vote, 6.7%
2
3 votes, 20.0%
3
4 votes, 26.7%
4
6 votes, 40.0%
5

Introduction  

This is an easy algorithm of image transformation from grayscale image to color. It's far from ideal, but it is very simple.

Background

Usually we calculate gray color as:

Gray  = Green * 0.59 + Blue * 0.30 + Red * 0.11;

Each color with structure [Gray, Gray, Gray] has a set of colors:

G = [Gray, Gray, Gray]
G -> P = {C}, for each C from P: Green * 0.59 + Blue * 0.30 + Red * 0.11 = Gray.

First of all, we create a set of control points. Control point is equivalent of "gray" color and "full" color. When we have control points set, we can approximate any "gray" color to "full" color. If "gray" color is located between two control points C1 and C2, then the color approximates as:

K = (Gray - C1Gray)/(C2Gray - C1Gray)
Red = C1Red + K*(C2Red - C1Red)
Green = C1Green + K*(C2Green - C1Green)
Blue = C1Blue + K*(C2.Blue - C1Blue)

Now we has "full color" equivalent for each "gray" color.

scheme_2.jpg

Figure 1. Transformation schema.

Using the Code  

ControlPoint is a structure for storing data about control points.

public struct ControlPoint
{
    private int level;
     public int Level
    {
        get { return level; }
        set { level = value; }
    }
    private Color color;
     public Color Color
    {
        get { return color; }
        set { color = value; }
    }
     public ControlPoint(int level, Color color)
    {
        this.color = color;
        this.level = level;
    }
     public override string ToString()
    {
        return "Level: " + level.ToString() + "; Color: " + color.ToString();
    }
} 

PointsComparer is a class for comparing two control points:

public class PointsComparer: IComparer<ControlPoint>
{
    #region IComparer<ControlPoint> Members
     public int Compare(ControlPoint x, ControlPoint y)
    {
        if (x.Level > y.Level)
        {
            return 1;
        }
        if (x.Level < y.Level)
        {
            return -1;
        }
        return 0;
    }
     #endregion
} 

ColorBuilder is a class for building a color diagram. It returns an array of 256 colors - our diagram.

public static Color[] GetColorDiagram(List<ControlPoint> points)
{
    Color[] colors = new Color[256];
    points.Sort(new PointsComparer());
     for (int i = 0; i < 256; i++)
    {
        ControlPoint leftColor = new ControlPoint
				(0, GetNearestLeftColor(points[0].Color)); 
        ControlPoint rightColor = new ControlPoint
			(255, GetNearestRigthColor(points[points.Count - 1].Color));
        if (i < points[0].Level)
        {
            rightColor = points[0];
        }
        if (i > points[points.Count - 1].Level)
        {
            leftColor = points[points.Count - 1];
        }
        else
        {
            for (int j = 0; j < points.Count - 1; j++)
            {
                if ((points[j].Level <= i) & (points[j + 1].Level > i))
                {
                    leftColor = points[j];
                    rightColor = points[j + 1];
                }
            }
        }
        if ((rightColor.Level - leftColor.Level) != 0)
        {
            double koef = (double)(i - leftColor.Level) / 
			(double)(rightColor.Level - leftColor.Level);
            int r = leftColor.Color.R + (int)(koef * 
			(rightColor.Color.R - leftColor.Color.R));
            int g = leftColor.Color.G + (int)(koef * 
			(rightColor.Color.G - leftColor.Color.G));
            int b = leftColor.Color.B + (int)(koef * 
			(rightColor.Color.B - leftColor.Color.B));
            colors[i] = Color.FromArgb(r, g, b);
        }
        else
        {
            colors[i] = leftColor.Color;
        }
     }
     return colors;
} 

Now we can get the color image:

colorBitmap = new Bitmap(sourceBitmap);
for (int i = 0; i < sourceBitmap.Width; i++)
{
    for (int j = 0; j < sourceBitmap.Height; j++)
    {
        int level = sourceBitmap.GetPixel(i, j).B;
        colorBitmap.SetPixel(i, j, colors[level]);
    }
}
pbxColorImage.Image = colorBitmap;

ColorDialog.JPG

Figure 2. Simple color dialog.

GrayColor.JPG

Figure 3. Grayscale image, color image and color diagram.

Points of Interest  

The algorithms is very easy, and has many defects. But it is a good example for beginners in image processing. 

Update Information

  1. New formula for grayscale level calculation
  2. Bug with empty list was removed

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Maxim_Barsuk


Member
Hello! My name is Maxim Subbotin. I'm from Russia.
Occupation: Software Developer
Company: EPAM
Location: Russian Federation Russian Federation

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Generalgrayscale to color on HSI model Pinmemberthanh_rio1:28 27 Jun '09  
GeneralRe: grayscale to color on HSI model PinmemberMaxim_Barsuk20:16 28 Jun '09  
GeneralCouldnt you make it better??? Pinmembergonzospy7:26 1 May '09  
GeneralRe: Couldnt you make it better??? PinmemberMaxim_Barsuk20:45 3 May '09  
GeneralRe: Couldnt you make it better??? Pinmembergonzospy19:25 5 May '09  
GeneralRe: Couldnt you make it better??? [modified] PinmemberMaxim_Barsuk20:08 6 May '09  
GeneralКруто PinmemberLaserson21:44 26 Jan '09  
GeneralRe: Круто PinmemberMaxim_Barsuk5:53 27 Jan '09  
GeneralSomething to go off of.... PinmemberSkully10225:34 26 Jan '09  
GeneralRe: Something to go off of.... PinmemberMaxim_Barsuk7:10 26 Jan '09  
GeneralRe: Something to go off of.... PinmemberMicroImaging8:13 3 Jun '09  
GeneralMy vote of 2 PinmemberMadZookeeper3:37 26 Jan '09  
Generalwhat about color to grayscale? Pinmemberbluish9:26 24 Jan '09  
GeneralRe: what about color to grayscale? PinmemberMaxim_Barsuk20:24 25 Jan '09  
GeneralLuminace from RGB Pinmemberterrymohre4:22 25 Nov '08  
GeneralRe: Luminace from RGB PinmemberMaxim_Barsuk6:55 25 Nov '08  
GeneralMy vote of 1 PinmemberChristian Wikander2:59 25 Nov '08  
GeneralWhy? PinmemberChristian Wikander2:58 25 Nov '08  
GeneralBugs PinmemberAlexander Yakovlev2:30 21 Nov '08  
GeneralRe: Bugs PinmemberMaxim_Barsuk6:39 21 Nov '08  
NewsRe: Bugs Pinmemberthanh_rio7:55 4 Jun '09  
GeneralRe: Bugs PinmemberMaxim_Barsuk20:08 4 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 6 May 2009
Editor: Deeksha Shenoy
Copyright 2008 by Maxim_Barsuk
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project