VB image processing, (vi) Image brightness contrast adjustment

Source: Internet
Author: User
Tags integer
In the image processing, I am afraid that everyone is most familiar with the image of the brightness and contrast adjustment.
There must be a lot of people in front of this article, but want to make a complete summary of my series, I will be more wordy.
Or a 24-bit color image as an example, each color can be expressed in 0-255, a total of 256 depth.
If we draw it in a two-dimensional coordinate, it's just a straight line.
For example, we use the color depth of the pixel as the horizontal axis, the output color depth as the ordinate of the painting, is just a pass through the origin (0,0) of the 45-degree slash.
As the line A in the figure shows, the angle T is 45 degrees, indicating that it is exactly 1 contrast.
Then it's easy to write out its linear equation: out = in * 1, coefficient 1 is the concept of contrast
If a line is added to the last offset to B, then its linear equation becomes: out = in * 1 + (AB)
The offset (AB) is the increment of the brightness.
As long as the algebra knowledge of junior high school it is easy to see that it satisfies a linear equation: y= A * X + B
However, we have to deal with a slightly different situation here, in the image processing, contrast and brightness to be treated separately.
The brightness cannot be changed because we are accustomed to the gray (127,127) as the central point.
For example, when we increase the contrast, the original line a becomes as shown in line D, changing the contrast and increasing the brightness (AB), and the change in our mind should be in line C. In other words, we map (127,127) This point to the origin of the coordinate system.
So we're going to change the original line formula to: y= (X-127) * A + B. A indicates contrast, and b represents the brightness increment.
Let's verify that if the brightness increment b=0, no matter how the contrast A is changed, the line always passes through the center point (127,127), which means that the brightness does not change while the contrast is changed.
Thus, we can deduce the contrast brightness 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 start using this formula to write your own brightness contrast subroutine?
Slow down, take one more step. We are in the process, not in the middle school algebra exam. This extra step will make your program more efficient to perform.
Let's deduce the formula above:
y= (X-127) * A + B => Y = X * A-127 * A + 127+b (1)
Order: B = B-127 * A +127 (2)
From above (1), (2) Two steps, get a new formula: Y = X * A + B
Hey? How come back again??
The form of the formula is indeed changed back, but the things that B represent are different.
Perhaps you will say that I am superfluous, please the wise Reader imagine: in a normal picture to do brightness contrast operation, we above these small changes will bring about what kind of efficiency improvement. Suppose a picture size is 1027*768
A total of 786,432 pixels, and each pixel to be calculated separately red, green and blue three colors.
So, the above formula needs to calculate 786432 * 3 = 2,359,296 times, after so many operations, even a small repeat calculation will waste a long time.
Since the brightness and contrast are determined at the time the subroutine is invoked, B = B-127 * A +127 This step can be placed outside the loop to be done first. Thus reducing the operation time of the program.
Here's my program for reference:
Public Sub Brightnessandcontrast (ByVal redoffset as Long, ByVal Greenoffset as Long, ByVal Blueoffset as Long, Optional by Val Redcontrast as Single = 1, Optional ByVal greencontrast as only = 1, Optional ByVal bluecontrast as single = 1)
Dim X as Long
Dim Y as Long
Dim Midr as Integer
Dim MIDG as Integer
Dim MidB as Integer
Dim Max as Long
On Error GoTo Errline
Done = False
Timefilter = timeGetTime

MIDR = RedOffset-127 * (RedContrast-1)
MIDG = GreenOffset-127 * (GreenContrast-1)
MidB = BlueOffset-127 * (BlueContrast-1)
Max = 255 ' * Valueratio
For X = 0 to Outputwid
For Y = 0 to Outputhei
R = Colout (2, X, Y)
G = Colout (1, X, Y)
B = colout (0, X, Y)
R = R * Redcontrast + MIDR
g = G * Greencontrast + MIDG
b = b * Bluecontrast + MidB
If r > Max Then r = Max
If R < 0 Then r = 0
If g > Max Then g = max
If g < 0 Then g = 0
If b > Max Then b = max
If B < 0 Then B = 0
Colout (2, X, Y) = R
Colout (1, X, Y) = G
Colout (0, X, Y) = B
Next
Next
Done = True
Timefilter = Timegettime-timefilter
Exit Sub
Errline:
MsgBox Err.Description
Done = True
End Sub

Because the calculated value is beyond (0,255) in the brightness contrast process, it is necessary to make a judgment and limit the result to that range.
This program is simple and can be computed based on the brightness offset and contrast parameters given for the red and green blue. Because the 6 parameters of the three colors are separated, you can adjust only a single color.
Another advantage is that when you set the contrast parameter to a negative value, you can directly get the original image of the inverse color output. (This is also one of the benefits of moving the previous coordinate system origin to 127.) )
Here's what you can do with my program:
Original:
Brightness +20, contrast 1.5 effect:
Contrast-1, reversed-phase color effect:


As the last article in this series, the algorithm and main code I used in my program ImageCast have been posted. It is also a small account of their own to everyone.
If interested friends can download my program to try.
Download Address: http://club.5ivb.net/dispbbs.asp?boardID=1&ID=40069
Program Interface:

If the reader is not clear about some of the arrays and variables in the process, please refer to my previous articles, which are described in detail:
VB image processing, (a) pixel acquisition and output

VB image processing, (II.) application of two linear interpolation

VB image processing, (three) several commonly used filters to achieve 1

VB image processing, (four) several commonly used filters to achieve 2

VB image processing, (v) color correction of the image

(here just said my own in the process of writing the method used, there are a lot of deficiencies.) And because in the paste up when made some changes, there may be some mistakes, please do not hesitate to enlighten me, will you use a better way to provide, I would appreciate. )




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.