Class: Wildfire::Converter::Analizer

Inherits:
Core
  • Object
show all
Defined in:
lib/wildfire/converter/analizer.rb

Constant Summary

Constants inherited from Core

Core::RED

Instance Method Summary collapse

Methods inherited from Core

#generate_path, #quick_save, #save, #temp_mat

Constructor Details

#initialize(big_cvmat) ⇒ Analizer



4
5
6
7
# File 'lib/wildfire/converter/analizer.rb', line 4

def initialize(big_cvmat)
  @big_cvmat = big_cvmat
  @resizer = Resizer.new(@big_cvmat)
end

Instance Method Details

#contoursObject



44
45
46
47
48
49
50
51
# File 'lib/wildfire/converter/analizer.rb', line 44

def contours
  greyed = Transformer.grey(@resizer.to_small)
  blurred = Transformer.blur(greyed)
  cannied = Transformer.canny(blurred)

  input_and_output = cannied.dup
  execute_contour_find(input_and_output)
end

#execute_contour_find(mat) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/wildfire/converter/analizer.rb', line 53

def execute_contour_find(mat)
  contour_mat = temp_mat
  output_array_of_arrays_contours = Std::Vector::Cv_Mat.new

  Cv.find_contours(mat, output_array_of_arrays_contours, contour_mat,
    CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE)

  output_array_of_arrays_contours
end

#longest_contourObject



36
37
38
39
40
41
42
# File 'lib/wildfire/converter/analizer.rb', line 36

def longest_contour
  sorted_by_length = contours.to_a.sort_by do |contour|
    Cv.arc_length(contour, false)
  end

  sorted_by_length.last
end

#page_contourObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wildfire/converter/analizer.rb', line 23

def page_contour
  greyed = Transformer.grey(@resizer.to_small)
  medianed = Transformer.median_blur(greyed)
  eroded = Transformer.erode(medianed)
  dilated = Transformer.dilate(medianed)
  substracted = Transformer.substract(dilated, eroded)
  binaried = Transformer.binary(substracted)


  result = Transformer.approximate(longest_contour)
  Orderer.to_tl_tr_br_bl(result.to_a)
end

#shaped_pointsObject



9
10
11
# File 'lib/wildfire/converter/analizer.rb', line 9

def shaped_points
  page_contour.to_a.map { |p| @resizer.big_point(p) }
end

#shaped_rect(rect) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/wildfire/converter/analizer.rb', line 13

def shaped_rect(rect)
  top = rect.tl
  bottom = rect.br

  Cv::Rect.new(top.x * @resizer.to_big_ratio,
    top.y * @resizer.to_big_ratio,
    (bottom.x - top.x) * @resizer.to_big_ratio,
    (bottom.y - top.y) * @resizer.to_big_ratio)
end