In image processing, I am afraid everyone is most familiar with the brightness and contrast of the image. There are also many people to write such an article, but I want to make my series a complete summary, I will come again. Or is therefore a 24-bit color image, each color can be represented by 0-255, a total of 256 depths. If we draw it on a two-dimensional coordinate, it is just a straight line. For example, we use the color of the pixel as the abscissa, the output color is deep as the longitudinal sites, exactly a 45 degree oblique line passed through the origin (0, 0). As shown in the figure, the angle T is 45 degrees, indicating that its contrast is 1. It is so easy to write its line equation: OUT = in * 1, the coefficient 1 is the concept of contrast If the straight line plus one offset becomes B, then its linear equation is: OUT = IN * 1 (ab) offset (ab) is the increment of brightness. As long as there is an algebraic knowledge of junior high school, it is easy to see a straight line equation: y = a * x b However, the situation we have to handle here is slightly different, in image processing, contrast and brightness should be treated separately. The brightness cannot be changed because of the change in contrast, because we are used to put gray (127, 127) as a central point. For example, we increase the contrast, the original straight line A turns into line D, which also increases the brightness (AB) while changing the contrast, and the change in our minds should be in line C. That is, we map (127, 127) to the origin of the coordinate system. Then we have to modify the original straight line formula: y = (x - 127) * A B 127. A represents the contrast, b represents the brightness increment. We verify: As long as the brightness increment B = 0, no matter how to change the contrast A, the line always passes through the central point (127, 127), that is, the brightness does not change while changing the contrast. Thus, we can derive the color contrast brightness calculation formula: newred = (oldred -127) * a 127 bnewgreen = (oldgreen -127) * a 127 bnewblue = (oldblue -127) * a 127 B now you Are you ready to use this formula to write your own brightness contrast subroutine? Slow, make another step. We are in the program, not doing junior high school algebra exams. This more step will make your program's execution efficiency higher. Let's derive the above formula: y = (x - 127) * a b 127 => y = x * a - 127 * a 127 b (1) order: b = b -127 * a 127 (2 2 steps from above (1), (2), get a new formula: y = x * a b 咦? How did you change it again? ? Yes, the form of formula is indeed changed back, but the things represented by B have been different. Maybe you will say that I am more, please want to think about smart readers: When a normal picture is a brightness contrast operation, we will bring what efficiency improvement.
Suppose a picture size is 1027 * 768 has a total of 786432 pixels, and each pixel is also calculated separately with red green blue color. Then, the above formula requires calculation of 786432 * 3 = 2359296 times, and after so many computments amplification, even a small repetition calculation will waste long. Because the brightness and contrast have been determined during the calling subroutine, then B = b -127 * a 127 can be placed outside the loop. Thereby reducing the operation time of the program. My program is given below for reference: Public Sub BrightnessAndContrast (ByVal RedOffset As Long, ByVal GreenOffset As Long, ByVal BlueOffset As Long, Optional ByVal RedContrast As Single = 1, Optional ByVal GreenContrast As Single = 1, Optional ByVal BlueContrast As Single = 1) Dim X As LongDim Y As LongDim MidR As IntegerDim MidG As IntegerDim MidB As IntegerDim Max As LongOn Error GoTo ErrLineDone = FalseTimeFilter = timeGetTimeMidR = redOffset - 127 * (RedContrast - 1) 'calculates a new shift amount BMidG = greenOffset - 127 * (GreenContrast - 1) MIDB = Blueoffset - 127 * (BlueContrast - 1) max = 255 for x = 0 to outputWid for y = 0 to outputhei r = colut (2, x, y) g = colut (1, x , Y) B = colord (0, x, y) r = r * RedContraSt midr 'calculate y = x * a b g = g * GreenContrast MIDG B = B * BlueContrast MIDB if r> max life r = Max 'output value determines if IF R <0 THEN R = 0 IF G> max life between 0 and 255 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) = g colut (0, x, y) = b NextNextDone = trueTimeFilter = TimegetTime - TimeFilterexit SuberRline: msgBox err.descriptionDone = trueEnd Sub
Since the calculated value exceeds (0,255) in the process of brightness, it is necessary to make a judgment to this range. This procedure is simple, and can be calculated based on a given red green blue brightness offset and contrast parameters. Since the six parameters of the three colors are separated, only one color can be adjusted. There is also a benefit, that is, when you set the contrast parameter to a negative value, you can directly get the original image of the original image. (This is also a benefit of moving the previous coordinate system to 127 this point.) Below is the effect obtained with my program processing: original: brightness 20, contrast 1.5 effect: contrast-1, reverse color effect: as The last article of this series, I have been posted on the effect of the effect used in my program imagecast. It is also a small trip to everyone. If you are interested, you can download my program. Download address: http://download.9cbs.net/source/325026
If the reader is unclear about some arrays and variables in this process, please refer to the previous articles in front, which describes the detailed description: VB image processing, (1) acquisition and output of pixels
(Here, I just said that I used the method of writing the program, there is a lot of shortcomings. Because some modifications are made in the post, there may be some errors, please enlighten you, you will use you. A better way to provide it, I will be grateful.)
VB image processing, (b) Application VB image processing, (3) Several common filter implementation 1VB image processing, (4) implementation 2 VB image processing of several common filters, (5) image Color correction
VB image processing, (six) brightness contrast adjustment
VB image processing, (7) an algorithm for a neighboring mean filter (dust, noise) (new)