lib/sqed/boundary_finder.rb in sqed-0.1.2 vs lib/sqed/boundary_finder.rb in sqed-0.1.3
- old
+ new
@@ -23,10 +23,35 @@
# Returns a Sqed::Boundaries instance initialized to the number of sections in the passed layout.
def boundaries
@boundaries ||= Sqed::Boundaries.new(@layout)
end
+
+ # return [Integer, nil]
+ # sample more with small images, less with large images
+ # we want to return larger numbers (= faster sampling)
+ #
+ #
+ def self.get_subdivision_size(image_width)
+ case image_width
+ when nil
+ nil
+ when 0..140
+ 6
+ when 141..640
+ 12
+ when 641..1000
+ 16
+ when 1001..3000
+ 60
+ when 3001..6400
+ 80
+ else
+ 140
+ end
+ end
+
# @return
# the column (x position) in the middle of the single green vertical line dividing the stage
#
# @param image
# the image to sample
@@ -41,20 +66,23 @@
# - when nil the cutoff defaults to the maximum of the pairwise difference between hit counts
#
# @param scan
# (:rows|:columns), :rows finds vertical borders, :columns finds horizontal borders
#
- def self.color_boundary_finder(image: image, sample_subdivision_size: 10, sample_cutoff_factor: nil, scan: :rows, boundary_color: :green)
+ def self.color_boundary_finder(image: image, sample_subdivision_size: nil, sample_cutoff_factor: nil, scan: :rows, boundary_color: :green)
+ image_width = image.send(scan)
+ sample_subdivision_size = get_subdivision_size(image_width) if sample_subdivision_size.nil?
+ samples_to_take = (image_width / sample_subdivision_size).to_i - 1
+
border_hits = {}
- samples_to_take = (image.send(scan) / sample_subdivision_size).to_i - 1
(0..samples_to_take).each do |s|
# Create a sample image a single pixel tall
if scan == :rows
- j = image.crop(0, s * sample_subdivision_size, image.columns, 1)
+ j = image.crop(0, s * sample_subdivision_size, image.columns, 1, true)
elsif scan == :columns
- j = image.crop(s * sample_subdivision_size, 0, 1, image.rows)
+ j = image.crop(s * sample_subdivision_size, 0, 1, image.rows, true)
else
raise
end
j.each_pixel do |pixel, c, r|
@@ -99,11 +127,11 @@
def self.is_black?(pixel)
black_threshold = 65535*0.15 #tune for black
(pixel.red < black_threshold) && (pixel.blue < black_threshold) && (pixel.green < black_threshold)
end
- # Takes a frequency hash of position => count key/values and returns
- # the median position of all positions that have a count greater than the cutoff
+ # return [Array]
+ # the median position of all (pixel) positions that have a count greater than the cutoff
def self.frequency_stats(frequency_hash, sample_cutoff = 0)
return nil if sample_cutoff.nil? || sample_cutoff < 1
hit_ranges = []
frequency_hash.each do |position, count|