How to use ColorMatrix in .NET to change Brightness, Color, Saturation, Hue

4.7k views Asked by At

I have a 'Bitmap' type containing some random bitmap data. I've written my own adjustments for Brightness, Color, Saturation, and Hue that act on each bit individually and, unsurprisingly, it's awful slow.

In my research I've noticed that using matrices can be very fast in adjusting these. In addition, .NET has a ColorMatrix where you can apply the matrix effects when you DrawImage().

The matrix we set up looks like the following (from MSDN website):

float[][] colorMatrixElements = { 
new float[] {2,  0,  0,  0, 0},        // red scaling factor of 2
new float[] {0,  1,  0,  0, 0},        // green scaling factor of 1
new float[] {0,  0,  1,  0, 0},        // blue scaling factor of 1
new float[] {0,  0,  0,  1, 0},        // alpha scaling factor of 1
new float[] {.2f, .2f, .2f, 0, 1}};    // three translations of 0.2

But I haven't been able to find proper ranges or what exactly any of these numbers actually do. I have no idea how to adjust for Brightness, Color, Saturation, and Hue.

Any help?? Am I missing some good documentation somewhere?

Thanks!!

2

There are 2 answers

1
Kris On BEST ANSWER

There are some details at http://www.graficaobscura.com/matrix/index.html but you may want to post your other code. Doing operations per-pixel is very common and you wouldn't usually encounter performance issues for this kind of operation. .NET's Bitmap.SetPixel is notoriously slow. There is a good C# image processing series at codeproject showing a faster method. I don't have experience with c++-cli but I'll take a look.

1
Coincoin On

There are limitations on what you can do with a color matrix.

Basically, the matrix you provide is in fact an homogenous 4 dimensions transformation. It's a big word that means you can rotate, scale, translate the different channels (R G B A) of your color.

See Transformation Matrix for more details.