Image filter algorithm code implemented by ruby

Time：2021-11-14

Original drawing

1、 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 different, so the color value of the pixel can also have many color values, which is colorpictureThe general processing method is to set the RGB three channel values of the picture color value to the same, so that the display effect of the picture will be gray.

There are generally three algorithms for gray processing:

1. Maximum value method: that is, the new color value r = g = b = max (R, G, b). The brightness value of the processed picture looks high.
2. Average value method: that is, the new color value r = g = b = (R + G + b) / 3. The processed picture is very soft
3. Weighted average method: that is, the new color value r = g = b = (R * WR + G * WG + b * WB). Generally, due to the different sensitivity of human eyes to different colors, the weights of the three color values are different. Generally speaking, 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%

The following is an example of the weighted average methodRubyrealization:

 1 2 3 4 5 6 7 8 9 10 11 `#Grayscale picture` `#Take the average value of RGB three colors` `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:

2、 Binarization

Image binarization is to set the gray value of pixels on the image to 0 or 255, that is, the whole image presents an obvious black-and-white effect. All pixels with grayscale greater than or equal to the threshold are determined to belong to a specific object, and their grayscale value is 255. Otherwise, these pixels are excluded from the object area, and the grayscale value is 0, indicating the background or exceptional object area.

Image binarization is often used in image recognition applications such as cracking verification codes

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 `#Binary picture` ` ``#If it is less than a certain threshold, it is set to 0, and if it is greater than 255, it is set to 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

Binarization effect

3、 Negative film

The realization of negative effect is very simple, that is, reverse each channel value of RGB, that is, subtract with 255

 1 2 3 4 5 6 7 8 9 10 `#Negative picture` ` ``#RGB invert 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

4、 Relief effect

The embossing algorithm is relatively complex. Subtract the RGB value of adjacent points from the RGB value of the current point and add 128 as the new RGB value. Because the color values of adjacent points in the picture are relatively close, after such algorithm processing, the results will be more obvious only in the edge area of the color, that is, the part with large adjacent color differences, while the values of other smooth areas are close to about 128, that is, gray
It has a relief effect.

In the actual effect, after such processing, some areas may still have some “color” points or strip traces, so it is best to do a gray processing for the new RGB value.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 `#Relief effect` ` ``#The embossing algorithm is relatively complex. Subtract the RGB value of adjacent points from the RGB value of the current point and add 128 as the new RGB value. Because the color values of adjacent points in the picture are relatively close,` ` ``#Therefore, after such algorithm processing, only the edge region of color, that is, the part with large adjacent color difference, will have obvious results, while the values of other smooth regions are close to about 128,` ` ``#That is, gray, which has the effect of relief.` ` ``#In the actual effect, after such processing, some areas may still have some "color" points or strip traces, so it is best to do a gray processing for the new RGB value.` ` ``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`

Relief effect

Project Home

Simplify ajax development with jquery

Learn the basic principles of jQuery, explore its features and functions, perform some common Ajax tasks, and master how to extend jQuery using plug-ins.What is jQuery?JQuery was created by John Resig in early 2006. It is a very useful JavaScript library for any programmer using JavaScript code. Whether you are new to the JavaScript language […]