refactored the nearby method into it's own module

This commit is contained in:
Tim Kächele 2016-05-31 18:05:18 +02:00
parent 5b7c6f8be0
commit 42fb9be4ef
5 changed files with 46 additions and 56 deletions

View File

@ -2,6 +2,7 @@ require 'chunky_png'
require 'matrix'
require 'edge_detect/version'
require 'edge_detect/nearby'
require 'edge_detect/matrix'
require 'edge_detect/gray_scaler'
require 'edge_detect/differ'
@ -12,6 +13,6 @@ require 'edge_detect/sobel_edge_detector'
##
# EdgeDetection module
#
#
module EdgeDetect
end
end

View File

@ -1,14 +1,15 @@
module EdgeDetect
##
# Abstract base class that provides the basis for both,
##
# Abstract base class that provides the basis for both,
# the horizontal and vertical differ.
#
class Differ
include Nearby
##
##
# Initializes a new differ with a given image
#
#
# @param image [ChunkyPNG::Image] the image that should be diffed
#
def initialize(image)
@ -16,33 +17,14 @@ module EdgeDetect
@gray_scaler = GrayScaler.new(image)
end
##
##
# Should return the diffed value of the given pixel.
# If the pixel is out of bounds 0 should be returned.
#
#
# @return [Integer] a value between 0 and 255 representing a grayscale value.
#
def [](x, y)
raise NotImplementedError
end
##
# Returns the nearby diffs of the given coordinates (x, y) with respect to
# the given radius.
#
# @return [Array<Array<Integer>>] a two dimensional array with the diffs.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end

View File

@ -1,10 +1,10 @@
module EdgeDetect
##
# Grayscales a given image
#
#
class GrayScaler
##
include Nearby
##
# Initializes a new GrayScaler with the given image
#
# @param image [ChunkyPNG::Image] the image that should be grayscaled.
@ -13,8 +13,8 @@ module EdgeDetect
@image = image
end
##
# Returns the grayscale value of the given coordinate.
##
# Returns the grayscale value of the given coordinate.
# If the coordinate is out of bounds 0 will be returned.
#
# @return [Integer] a value between 0 and 255.
@ -26,24 +26,5 @@ module EdgeDetect
0
end
end
##
# Returns the nearby grayscale values of the given coordinates (x, y) with
# respect to the given radius.
#
# @return [Array<Array<Integer>>] a two dimensional array with the grayscale values.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end

26
lib/edge_detect/nearby.rb Normal file
View File

@ -0,0 +1,26 @@
module EdgeDetect
##
# The nearby module provides a method that uses the [x, y] method
# to query a two dimensional array.
#
module Nearby
##
# Returns the nearby values of the given coordinates (x, y) with respect to
# the given radius.
#
# @return [Array<Array<Value>>] a two dimensional array with the Values.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end

View File

@ -7,9 +7,9 @@ module EdgeDetect
##
# Edge detects the image by applying the sobel operations.
#
#
# The original image will not be changed by this operation.
#
#
# @return [ChunkyPNG::Image] the edge detected image.
#
def detect_edges
@ -31,7 +31,7 @@ module EdgeDetect
private
##
##
# The sobel matrix to transform horizontal pixels
#
# @return [Matrix] the sobel matrix
@ -44,7 +44,7 @@ module EdgeDetect
]
end
##
##
# The sobel matrix to transform vertical pixels
#
# @return [Matrix] the sobel matrix