diff --git a/lib/edge_detect.rb b/lib/edge_detect.rb index 8079cb3..9a4e8f9 100644 --- a/lib/edge_detect.rb +++ b/lib/edge_detect.rb @@ -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 \ No newline at end of file +end diff --git a/lib/edge_detect/differ.rb b/lib/edge_detect/differ.rb index 09f9faa..a49711b 100644 --- a/lib/edge_detect/differ.rb +++ b/lib/edge_detect/differ.rb @@ -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>] 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 diff --git a/lib/edge_detect/gray_scaler.rb b/lib/edge_detect/gray_scaler.rb index 41933a0..5b04537 100644 --- a/lib/edge_detect/gray_scaler.rb +++ b/lib/edge_detect/gray_scaler.rb @@ -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>] 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 diff --git a/lib/edge_detect/nearby.rb b/lib/edge_detect/nearby.rb new file mode 100644 index 0000000..cd0c6b3 --- /dev/null +++ b/lib/edge_detect/nearby.rb @@ -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>] 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 diff --git a/lib/edge_detect/sobel_edge_detector.rb b/lib/edge_detect/sobel_edge_detector.rb index 34f3895..d1de255 100644 --- a/lib/edge_detect/sobel_edge_detector.rb +++ b/lib/edge_detect/sobel_edge_detector.rb @@ -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