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

Source: Internet
Author: User
Tags array
Several previous sessions have been about how to manipulate pixels to achieve some simple filter effects.
This time I would like to tell you about the use of gray-scale histogram equalization to adjust the color space of the image.
First of all to introduce some of the color of small common sense.
In the color of the computer, the composition of the color is right red, green, blue, composed of three colors.
With the easiest 24-bit color, red, green, and blue are each represented by 1 bytes, 1 bytes 8 bits, so it's exactly 24 digits. Because the computer can not use a continuous analog value to represent a natural quantity, it can only be divided into a paragraph to show that the more the more the more close to nature. 1 bytes is 2^8=256, so in 24-bit color each monochrome has 256 different intensities, three colors are mixed according to different intensity, can get 2^24 kind of color. About 16.77 million colors can be expressed, for the eyes of people has no difference with the natural color.
If we use Photoshop to open a picture, select the Level tool, we can see the color distribution of this picture.


Original:


Grayscale Channel: Red channel:


Green channel: Blue channel:


From the above four channels, we can find that the color of this picture is mainly low brightness, red, green and blue three colors in the high brightness area of the distribution are very small, and gray channel also indicates that the overall picture brightness value is very low.
In the previous article, I have told you that the human eye is the most sensitive to grayscale (brightness). So if we can improve the grayscale of this chapter image in one way, then it will have better performance in our vision.
Perhaps a friend said as long as the picture to add brightness, not on it? Yes, by improving the brightness of all colors, you can shift the color of the original light area to the medium brightness or high brightness area, but as you know, in the algorithm, the addition of brightness is very simple in R, G, b three values directly followed by an offset: Newred=oldred+offset, Newgreen=oldgreen+offset,newblue+offset, but this approach only "rudely" to the entire color space moved a position, but did not change its distribution. Please see below, I add 120 points brightness in this picture: Look at the color distribution at this time you know why I said it was "rough".


Grayscale Channel: Red channel:


Green channel: Blue channel:
You can see that the high brightness part of the original image of the color information is all lost, and the low brightness part (0-120) is a blank,
If the original picture is 2^8x2^8x2^8 altogether 16.77 million colors, then now the picture is:
(256-120) ^3=251, that is to say, by adding 120 points of brightness to the picture, we lost the half color information.
(choose to add 120 points brightness is to make the picture overall brightness and histogram balance after the brightness is comparable, easy to compare.) )
So, what can be done without losing, or missing very little color information on the premise of the picture Show better?
Yes, this is the "Gray histogram equalization" method that we are going to say today.
First of all, let us look at the effect: The following is the original picture through the gray histogram method of processing after the effect and the previous comparison.


Original:
Increase Brightness 120:

Gray-level histogram equalization: The color distribution after histogram equalization is as follows:


Grayscale Channel: Red channel:


Green channel: Blue channel:
By contrast, we can find that the gray histogram equalization is a distribution of the color distribution in the original image according to the frequency of occurrence.
will appear the most color "cent" open, will appear little color "squeeze" tighter, the advantage of this is to see the main body of our eyes to express more clearly.
Well, about the effect, also has said a lot, below we will explain how this algorithm is implemented.
First, we need to get the color distribution statistics of all the pixels in the picture to be processed, which is what the above channels do.
Let's say there is a graph (we're going to represent it directly in grayscale):
100 50 20
20 40 50
100 250 200
Statistics into the following:
20:2
40:1
50:2
100:2
200:1
250:1
This picture has a total of 9 pixels, and we use proportions to denote the proportions of each color:
20:2/9
40:1/9
50:2/9
100:2/9
200:1/9
250:1/9
Since all colors cannot appear more than the total pixel of the picture, the proportion of all colors will not be added to more than 1 (you can already see exactly 1).
Finally, we follow the order from low to high, the proportion of each color weighted statistics, that is, the current point of the "right" equals the original proportion of the point and the previous point of the "right", we get a new statistical table:
20:2/9
40:3/9
50:5/9
100:7/9
200:8/9
250:9/9
Finally, based on this new tab, we'll replace the brightness of the pixel with a new brightness, and the algorithm is:
New brightness = the point "right" x255
20:2/9 >> 20 (1th No move, still use 20)
40:3/9x255=85
50:5/9x255=141
100:7/9x255=198
200:8/9x255=226
250:9/9x255=255

And then we got a new diagram:
100 50 20 198 141 20
>> 10 85 141
100 250 200 198 255 226
The width of the portion of the original image that is more frequent than the frequency becomes larger. And the smaller ones become narrower.
Therefore, the role of gray-scale histogram equalization is to put a picture on the appearance of many color expansion, and the emergence of less color compression.
Thus, a more "balanced" color distribution is obtained.
My routines are attached below:
Private Type Colorchart
Colorcount (255) as Long ' counts the number of brightness occurrences in the original picture
Pixcelcount as Long ' to record the number of pixels in a picture
Colratio (255) as single to record the percentage of each brightness
newval (255) as Byte holds the new brightness index
End Type
Dim Colchart as Colorchart

Public Sub Statisticschart ()
Dim R as Byte
Dim G as Byte
Dim B as Byte
Dim Gray as Integer
Dim X as Long
Dim Y as Long
Dim I as Long
Dim L as Long
Dim M as Long
Dim C as Double
On Error GoTo Errline

Done = False
Timefilter = timeGetTime
With Colchart
For X = 0 to 255 ' first clear the array to zero
. Colorcount (X) = 0
Next
For X = 0 to Outputwid ' These two loops are used to scan picture data, to record the grayscale and occurrences of each point
For Y = 0 to Outputhei
R = Colval (2, X, Y)
G = Colval (1, X, Y)
B = Colval (0, X, Y)
Gray = R * 3 + G * 6 + B
Gray = Gray \ 10
. Colorcount (Gray) =. Colorcount (Gray) + 1
Next
Next

. Pixcelcount = X * Y ' gets the total number of pixels in the picture
C = 1/. Pixcelcount

. Colratio (0) =. Colorcount (M, 0) * C ' calculates the occurrence ratio of each brightness
. newval (0) = 0 ' color value is always the smallest color 0, do not participate in the calculation
L = 0
For I = 1 to 255
. Colratio (I) =. Colorcount (I) * C +. Colratio (L) ' Weighted
. newval (I) =. Colratio (I) * 255 ' calculates the new color index
L = L + 1
Next

For X = 0 to Outputwid
For Y = 0 to Outputhei
R = Colval (2, X, Y) ' reads the color of the original point
G = Colval (1, X, Y)
B = Colval (0, X, Y)
R =. newval (R) ' look-up table to get new color
G =. newval (G)
B =. newval (B)
Colout (2, X, Y) = R ' puts the new color in the output array
Colout (1, X, Y) = G
Colout (0, X, Y) = B
Next
Next

End With

Done = True
Timefilter = Timegettime-timefilter
Exit Sub
Errline:
Done = True
MsgBox Err.Description
End Sub


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


The next one will continue to describe the color contrast and brightness of the adjustment algorithm, as well as color image conversion to grayscale image algorithm.
Do not think this is very simple, there are some small problems to pay attention to OH.

(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.