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

你离开我真会死。 提交于 2019-12-10 19:16:24

问题


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!!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4888503/how-to-use-colormatrix-in-net-to-change-brightness-color-saturation-hue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!