Created
April 20, 2011 07:02
-
-
Save jrobertson/930548 to your computer and use it in GitHub Desktop.
Edge detection with the Sobel operator in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chunky_png' | |
class ChunkyPNG::Image | |
def at(x,y) | |
ChunkyPNG::Color.to_grayscale_bytes(self[x,y]).first | |
end | |
end | |
img = ChunkyPNG::Image.from_file('engine.png') | |
sobel_x = [[-1,0,1], | |
[-2,0,2], | |
[-1,0,1]] | |
sobel_y = [[-1,-2,-1], | |
[0,0,0], | |
[1,2,1]] | |
edge = ChunkyPNG::Image.new(img.width, img.height, ChunkyPNG::Color::TRANSPARENT) | |
for x in 1..img.width-2 | |
for y in 1..img.height-2 | |
pixel_x = (sobel_x[0][0] * img.at(x-1,y-1)) + (sobel_x[0][1] * img.at(x,y-1)) + (sobel_x[0][2] * img.at(x+1,y-1)) + | |
(sobel_x[1][0] * img.at(x-1,y)) + (sobel_x[1][1] * img.at(x,y)) + (sobel_x[1][2] * img.at(x+1,y)) + | |
(sobel_x[2][0] * img.at(x-1,y+1)) + (sobel_x[2][1] * img.at(x,y+1)) + (sobel_x[2][2] * img.at(x+1,y+1)) | |
pixel_y = (sobel_y[0][0] * img.at(x-1,y-1)) + (sobel_y[0][1] * img.at(x,y-1)) + (sobel_y[0][2] * img.at(x+1,y-1)) + | |
(sobel_y[1][0] * img.at(x-1,y)) + (sobel_y[1][1] * img.at(x,y)) + (sobel_y[1][2] * img.at(x+1,y)) + | |
(sobel_y[2][0] * img.at(x-1,y+1)) + (sobel_y[2][1] * img.at(x,y+1)) + (sobel_y[2][2] * img.at(x+1,y+1)) | |
val = Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y)).ceil | |
edge[x,y] = ChunkyPNG::Color.grayscale(val) | |
end | |
end | |
edge.save('engine_edge.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment