Click here to Skip to main content
5,787,682 members and growing! (16,961 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, C#, Windows (Windows, WinXP), .NET (.NET, .NET 2.0), Visual Studio (VS2005, Visual Studio), GDI, Dev

Posted: 20 Nov 2008
Updated: 20 Nov 2008
Views: 3,949
Bookmarked: 15 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.82 Rating: 3.63 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
2 votes, 33.3%
3
1 vote, 16.7%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Download Mono_2_Color.zip - 95.5 KB

Introduction  

This is easy algorithm of image transformation from grayscale image to color. Its far not ideal, but he 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 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 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 locate between two control points C1 and C2, that his color approximate 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 structure for store 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 class for compare 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 class for build color diagram. He return 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 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  

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

Update info

1.New formula for grayscale level calculation.

2.Bug with empty list was remove.

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


Hello! My name is Maxim Subbotin. I'm from Russia. I'm junior software developer in EPAM Systems.
Occupation: Software Developer (Junior)
Company: EPAM
Location: Russian Federation Russian Federation

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralLuminace from RGBmemberterrymohre4:22 25 Nov '08  
GeneralRe: Luminace from RGBmemberMaxim_Barsuk6:55 25 Nov '08  
GeneralMy vote of 1memberChristian Wikander2:59 25 Nov '08  
GeneralWhy?memberChristian Wikander2:58 25 Nov '08  
GeneralBugsmemberAlexander Yakovlev2:30 21 Nov '08  
GeneralRe: BugsmemberMaxim_Barsuk6:39 21 Nov '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 Nov 2008
Editor: Sean Ewington
Copyright 2008 by Maxim_Barsuk
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project