Ruby-implemented image filter algorithm code, ruby-implemented Filter Algorithm

Source: Internet
Author: User
Tags image filter scale image

Ruby-implemented image filter algorithm code, ruby-implemented Filter Algorithm

Source image


I. grayscale Algorithms

The color values of each pixel in a color photo are composed of three values: Red, green, and blue. The values of red, green, and blue are different. Therefore, the color values of pixels can also contain many color values, this is the principle of a color image, while a gray-scale image has only 256 colors. The general solution is to set the RGB channel values of the image to the same value, in this way, the image is displayed in gray.

There are three algorithms for Grayscale processing:

  1. Maximum value method: the new color value R = G = B = Max (R, G, B). The Brightness Value of the processed image is high.
  2. Average Method: the new color value R = G = B = (R + G + B)/3. The processed image is very soft.
  3. Weighted average value method: the new color value R = G = B = (R * Wr + G * Wg + B * Wb). Generally, because the human eyes have different sensitivity to different colors, therefore, the weights of the three color values are different. Generally, the highest value is green, the second value is red, and the lowest value is blue. The most reasonable values are Wr = 30%, Wg = 59%, and Wb = 11%.

The following is the Ruby Implementation of the weighted average method:

# Grayscale image # obtain the RGB three-color average def self. gray (bmp) for I in 0 .. bmp. height-1 for j in 0 .. bmp. width-1 rgb = bmp. getRGB (I, j) gray = 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 (gray, gray, gray) end

Grayscale effect:


Ii. binarization

Image binarization is to set the gray value of the pixel on the image to 0 or 255, that is, to display the entire image with a clear black and white effect. All pixels whose gray scale is greater than or equal to the threshold value are determined to belong to a specific object, and their gray scale value is 255. Otherwise, these pixels are excluded from the object area and their gray scale value is 0, indicates the background or the area of the exception object.

Image binarization is often used to crack verification codes and other image recognition applications.

# Binarization image # set to 0 0 if it is less than a certain threshold, and to 255 255 255 def self if it is greater than the threshold. 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

Binarization Effect


Iii. Negative Film

The implementation of the negative effect is very simple, that is, to reverse each channel value of RGB, that is, to use 255 to subtract

# Negative image # reverse color of RGB 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

Negative Film Effect


Iv. Relief Effect

The embossed algorithm is relatively more complex. The RGB value of the current vertex is used to subtract the RGB value of the adjacent vertex and add 128 as the new RGB value. Because the color values of adjacent points in the image are relatively close, after such an algorithm is processed, only the edge areas of the color are displayed, that is to say, the result of the large difference between adjacent colors is obvious, while the value of other smooth areas is close to about 128, that is, Gray.
It has the relief effect.

In actual results, after such processing, some areas may still have "colored" vertices or strip traces, so it is best to perform a grayscale processing on the new RGB value.

# Relief effect # The embossed algorithm is relatively complicated. Use the RGB value of the current vertex to subtract the RGB value of the adjacent vertex and add 128 as the new RGB value. Because the color values of adjacent points in the image are relatively close, # after such an algorithm is processed, only the Edge Area of the color is available, that is to say, the result of the large difference between adjacent colors is more obvious, while the value of other smooth areas is close to about 128, # is gray, which has a relief effect. # In actual results, after such processing, some areas may still have "colored" vertices or strip traces, so it is best to perform a grayscale processing on 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 + 128g = (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

Embossed Effect


Project Homepage

Geekeren/RubyImageProcess

Related Article

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.