Using lambda to manipulate images
February 23, 2010 2 Comments
Working with every pixel in an image – the lambda way!
When working with images, I frequently need to apply the same function to every pixel in the image. Since I do that a lot, I decided to create some nice lambda image processing extensions!
In C# Win32, you would typically work with the System.Drawing.Bitmap class, so that’s what I’ll use. In C# WPF, there are other classes you’d use instead, but I won’t go into that in this blog post.
Initially I’ll use two methods, one that allows me to compute the color of each pixel in the image. Another that allows me to perform an arbitrary action for each pixel in the bitmap – the action could set the color of the pixel, but it’s not required.
Here are my extension methods for image manipulation;
using System;
using System.Drawing;
namespace LambdaImageProcessing.Core
{
public static class BitmapExtensionMethods
{
public static void ExecuteForEachPixel(this Bitmap bitmap, Action<Point, Bitmap> action)
{
Point point = new Point(0, 0);
for (int x = 0; x < bitmap.Width; x++)
{
point.X = x;
for (int y = 0; y < bitmap.Height; y++)
{
point.Y = y;
action(point, bitmap);
}
}
}
public static void ExecuteForEachPixel(this Bitmap bitmap, Action<Point> action)
{
Point point = new Point(0, 0);
for (int x = 0; x < bitmap.Width; x++)
{
point.X = x;
for (int y = 0; y < bitmap.Height; y++)
{
point.Y = y;
action(point);
}
}
}
public static void SetEachPixelColour(this Bitmap bitmap, Func<Point, Color> colourFunc)
{
Point point = new Point(0, 0);
for (int x = 0; x < bitmap.Width; x++)
{
point.X = x;
for (int y = 0; y < bitmap.Height; y++)
{
point.Y = y;
bitmap.SetPixel(x, y, colourFunc(point));
}
}
}
public static void SetEachPixelColour(this Bitmap bitmap, Func<Point, Color, Color> colourFunc)
{
Point point = new Point(0, 0);
for (int x = 0; x < bitmap.Width; x++)
{
point.X = x;
for (int y = 0; y < bitmap.Height; y++)
{
point.Y = y;
bitmap.SetPixel(x, y, colourFunc(point, bitmap.GetPixel(x, y)));
}
}
}
}
}
Using my lambda method to draw the color of each pixel as a gradient given the distance to the center of the image. That code looks like this;
private void renderDistanceFromCenterButton_Click(object sender, EventArgs e)
{
// Build the bitmap
Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
Point imageCenter = new Point(pictureBox.Width / 2, pictureBox.Height / 2);
// Ok, this is terrible, you _really_ shouldn't use lambda like this... But you can...
Func<double, double, double> distance = (x, y) => Math.Sqrt(x * x + y * y);
double maxDistance = distance(imageCenter.X, imageCenter.Y);
bitmap.SetEachPixelColour(
point =>
{
// Pythagoras
double dist = distance((point.X - imageCenter.X), (point.Y - imageCenter.Y));
byte b = Convert.ToByte(255 * dist / maxDistance);
return Color.FromArgb(b, b, b);
});
pictureBox.Image = bitmap;
}
Running this code produces this image;
Not very pretty, but it works. And I didn’t have to write a single loop, which I find myself doing over and over again when not using lambda functions.
Make image black and white
Converting a color image to black and white can be done this way;
private void makeBWButton_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap("lena.jpg");
bitmap.SetEachPixelColour(
(point, color) =>
{
// Simplest way for making BW
byte b = (byte)((color.R + color.B + color.G) / 3);
return Color.FromArgb(b, b, b);
});
pictureBox.Image = bitmap;
}
Here’s Lena in color;
And with the BW method above;
Tinting an image – method 1
If you have an image that you wish to tint (mix it with a color) – for instance make it sepia toned – you can use this method;
public static Color TintColor(Color originalColor, Color tintColor, byte strength)
{
byte s1 = (byte)(100 - strength);
return
Color.FromArgb(
(s1 * originalColor.R + strength * tintColor.R) / 100,
(s1 * originalColor.G + strength * tintColor.G) / 100,
(s1 * originalColor.B + strength * tintColor.B) / 100);
}
private void tintBitmapButton_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap("lena.jpg");
bitmap.SetEachPixelColour(
(point, color) =>
{
return TintColor(color, Color.Blue, 50);
});
pictureBox.Image = bitmap;
}
Which produces this Lena;
Tinting an image – method 2
This method converts the pixel to black and white before mixing in the tint color – so that nothing of the original image color remains;
private void tint2BitmapButton_Click(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap("lena.jpg");
Color targetColor = Color.SpringGreen;
bitmap.SetEachPixelColour(
(point, color) =>
{
double luminence = ((color.R * 30 + color.G * 59 + color.B * 11) / 100.0) / 255.0;
return
Color.FromArgb(
Convert.ToByte(targetColor.R * luminence),
Convert.ToByte(targetColor.G * luminence),
Convert.ToByte(targetColor.B * luminence));
});
pictureBox.Image = bitmap;
}
Which produces this Lena;
Compare two images – the naive way
In a later blog post, I’ll talk about robust ways of comparing images, but here’s a naive approach for comparing two images of the same size. It computes the summed “colour distance” between the images.
private double CompareBitmaps(Bitmap firstBitmap, Bitmap secondBitmap)
{
if (firstBitmap.Width != secondBitmap.Width || firstBitmap.Height != secondBitmap.Height)
{
return 1;
}
double difference = 0;
firstBitmap.ExecuteForEachPixel(
point =>
{
Color firstColor = firstBitmap.GetPixel(point.X, point.Y);
Color secondColor = secondBitmap.GetPixel(point.X, point.Y);
difference += Math.Abs(secondColor.R / 255.0 - firstColor.R / 255.0);
difference += Math.Abs(secondColor.G / 255.0 - firstColor.G / 255.0);
difference += Math.Abs(secondColor.B / 255.0 - firstColor.B / 255.0);
});
// Normalize the difference so it's not size dependent
difference /= (firstBitmap.Width * firstBitmap.Height);
return difference;
}
Comparing identical images returns 1, comparing totally de-similar images returns 0.
Pingback: Perlin Noise in C# « Mattias Fagerlund's Coding Blog
Pingback: Using Lambda to manipulate Silverlight/Wpf bitmaps « Mattias Fagerlund's Coding Blog