Last active
January 20, 2019 22:27
-
-
Save niuage/13453edcd5b14bb59ac14a6e3671a354 to your computer and use it in GitHub Desktop.
Quick and dirty test - implementation of the sobel operator algo
This file contains hidden or 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
class Img | |
def initialize(img) | |
@img = img | |
@pixels = img.get_pixels(0, 0, img.columns, img.rows) | |
end | |
def at(x, y) | |
pos = x + y * @img.columns | |
@pixels[pos].red | |
end | |
end | |
img = Img.new(cat2) | |
class MyMatrix < Matrix | |
public :"[]=", :set_element, :set_component | |
end | |
edge = MyMatrix.build(cat2.columns, cat2.rows) { 0 } | |
(0..cat2.columns - 1).each do |x| | |
(0..cat2.rows - 1).each do |y| | |
if x == 0 || y == 0 || x > cat2.columns - 2 || y > cat2.rows - 2 | |
# edge[x, y] = [255, 255, 255].map { |a| a * 255 } | |
cat2.pixel_color(x, y, Magick::Pixel.new(255 * 255, 255* 255, 255* 255)) | |
next | |
end | |
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] = [val, val, val] | |
cat2.pixel_color(x, y, Magick::Pixel.new(val, val, val)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment