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 'matrix'
require 'edge_detect/version' require 'edge_detect/version'
require 'edge_detect/nearby'
require 'edge_detect/matrix' require 'edge_detect/matrix'
require 'edge_detect/gray_scaler' require 'edge_detect/gray_scaler'
require 'edge_detect/differ' require 'edge_detect/differ'
@ -12,6 +13,6 @@ require 'edge_detect/sobel_edge_detector'
## ##
# EdgeDetection module # EdgeDetection module
# #
module EdgeDetect module EdgeDetect
end end

View File

@ -1,14 +1,15 @@
module EdgeDetect 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. # the horizontal and vertical differ.
# #
class Differ class Differ
include Nearby
## ##
# Initializes a new differ with a given image # Initializes a new differ with a given image
# #
# @param image [ChunkyPNG::Image] the image that should be diffed # @param image [ChunkyPNG::Image] the image that should be diffed
# #
def initialize(image) def initialize(image)
@ -16,33 +17,14 @@ module EdgeDetect
@gray_scaler = GrayScaler.new(image) @gray_scaler = GrayScaler.new(image)
end end
## ##
# Should return the diffed value of the given pixel. # Should return the diffed value of the given pixel.
# If the pixel is out of bounds 0 should be returned. # If the pixel is out of bounds 0 should be returned.
# #
# @return [Integer] a value between 0 and 255 representing a grayscale value. # @return [Integer] a value between 0 and 255 representing a grayscale value.
# #
def [](x, y) def [](x, y)
raise NotImplementedError raise NotImplementedError
end 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
end end

View File

@ -1,10 +1,10 @@
module EdgeDetect module EdgeDetect
## ##
# Grayscales a given image # Grayscales a given image
# #
class GrayScaler class GrayScaler
include Nearby
## ##
# Initializes a new GrayScaler with the given image # Initializes a new GrayScaler with the given image
# #
# @param image [ChunkyPNG::Image] the image that should be grayscaled. # @param image [ChunkyPNG::Image] the image that should be grayscaled.
@ -13,8 +13,8 @@ module EdgeDetect
@image = image @image = image
end 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. # If the coordinate is out of bounds 0 will be returned.
# #
# @return [Integer] a value between 0 and 255. # @return [Integer] a value between 0 and 255.
@ -26,24 +26,5 @@ module EdgeDetect
0 0
end end
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
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. # Edge detects the image by applying the sobel operations.
# #
# The original image will not be changed by this operation. # The original image will not be changed by this operation.
# #
# @return [ChunkyPNG::Image] the edge detected image. # @return [ChunkyPNG::Image] the edge detected image.
# #
def detect_edges def detect_edges
@ -31,7 +31,7 @@ module EdgeDetect
private private
## ##
# The sobel matrix to transform horizontal pixels # The sobel matrix to transform horizontal pixels
# #
# @return [Matrix] the sobel matrix # @return [Matrix] the sobel matrix
@ -44,7 +44,7 @@ module EdgeDetect
] ]
end end
## ##
# The sobel matrix to transform vertical pixels # The sobel matrix to transform vertical pixels
# #
# @return [Matrix] the sobel matrix # @return [Matrix] the sobel matrix