-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify_regions.py
109 lines (86 loc) · 3.43 KB
/
identify_regions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import cv2 as cv
import numpy as np
def checkForLength(scanner, t):
t = int(t)
lengths = []
start_index = -1
for i in range(len(scanner)):
if scanner[i] == 255:
if start_index == -1:
start_index = i
else:
if start_index != -1:
if i - start_index > t:
lengths.append((start_index, i - 1))
start_index = -1
if (start_index != -1) and (len(scanner) - start_index > t):
lengths.append((start_index, len(scanner) - 1))
return lengths
def getXCoord(start_row):
longest_sequence = []
current_sequence = []
for i, num in enumerate(start_row):
if num == 255:
current_sequence.append(i)
else:
if len(current_sequence) > len(longest_sequence):
longest_sequence = current_sequence.copy()
current_sequence = []
if len(current_sequence) > len(longest_sequence):
longest_sequence = current_sequence.copy()
start_index = longest_sequence[0]
end_index = start_index + len(longest_sequence) - 1
return (start_index + end_index) // 2
def is_recurring(start_p, end_p, lines):
ref_x = start_p[0]
for line in lines:
# start point line[0]
## start point x = line[0][0]
## start point y = line[0][1]
# end point line[1]
## end point x = line[1][0]
## end point y = line[1][1]
line_x = line[0][0]
if abs(line_x - ref_x) < 20:
return True
return False
def get_region_lines(gray):
height, width = gray.shape[:2]
gray = cv.bitwise_not(gray)
bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
cv.THRESH_BINARY, 15, -2)
# specify the filtering thresholds
threshW = 20 # Number of columns used per scan
threshH = height / 2 # Minimum height of line (Will be calculated at the end of the process)
output_lines = []
# scan the image
for i in range(0, width, threshW):
roi = bw[0:height, i:i + threshW]
# create a scanner 1x [img height] -> this is a indicator for each roi area
scanner = [[None] for _ in range(height)]
# detect lines
for j in range(len(scanner)):
# check for white pixels
if np.any(roi[j] == 255): # we are looking for white pixels
scanner[j] = 255
else:
scanner[j] = 0
# filter by line lengths
a = checkForLength(scanner, threshH)
if len(a) != 0:
# draw the line
start_index = a[0][0]
end_index = a[0][1]
# get endpoints' x coordinates
start_x_coord = getXCoord(roi[start_index])
end_x_coord = getXCoord(roi[end_index])
# threshold for eliminating border lines
border_thresh = 60
# eliminate the border lines. We just need lines in the middle
if not ((start_x_coord + i <= border_thresh) or (start_x_coord + i >= width - border_thresh) \
or (end_x_coord + i <= border_thresh) or (end_x_coord + i >= width - border_thresh)):
start_point = (i + start_x_coord, start_index)
end_point = (i + end_x_coord, end_index)
if is_recurring(start_point, end_point, output_lines) != True:
output_lines.append([start_point, end_point])
return output_lines