First, the gray algorithm
The color value of each pixel in a color photo is a mixture of red, green, and blue values. The values of red, green, and blue are made up of a wide variety of colors, and the color values of pixels can also have a variety of color values. This is the color picture. The principle, but the grayscale photograph only has 256 kinds of colors, the general processing method is to set the RGB three channel values of the picture color value as the same, so the display effect of the picture will be gray. Grayscale processing generally has three algorithms:
- 1 The maximum value method: that is, the new color value R = G = B = Max (R, G, B), this method of processing the picture looks high brightness value.
- 2 means: the new color value R = G = B = (R + G + B) / 3, so that the picture processed is very soft
- 3 weighted average method: that is, the new color value R = G = B = (R * Wr + G * Wg + B * Wb), generally because the human eye is different for different colors of sensitivity, so the three color values of the weight is not the same, In general, green is the highest, red is the second, and blue is the lowest. The most reasonable values are Wr = 30%, Wg = 59%, and Wb = 11% respectively.
Here is the Ruby implementation of the weighted average method:
1 2 3 4 5 6 7 8 9 10 11 |
# Grayscale pictures # Take RGB three-color average Def self.grey(bmp) For i in 0 .. Bmp.height - 1 For j in 0 .. Bmp.width - 1 Rgb = bmp.getRGB(i, j) Grey = rgb.r.to_f * 0.3+rgb.g.to_f *0.59 +rgb.b.to_f * 0.11.to_i bmp.setRGB(i, j, RGB.new(grey, grey, grey)) End End End |
Grayscale effect:

Second, binary
Binarization of the image is to set the gray value of the pixel on the image to 0 or 255, which means that the entire image exhibits a clear black and white effect.All pixels whose gray levels are greater than or equal to the threshold are determined to belong to a specific object. The gray value is represented by 255. Otherwise, these pixels are excluded from the object area, and the gray value is 0, indicating a background or an exceptional object area. Image binarization is often used to crack verification codes and other image recognition applications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Binary pictures # less than a certain threshold is set to 0 0 0, greater than the setting is 255 255 255 Def self.binarization(bmp) imageGreyLevel = bmp.getGreyLevel For i in 0 .. Bmp.height - 1 For j in 0 .. Bmp.width - 1 Rgb = bmp.getRGB(i, j) If rgb.getGreyLevel<imageGreyLevel bmp.setRGB(i, j, RGB.new(0, 0, 0)) Else bmp.setRGB(i, j, RGB.new(255, 255, 255)) End End End End |
Binary effect

Third, the film
The effect of the negative film is very simple. It is to reverse every channel value of RGB, that is, to reduce it by 255.
1 2 3 4 5 6 7 8 9 10 |
#Reversal picture #RGB Invert color 255- Def self.contraryColor(bmp) For i in 0 .. Bmp.height - 1 For j in 0 .. Bmp.width - 1 Rgb = bmp.getRGB(i, j) bmp.setRGB(i, j, rgb.getContrary) End End End |
Negative effect

Fourth, embossed effect
The algorithm for embossing is relatively complex, subtracting the RGB value of the neighboring point from the current point's RGB value and adding 128 as the new RGB value.Because the color values of the neighboring points in the picture are relatively close, after such an algorithm is processed, only the edge of the color, that is, the result of the part where the adjacent color difference is relatively large will be more obvious, and the other smooth area is the value. All are close to 128 or so, that is, gray, so it has an embossed effect. In the actual effect, after such processing, some areas may still have some "color" points or stripes, so it is better to do a grayscale processing of the new RGB values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Emboss effect The algorithm for embossing is relatively complicated. Use the RGB value of the current point to subtract the RGB value of the neighboring point and add 128 as the new RGB value.Because the color values of the neighboring points in the picture are close, Therefore, after such an algorithm is processed, only the edge of the color, that is, the part where the adjacent color difference is relatively large, will be more significant, while the other smooth areas will be close to 128 or so. # is also gray, so it has an embossed effect. # In the actual effect, after such processing, some areas may still have some "color" points or stripes, so it is better to do a grayscale processing of the new RGB values. Def self.emboss(bmp) preRGB=RGB.new(128, 128, 128) For i in 0 .. Bmp.height - 1 For j in 0 .. Bmp.width - 1 currentRGB=bmp.getRGB(i, j) r=(currentRGB.r-preRGB.r)*1+128 g=(currentRGB.g - preRGB.g)*1+128 b=(currentRGB.b-preRGB.b)*1+128 bmp.setRGB(i, j, RGB.new(r,g,b).getGreyRGB) preRGB = currentRGB End End End |
Emboss effect

Project home page
Geekeren/RubyImageProcess
This article has been printed on copyright and is protected by copyright laws. It must not be reproduced without permission.If you need to reprint, please contact the author or visit the copyright to obtain the authorization. If you feel that this article is useful to you, you can click the "Sponsoring Author" below to call the author!
Reprinted Note Source: Baiyuan's Blog>>https://wangbaiyuan.cn/en/ruby-basic-image-processing-algorithm-2-binary-grayscale-emboss-2.html
No Comment