contrast with color matrix

扶醉桌前 提交于 2019-12-04 20:34:15

The t value must be derived from the new contrast value c. so change its assignment like this:

float t = (1.0f - c) / 2.0f;

According to this nice link to the ColorMatrix Guide the rest of the matrix code seems to be OK.

Note: I was wrong about the range of c!! The value of c is not an absolute value but it is the factor that should be applied! So to double the contrast it should be 2f.

Note 2: Your code is not quite clear about source and target; and since you are changing contrast on the fly with the trackbar it should be clear that until the change is applied,

  • The intermediate results must always be calculated from the same original bitmap.

  • Also, that the trackbar should be so intialized that it starts with a value of 1.

  • And finally, in order to reduce contrast values should translate to 0 < c < 1.

Nico's suggestion c = 1+ value; would work nicely with your original range -100 to +100 with an intial value of 0 and your factor 0.01f.

Here are some further explanations about the maths behind the process. If you are going to accept an answer, accept TaW's answer, he was quicker than me.

Adjusting the contrast basically does the following:

  1. It shifts all pixel values by -0.5 (so that a medium-gray becomes 0)
  2. It applies a factor to all pixel values
  3. It reverts the shift

So in a formula:

newValue = factor * (oldValue - 0.5) + 0.5
         = factor * oldValue - factor * 0.5 + 0.5
         = factor * oldValue + 0.5 * (1 - factor)

For a single-channel image we can transform this into a matrix multiplication:

(newValue, 1) = (oldValue, 1) * /        factor      , 0\     <-- this is c, 0
                                \ 0.5 * (1 - factor) , 1/     <-- this is t, 1

For multi-channel images, the 2x2 matrix becomes a 5x5 (four channels + w-channel for translations) matrix with c and t at the positions you already specified.

Keep in mind that a factor of 1 does not change anything. A factor of 0 makes the whole image medium-gray. So you may want to adjust your calculation of c:

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