Tech Tips

  1. Uncategorized
  2. 216 view

[Ruby][ImageProcessing]Try to make environment for image processing

1. Install RVM

wget --no-check-certificate https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer

bash ./rvm-installer

source ~/.bashrc

ls -la ~/.rvm

rvm install ruby-1.9.3-p385

/bin/bash --login
rvm use ruby-1.9.3-p385 --default

# .bashrcに以下を追加
if [[ -s $HOME/.rvm/scripts/rvm ]] then
    source $HOME/.rvm/scripts/rvm
fi

rvm reload

ruby -v
gem -v

echo "gem: --no-rdoc --no-ri" >> ~/.gemrc

gem install bundler

2. Install libimlib2-ruby

sudo apt-get install libimlib2-ruby

3. Try to write a program which makes color image to gray scale image

参考:http://d.hatena.ne.jp/tohka383/20120201/1328037573
require 'imlib2'

img = Imlib2::Image.load("src/lena.jpg")

img.h.times do |y|
  img.w.times do |x|
    color = img.pixel(x, y)

    gray = (0.299 * color.r + 0.587 * color.g + 0.114 * color.b).to_i
    color.r = color.g = color.b = gray

    img.draw_pixel(x, y, color)
  end
end

img.save("dst/lena.jpg")
src
dst
It works 🙂

4. ラプラシアンフィルタをかけてみる

require 'imlib2'

img = Imlib2::Image.load("srcimg/lena.jpg")

img.h.times do |y|
    img.w.times do |x|
        color = img.pixel(x, y)

        gray = (0.299 * color.r + 0.587 * color.g + 0.114 * color.b).to_i
        color.r = color.g = color.b = gray

        img.draw_pixel(x, y, color)
    end
end

dstimg = Imlib2::Image.create(img.width, img.height)
laplacian_filter=[[1,1,1],[1,-8,1],[1,1,1]]
dstimg.h.times do |y|
    dstimg.w.times do |x|
        if x == 0 or x == dstimg.width
            next
        end
        if y == 0 or y == dstimg.height
            next
        end

        sum = 0
        for i in -1..1 do
            for j in -1..1 do
                color = img.pixel(x+j, y+i)
                sum += color.r * laplacian_filter[i+1][j+1]
            end
        end
        if sum < 0
            sum = 0
        end

        color = img.pixel(x,y)
        color.r = color.b = color.g = sum

        dstimg.draw_pixel(x, y, color)
    end
end

dstimg.save("dstimg/edge.jpg")
src
edge
Nice 🙂

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP