Skip to content

Commit

Permalink
BROKEN experimental canny and hough NOT YET WORKING.
Browse files Browse the repository at this point in the history
  • Loading branch information
mudasobwa committed Oct 23, 2014
1 parent 30492e8 commit ad22fd5
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
19 changes: 19 additions & 0 deletions bin/magick_canny
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ options.highThreshold = 7.5
options.kernelRadius = 2.0
options.kernelWidth = 16
options.color = 'black'
options.roughly = true
options.hough = false
options.contrastNormalized = false
options.logger = nil

Expand Down Expand Up @@ -63,6 +65,16 @@ OptionParser.new do |opts|
options.contrastNormalized = !!contrastNormalized
end

# Thoroughly
opts.on("-u", "--thoroughly", "If set, the image will not be grayscaled before processing (requires less resources, default: false)") do |thoroughly|
options.roughly = !thoroughly
end

# Thoroughly
opts.on("-y", "--hough", "If set, the hough matrix will be built and an image will be rotated accordingly, default: false)") do |hough|
options.hough = hough
end


# Verbose?
opts.on("-v", "--verbose", "Run verbosely (default: false)") do |v|
Expand All @@ -88,3 +100,10 @@ outfile = File.basename(file).sub(/(\.\w+)$/, "-canny\\1")
Magick::Screwdrivers::canny(file, options).write(File.join File.dirname(file), outfile) {
self.quality = options.quality || 90
}

if options.hough
houghfile = File.basename(file).sub(/(\.\w+)$/, "-hough\\1")
Magick::Screwdrivers::hough(file, {:roughly => options.roughly, :logger => options.logger}).write(File.join File.dirname(file), houghfile) {
self.quality = options.quality || 90
}
end
2 changes: 1 addition & 1 deletion bin/magick_sobel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ OptionParser.new do |opts|
end

# Thoroughly
opts.on("-r", "--roughly", "If set, the image will not be grayscaled before processing (requires less resources, default: false)") do |roughly|
opts.on("-u", "--roughly", "If set, the image will not be grayscaled before processing (requires less resources, default: false)") do |roughly|
options[:roughly] = !!roughly
end

Expand Down
1 change: 1 addition & 0 deletions lib/rmagick/screwdrivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "rmagick/screwdrivers/poster"
require "rmagick/screwdrivers/collage"
require "rmagick/screwdrivers/scale"
require "rmagick/screwdrivers/hough"
require "rmagick/screwdrivers/sobel"
require "rmagick/screwdrivers/canny"

Expand Down
9 changes: 6 additions & 3 deletions lib/rmagick/screwdrivers/canny.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

# http:# www.tomgibara.com/computer-vision/CannyEdgeDetector.java
# http://www.tomgibara.com/computer-vision/CannyEdgeDetector.java
# @author Tom Gibara
# ported from java Alexei Matyushkin

Expand All @@ -22,7 +22,7 @@ def self.gaussian x, sigma
end

def self.hypot x, y
return Math.hypot x, y # x.abs + y.abs
Math.hypot x, y # x.abs + y.abs
end

def self.normalizeContrast data
Expand Down Expand Up @@ -208,9 +208,12 @@ def self.canny file, options = OpenStruct.new
options.kernelWidth = 16 unless options.kernelWidth && options.kernelWidth >= 2
options.color ||= 'black'
options.contrastNormalized = !!options.contrastNormalized
options.roughly = !!options.roughly

begin
orig = img_from_file(file)
orig = orig.gaussian_blur 0, 3.0
orig = orig.quantize 256, Magick::GRAYColorspace unless options.roughly
rescue
warn(options.logger, "Skipping invalid file #{file}…")
return nil
Expand All @@ -231,7 +234,7 @@ def self.canny file, options = OpenStruct.new

magnitude = Canny::computeGradients options.kernelRadius, options.kernelWidth, width, height, data

data = (0...width*height).map { 0.0 }
data = (0...width*height).map { 0 }
offset = 0
for y in 0...height
for x in 0...width
Expand Down
108 changes: 108 additions & 0 deletions lib/rmagick/screwdrivers/hough.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# encoding: utf-8

require 'RMagick'

module Magick
module Screwdrivers
module Hough
def self.is_dark? px
px.red + px.green + px.blue < 40
end

def self.is_light? px
px.red + px.green + px.blue > 600
end
end

# based on http://jonathan-jackson.net/ruby-hough-hack.html
def self.hough file, options={}
options = {
:roughly => false,
:logger => nil
}.merge(options)

begin
orig = img_from_file(file)
# orig = orig.gaussian_blur 0, 3.0
orig = orig.quantize 256, Magick::GRAYColorspace unless options[:roughly]
rescue
warn(options[:logger], "Skipping invalid file #{file}…")
return nil
end

trigons = { :cos => [], :sin => [] }
hough = Hash.new(0)
(orig.rows - 1).times do |y|
orig.columns.times do |x|
if Hough::is_dark?(orig.pixel_color(x,y)) && Hough::is_light?(orig.pixel_color(x,y + 1))
(0..Math::PI).step(0.1).each do |theta|
trigons[:cos][theta] ||= Math.cos(theta)
trigons[:sin][theta] ||= Math.sin(theta)
distance = (x * trigons[:cos][theta] + y * trigons[:sin][theta]).to_i
hough[[theta, distance]] += 1 if distance >= 0
end
end
end
end

info options[:logger], 'Will print first 200 results:'
hough.sort_by { |k,v| v }.take(200).each { |v|
info options[:logger], v
}
at = hough.sort_by { |k,v| v }.inject(0.0) { |m,v| m + v[0][0] } / 20
info options[:logger], "Average theta: #{at}"

orig.rotate at
end

# based on http://rosettacode.org/wiki/Hough_transform#Ruby
def self.hough_transform file, options={}
options = {
:roughly => false,
:logger => nil
}.merge(options)

begin
orig = img_from_file(file)
# orig = orig.gaussian_blur 0, 3.0
orig = orig.quantize 256, Magick::GRAYColorspace unless options[:roughly]
rescue
warn(options[:logger], "Skipping invalid file #{file}…")
return nil
end

mx, my = orig.columns * 0.5, orig.rows * 0.5
max_d = Math.sqrt(mx * mx + my * my)
min_d = -max_d
hough = Hash.new(0)
(0...orig.columns).each do |x|
warn(options[:logger], "#{x} of #{orig.columns}")
(0...orig.rows).each do |y|
if orig.pixel_color(x,y).green > 32
(0...180).each do |a|
rad = a * (Math::PI / 180.0)
d = (x - mx) * Math.cos(rad) + (y - my) * Math.sin(rad)
hough["#{a.to_i}_#{d.to_i}"] = hough["#{a.to_i}_#{d.to_i}"] + 1
end
end
end
end

max = hough.values.max

houghed = Image.new(orig.columns, orig.rows) {
self.background_color = 'transparent'
}

hough.each_pair do |k, v|
a, d = k.split('_').map(&:to_i)
c = (v / max) * 255
# c = heat.get_pixel(c,0)
houghed.pixel_color a, max_d + d, Pixel.new(c, c, c)
end

houghed
end

end
end
1 change: 1 addition & 0 deletions lib/rmagick/screwdrivers/sobel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.sobel file, options={}

begin
orig = img_from_file(file)
# orig = orig.gaussian_blur 0, 3.0
orig = orig.quantize 256, Magick::GRAYColorspace unless options[:roughly]
rescue
warn(options[:logger], "Skipping invalid file #{file}…")
Expand Down

0 comments on commit ad22fd5

Please sign in to comment.