15,114,611 members
Articles / Multimedia / GDI
Article
Posted 20 Nov 2008

111.6K views
59 bookmarked

# Image Transformation: Grayscale to Color

Rate me:
Algorithm for easy transformation images

## 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:

C#
`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]<br />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 - C1<sub>Gray</sub>)/(C2<sub>Gray </sub>- C1<sub>Gray</sub>)<br />Red = C1<sub>Red </sub>+ K*(C2<sub>Red </sub>- C1<sub>Red</sub>)<br />Green = C1<sub>Green </sub>+ K*(C2<sub>Green </sub>- C1<sub>Green</sub>)<br />Blue = C1<sub>Blue </sub>+ K*(C2.<sub>Blue </sub>- C1<sub>Blue</sub>)`

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

Figure 1. Transformation schema.

## Using the Code

`ControlPoint `is a structure for storing data about control points.

C#
```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:

C#
```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.

C#
```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:

C#
```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;```

Figure 2. Simple color dialog.

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

## Share

 Software Developer Russian Federation
Hello! My name is Maxim Subbotin.

Now I work in sphere of web-development. I'm interesting researches in SEO field.
If you interesting, you can see this tool:

KeywordCompetitor

 First Prev Next
 grayscale to color image Member 1419464424-Aug-20 2:35 Member 14194644 24-Aug-20 2:35
 about algorithm Norhaat18-Dec-11 14:44 Norhaat 18-Dec-11 14:44
 grayscale to color on HSI model thanh_rio27-Jun-09 1:28 thanh_rio 27-Jun-09 1:28
 Re: grayscale to color on HSI model Maxim_Barsuk28-Jun-09 20:16 Maxim_Barsuk 28-Jun-09 20:16
 Couldnt you make it better??? gonzospy1-May-09 7:26 gonzospy 1-May-09 7:26
 Re: Couldnt you make it better??? Maxim_Barsuk3-May-09 20:45 Maxim_Barsuk 3-May-09 20:45
 Re: Couldnt you make it better??? gonzospy5-May-09 19:25 gonzospy 5-May-09 19:25
 Re: Couldnt you make it better??? [modified] Maxim_Barsuk6-May-09 20:08 Maxim_Barsuk 6-May-09 20:08
 Круто Laserson26-Jan-09 21:44 Laserson 26-Jan-09 21:44
 Re: Круто Maxim_Barsuk27-Jan-09 5:53 Maxim_Barsuk 27-Jan-09 5:53
 Something to go off of.... Sike Mullivan26-Jan-09 5:34 Sike Mullivan 26-Jan-09 5:34
 Re: Something to go off of.... Maxim_Barsuk26-Jan-09 7:10 Maxim_Barsuk 26-Jan-09 7:10
 Re: Something to go off of.... MicroImaging3-Jun-09 8:13 MicroImaging 3-Jun-09 8:13
 Re: My vote of 2 Afzaal Ahmad Zeeshan3-Sep-15 4:55 Afzaal Ahmad Zeeshan 3-Sep-15 4:55
 what about color to grayscale? bluish24-Jan-09 9:26 bluish 24-Jan-09 9:26
 Re: what about color to grayscale? Maxim_Barsuk25-Jan-09 20:24 Maxim_Barsuk 25-Jan-09 20:24
 Luminace from RGB terry mohre25-Nov-08 4:22 terry mohre 25-Nov-08 4:22
 Re: Luminace from RGB Maxim_Barsuk25-Nov-08 6:55 Maxim_Barsuk 25-Nov-08 6:55
 My vote of 1 Christian Wikander25-Nov-08 2:59 Christian Wikander 25-Nov-08 2:59
 Why? Christian Wikander25-Nov-08 2:58 Christian Wikander 25-Nov-08 2:58
 Bugs Alexander Yakovlev21-Nov-08 2:30 Alexander Yakovlev 21-Nov-08 2:30
 Re: Bugs Maxim_Barsuk21-Nov-08 6:39 Maxim_Barsuk 21-Nov-08 6:39
 Re: Bugs thanh_rio4-Jun-09 7:55 thanh_rio 4-Jun-09 7:55
 Re: Bugs Maxim_Barsuk4-Jun-09 20:08 Maxim_Barsuk 4-Jun-09 20:08
 thanh_rio wrote:i can't colored an image perfectly. Quality color image depends on the number of control points. For the control point should be to choose the most common colors in the image. Building a color chart is the next step. Remember that the transformation of monochrome images in color is not unique. Monochrome color lies in the one-dimensional space, color RGB - in three-dimensional space. This algorithm can't give an ideal result, but in some cases the result may be good.
 Last Visit: 31-Dec-99 19:00     Last Update: 29-Nov-21 2:41 Refresh 1