|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
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 IntroductionThis is easy algorithm of image transformation from grayscale image to color. Its far not ideal, but he very simple. BackgroundUsually we calculate gray color as:
Using the code
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();
}
}
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
}
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;
Figure 2. Simple color dialog.
Figure 3. Grayscale image, color image and color diagram. Points of InterestIts algorithms is very easy, and he has many defects. But it is good example for beginners in image processing. Update info1.New formula for grayscale level calculation. 2.Bug with empty list was remove.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||