4 years ago (2016-12-30)  Algorithm language |   First to comment  58 
post score 0 times, average 0.0

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:


Grayscale effect:

Ruby image processing basic algorithm (two) binary, grayscale, embossed...


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.


Binary effect

Ruby image processing basic algorithm (two) binary, grayscale, embossed...


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.


Negative effect

Ruby image processing basic algorithm (two) binary, grayscale, embossed...


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.


Emboss effect

Ruby image processing basic algorithm (two) binary, grayscale, embossed...


Project home page



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

Post comment


No Comment


Forget password?