A few times a few more ways to implement some simple filter effects through operating pixels. This time I want to tell more about the color space of the image with grayscale histogram balance. Let's introduce some of some colors. For the color of the computer, the composition of the color is the three colors of right red, green, and blue. In the easiest 24-bit color, red, green, blue each uses 1 byte, 1 byte is 8 bits, so it is just 24 bits. Since the computer cannot express a natural amount with a continuous simulation value, only they can be divided into a segment, the more it is, the more natural. One byte is 2 ^ 8 = 256, so each monochrome in 24-bit color has 256 different intensities. The three colors are mixed in different strengths, and 2 ^ 24 colors can be obtained. About can express 16.77 million colors, which have nothing to do with natural color for human eyes. If we use Photoshop to open a picture, choose the Level tool, you can see the color distribution of this picture.
Original image:
Grayscale channel: red channel:
Green channel: blue channel:
From the above four channels, we can find that the color of this picture is based on low brightness, and the distribution of red and green blue in high brightness areas is small, while the grayscale channel also shows the entire picture. The brightness value is very low. In the previous article, I have talked with you that the human eye is the highest in grayscale (brightness). Therefore, if we can improve the grayscale of this chapter through a method, it will have a better performance in our vision. Maybe there is a friend saying that just add the picture, isn't it? Yes, by increasing all color brightness, you can transfer the color of the bright area to the middle brightness or high brightness area, but everyone knows that in the algorithm, the brightness is only very simple in R, G, and B. The value is directly plus an offset: newred = Oldred Offset, newgreen = OldGreen Offset, NewBlue Offset, but this approach is just a "rude" to move the entire color space in a position without changing its distribution. Please see below, I add 120 pixels in this picture: Take a look at the color distribution at this time you will know why I say it "rude".
Grayscale channel: red channel:
Green channel: Blue channel: It can be seen that the color information on the high brightness part on the original image is all lost, and the low brightness section (0-120) is a blank, if the original picture is 2 ^ 8 × 2 ^ 8 × 2 ^ 8 a total of 1677 million colors, then the present picture is: (256-120) ^ 3 = 251 million colors, that is, through our 120 points, we lost a big half Color information. (Selecting plus 120 lightening is to make the overall brightness of the picture and the brightness of the histogram balance, easy to compare.) So, what method can be made without loss, or lose very little color information, it is a picture. What is displayed? Yes, this is the method of "grayscale histogram balance" today. Let's take a look at the effect: The following is the effect and the previous comparison of the original picture by the method of grayscale histogram.
Original image: Improve the brightness 120:
Grayscale histogram balance: The color distribution after the equalization equilibrium is as follows:
Grayscale channel: red channel:
Green Channel: Blue Channel: By comparison, we can find that the grayscale histogram balance is a distribution of color distribution in the original image in accordance with the frequency. The most color "division" will appear, and few colors "squeezes" are tighter, so the benefit is that the subjects seen in our eyes are more distinct. Ok, about the effect, it has already been said, let's explain how this algorithm is implemented. First, we need to get the color distribution statistics of all pixels in the picture to be processed, that is, the above channels.
Suppose there is a picture (we use grayscale directly): 100 50 2020 40 50 100 250 200 Meeting: 20: 2 40: 1 50: 2 100: 2 200: 1 250: 1 This picture has 9 Pixel, we use proportion to the ratio of each color: 20: 2/9 40: 1/9 50: 2/9 100: 2/9 200: 1/9 250: 1/9 Due to all colors The number of times is impossible to exceed the total pixel of the picture, and therefore, the proportion of all colors will not exceed 1 (you can see that it is exactly 1), in order to put the proportion of each color according to the low to high order Weighted statistics, that is, the current "right" is equal to the original proportion of this point plus the "right" of the previous point, we get a new statistics: 20: 2/9 40: 3/9 50: 5 / 9 100: 7/9 200: 8/9 250: 9/9 Finally, according to this new statistics, let's replace the brightness of the pixel in a new brightness, the algorithm is: new brightness = this point "right" × 255 20: 2/9 >> 20 (the first point is not moving, still use 20) 40: 3/9 × 255 = 85 50: 5/9 × 255 = 141 100: 7/9 × 255 = 198 200: 8/9 × 255 = 226 250: 9/9 × 255 = 255 we got new map: 100 5 0 20 198 141 2020 40 50 >> 10 85 141100 250 200 198 255 226 The width of the relative frequency in the original map has become large. There is a small portion that occurs. Therefore, the role of grayscale histogram balancing is to put a lot of color expansion on a picture, and less color compression. Thereby, a more "balanced" color distribution is obtained.
Below my routine: Private Type Colorchart Colorcount (255) As long 'Statistics The brightness of the original picture Pixcelcount as long' records Pixels of the picture Colratio (255) AS Single 'records the proportion of each brightness NewVal (255) As Byte 'to store the new brightness index End TypeDim ColChart As ColorChartPublic Sub StatisticsChart () Dim R As ByteDim G As ByteDim B As ByteDim Gray As IntegerDim X As LongDim Y As LongDim I As LongDim L As LongDim M As LongDim C As Doubleon Error Goto ErrlineDone = falsetimefilter = timegettime with colchart for x = 0 to 255 'First put the array clear. COLORCOUNT (X) = 0 Next for x = 0 to outputwid' These two loops used to scan the picture data, record each Gray and appearance of a 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 'Get the total amount of pixels of the picture C = 1 / .pixcelcount .colratio (0) = .Colorcount (m, 0) * c 'calculates each The proportion of brightness .NewVal (0) = 0 'color value minimal color always 0, does not participate in calculation L = 0 for i = 1 to 255 .Colratio (i) = .colorcount (i) * c .colratio (L) 'for weighting .NewVal (i) = .colratio (i) * 255' Calculate new color index L = L 1 Next
For x = 0 to outputwid for y = 0 to outputhei r = colval (2, x, y) 'Read the color g = colval (1, x, y) b = colval (0, x, y) r = .Newval (r) 'Checks table gets new color g = .newval (g) b = .newval (b) colut (2, x, y) = r' puts new color in output array COLOUT (1 , Y) = g colut (0, x, y) = b Next Next end withdone = truetimefilter = timegettime - timefilterexit suberrline: DONE = TruemsgBox err.descriptionnd Sub If the reader is unclear, Please refer to the previous articles in front, which describes the detailed description: VB image processing, (1) acquisition and output of pixels
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)
The next article will continue to tell the color contrast and brightness adjustment algorithm, and the color picture to the grayscale image. Don't think this is very simple, there is still some small problems in the middle.
(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.)