16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/percept/field_detector.rb', line 16
def detect_field(line, image)
stopping_point = [0, line.start_y - Percept.config.max_field_height].max
starting_point = line.start_y - 2
min_index = line.start_y - Percept.config.min_field_height
return nil unless min_index >= 0
buffered_start = line.start_x + Percept.config.field_edge_buffer
buffered_length = line.length - 2 * Percept.config.field_edge_buffer
starting_point.downto(stopping_point) do |index|
pixels = image.get_pixels(buffered_start, index, buffered_length, 1)
blackish_pixel = pixels.any?(&:blackish?)
if index >= min_index && blackish_pixel
return nil
elsif blackish_pixel
return Percept::Field.new(
start_x: line.start_x,
end_x: line.end_x,
start_y: index + 1,
end_y: line.start_y,
line: line,
image: image,
)
end
end
Percept::Field.new(
start_x: line.start_x,
end_x: line.end_x,
start_y: stopping_point,
end_y: line.start_y,
line: line,
image: image,
)
end
|