added all the documentation

This commit is contained in:
Tim Kächele 2015-08-15 09:26:51 +02:00
parent a72b36cc20
commit 26bffa48b6
10 changed files with 124 additions and 13 deletions

View File

@ -77,5 +77,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/timkae
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
The gem is available as open source under the terms of the GNU GPL v2.

View File

@ -9,3 +9,9 @@ require 'edge_detect/horizontal_differ'
require 'edge_detect/vertical_differ'
require 'edge_detect/edge_detector'
require 'edge_detect/sobel_edge_detector'
##
# EdgeDetection module
#
module EdgeDetect
end

View File

@ -1,16 +1,37 @@
module EdgeDetect
##
# Abstract base class that provides the basis for both,
# the horizontal and vertical differ.
#
class Differ
##
# Initializes a new differ with a given image
#
# @param image [ChunkyPNG::Image] the image that should be diffed
#
def initialize(image)
@image = image
@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|

View File

@ -2,14 +2,33 @@ require 'matrix'
module EdgeDetect
##
# Naive EdgeDetect Implementation
#
class EdgeDetector
##
# The original image
#
attr_reader :image
##
# Initializes a new EdgeDetector with the given image.
#
# @param image [ChunkyPNG::Image] the image that should be edge detected.
#
def initialize(image)
@image = image
end
##
# Edge detects the image by building the horizontal and vertical diffs.
# (naive approach)
#
# The original image will not be changed by this operation.
#
# @return [ChunkyPNG::Image] the edge detected image.
#
def detect_edges
image = ChunkyPNG::Image.new(@image.width, @image.height, ChunkyPNG::Color::TRANSPARENT)
h_diff = HorizontalDiffer.new(self.image)

View File

@ -1,10 +1,24 @@
module EdgeDetect
##
# Grayscales a given image
#
class GrayScaler
##
# Initializes a new GrayScaler with the given image
#
# @param image [ChunkyPNG::Image] the image that should be grayscaled.
#
def initialize(image)
@image = image
end
##
# 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.
#
def [](x, y)
begin
(ChunkyPNG::Color.to_grayscale(@image[x, y]) & 0x0000ff00) >> 8
@ -13,6 +27,12 @@ module EdgeDetect
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|

View File

@ -1,8 +1,19 @@
module EdgeDetect
# Simple Datastructure to hold horizontal and vertical diffs as one object
##
# Diffs the image horizontal.
#
class HorizontalDiffer < Differ
##
# Calculates the diff of the the given coordinate relative to the
# above pixel in the same column.
#
# The calculation is operated on the gray scale value of pixel
# If the given coordinate is out of bounds 0 (black) will be returned.
#
# @return [Integer] a value between 0 and 255
#
def [](x, y)
begin
@gray_scaler[x, y] - @gray_scaler[x, y - 1]

View File

@ -1,5 +1,17 @@
module EdgeDetect
##
# Sobel EdgeDetect Implementation
#
class SobelEdgeDetector < EdgeDetector
##
# 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
image = ChunkyPNG::Image.new(@image.width, @image.height, ChunkyPNG::Color::TRANSPARENT)
gray = GrayScaler.new(self.image)
@ -19,6 +31,11 @@ module EdgeDetect
private
##
# The sobel matrix to transform horizontal pixels
#
# @return [Matrix] the sobel matrix
#
def sobel_x
@sobel_x ||= Matrix[
[1, 0, -1],
@ -27,6 +44,11 @@ module EdgeDetect
]
end
##
# The sobel matrix to transform vertical pixels
#
# @return [Matrix] the sobel matrix
#
def sobel_y
@sobel_y ||= Matrix[
[1, 2, 1],

View File

@ -1,3 +1,6 @@
module EdgeDetect
##
# The version of the gem
#
VERSION = "0.1.0"
end

View File

@ -1,8 +1,19 @@
module EdgeDetect
# Simple Datastructure to hold horizontal and vertical diffs as one object
##
# Diffs the image vertical.
#
class VerticalDiffer < Differ
##
# Calculates the diff of the the given coordinate relative to the
# above pixel in the same column.
#
# The calculation is operated on the gray scale value of pixel.
# If the given coordinate is out of bounds 0 (black) will be returned.
#
# @return [Integer] a value between 0 and 255
#
def [](x, y)
begin
@gray_scaler[x, y] - @gray_scaler[x - 1, y]

View File

@ -3,7 +3,7 @@ require 'test_helper'
class EdgeDetectorTest < Minitest::Test
def test_vertical_line
def test_airplane
input = File.expand_path('../../pictures/airplane.png', __FILE__)
image = ChunkyPNG::Image.from_file(input)
@ -17,7 +17,7 @@ class EdgeDetectorTest < Minitest::Test
sobel.detect_edges.save(output_sobel)
end
def test_horizontal_line
def test_robot
input = File.expand_path('../../pictures/robot.png', __FILE__)
image = ChunkyPNG::Image.from_file(input)
@ -31,6 +31,4 @@ class EdgeDetectorTest < Minitest::Test
sobel.detect_edges.save(output_sobel)
end
end