Ruby implementation of picture filter algorithm code

Source: Internet
Author: User
Tags image filter
Filters are now used in our lives very widely, in the development we will also touch, this article to you to share is the use of ruby implementation of several image filter algorithms, including grayscale, binary, negative, emboss. Very simple and practical, the need for small partners can refer to the next.

Original


First, Gray algorithm

Color photos each pixel color value from the red, green, blue mixture of three values, the value of red and green blue is a lot of different, so the color value of the pixel can also have a lot of color values, this is the principle of color pictures, and grayscale photos only 256 colors, The usual way to do this is to set the RGB three channel value of the image color value to the same, so that the picture will be grayed out.

Grayscale processing generally has three kinds of algorithms:

    1. Maximum Value method: That is, the new color value R=g=b=max (r,g,b), this method after processing the image appears to be high brightness value.

    2. Average method: The new color value r=g=b= (r+g+b)/3, so the picture is very soft

    3. Weighted mean value method: That is, the new color value r=g=b= (R * wr+g*wg+b*wb), generally because the human eye to different color sensitivity, so the weight of the three color values are not the same, generally green highest, red next, blue lowest, the most reasonable value is Wr = 30%,WG = 59% , Wb = 11%

The following is a ruby implementation of the weighted average method:


#灰度化图片 #取RGB三色平均值 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:


Two or two value of

Image binary is to set the gray value of the pixel on the image to 0 or 255, that is, the entire image will show a significant black and white effect. All pixels with a grayscale greater than or equal to the threshold are judged to be of a particular object, with a grayscale value of 255, otherwise these pixels are excluded from the object area, with a grayscale value of 0, representing the background or exception of the object area.

Image binary is often used to crack the verification code and other image recognition applications

#二值化图片 #小于一定阈值设为0 0 0, greater than set to 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

The effect of binary value


Third, negative

The effect of the film is very simple, that is, the RGB of each channel value is reversed, is to use 255 to reduce

#底片化图片 #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 End End  

Negative effect


Four, relief effect

The emboss algorithm is relatively complex, subtracting the RGB value of the adjacent point from the RGB value of the current point and adding 128 as the new RGB value. Because the color value of the neighboring points in the picture is close, so after processing, only the color of the edge area, that is, the parts of the adjacent color difference is more obvious results, and the other smooth areas of the value is nearly 128, that is, gray, so
It has an embossed effect.

In the actual effect, after this processing, some areas may still have "color" some points or strip traces, so it is best to do a grayscale processing of the new RGB values.

#浮雕效果 #浮雕的算法相对复杂一些, subtract the RGB value of the adjacent point from the RGB value of the current point and add 128 as the new RGB value. Because the color value of the adjacent points in the picture is close, #因此这样的算法 processing, only the edge area of the color, that is, the parts of the adjacent color difference is more obvious, and the other smooth areas of the value is nearly 128, #也就是灰色, so it has a relief effect. #在实际的效果中, after this processing, some areas may still have "color" some points or strip traces, so it is best to do a grayscale processing of the new RGB values. def self.emboss (BMP) Prergb=rgb.new (-----)--  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


The above content is about the ruby implementation of the image filter algorithm code, I hope to help you.

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.