In image processing, I am afraid everyone is most familiar with adjusting the brightness and contrast of the image.
A lot of people have written such articles before, but I want to make a complete summary of my series, so I will be a bit wordy again.
In the case of a 24-digit color image, each color can be expressed in a total of 255 depths, ranging from 0 to 256.
If we plot it on a two-dimensional coordinate, It is a straight line.
For example, we use the pixel color depth as the horizontal coordinate, and the output color depth as the vertical coordinate, Which is exactly a 45-degree diagonal line passing through the origin (0, 0.
In line A, the angle T is 45 degrees, indicating that its contrast is exactly 1.
It is easy to write its linear equation: Out = In * 1, and coefficient 1 is the concept of contrast.
If a line is added with an offset to B, its linear equation becomes: Out = In * 1 + (AB)
Offset (AB) is the increment of brightness.
As long as you have junior high school algebra knowledge, it is easy to see that it satisfies A linear equation: Y = A * X + B
However, the process here is slightly different. in image processing, the contrast and brightness must be treated separately.
The brightness cannot be changed because the gray (127,127) is used as the center point.
For example, when we increase the contrast, the original line A is changed to A line D, and the brightness (AB) is also increased while the contrast is changed ), the change in our mind should be like a straight line C. That is to say, we map (127,127) to the origin of the coordinate system.
Then we need to change the original linear formula to: Y = (X-127) * A + B. A Indicates contrast, and B Indicates brightness increase.
Let's verify: as long as the brightness increment is B = 0, no matter how contrast A is changed, the line always passes through the center point (127,127). That is to say, the brightness does not change while the contrast is changed.
As a result, we can export the contrast brightness calculation formula of the color:
NewRed = (OldRed-127) * A + 127 + B
NewGreen = (OldGreen-127) * A + 127 + B
NewBlue = (OldBlue-127) * A + 127 + B
Are you ready to use this formula to write your own brightness and contrast subroutine?
Take another step. We are in the program, not in the junior high school algebra examination. This step will make the execution of your program more efficient.