Saturday, June 18, 2005

Converting a Color Bitmap to Greyscale with .Net



"One of the most frequently asked questions that I've seen on Drawing in .Net is how to convert a color image to greyscale. Here I will describe 3 methods of performing the conversion as well as 3 algorithms for computing the conversion. ..."

In my post, Playing with Text and GDI+ in VB.Net - System.Drawing.Fun, I talk a little about how there's no .Net method to convert a color image to black and white (or greyscale).

I mention that it's been suggested that you could loop through every pixel and determine if a bit should be Black or White. I still think that's crazy talk. What's the performance going to be like on a 300DPI, 8 1/2" x 11" bitmap? Not having actually done it, I just have to assume it's going to suck... So I've let others do my conversion dirty work and have stuck with Lead14.Net.

Now I'm thinking about ColorMatrix ...

The above article, Converting Color images to Greyscale, provides another approach, via applying a ColorMatrix (it also talks about pixel looping).

The matrix approach is pretty interesting and something I want to look at in more detail. Check out the below performance quote.

"...Comparing the two transformation algorithms is pretty straight forward. I ran the transform 1000 times and took the average time required to perform the transformation:

By Pixel
0.5013256 seconds

Matrix
0.0519308 seconds

So roughly speaking, using a Matrix is 10 times faster then converting every pixel individually. ..."


Interesting...

No comments: