-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.py
238 lines (203 loc) · 8.59 KB
/
components.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from heapq import heappush, heappop
import numpy as np
from DisjointSet import DisjointSet
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import processing
class Component():
'''a class to hold a single component as found by the mser or blob extraction algorithm.
Attributes:
region (tuple of lists): as accepted by __init__
bbox (list): bounding box as accepted by __init__
x (float): upper left x coordinate of bbox (horizontal dimension)
y (flaot): upper left y coordinate of bbox (vertical dimension directed down)
w (float): width of bbox (horizontal)
h (float): height of bbox (vertical)
A (float): area of bbox
asp (float): aspect ration of bbox (w/h)
color (float): mean color, i.e. intensity of all pixels in region
'''
def __init__(self, region, bbox, img):
'''
function to initialize an object.
Args:
region (tuple of lists): in the shape ([y_coordinates], [x_coordinates]) of pixels belongint to the region
note: x is the horizontal coordinate
bbox (list): in the format [x_min, x_max, y_min, y_max] of the bounding box
img (ndarray): reference to the image the component is contained in
'''
self.region = region
self.bbox = bbox
self.x = self.bbox[0]
self.y = self.bbox[2]
self.w = self.bbox[1] - bbox[0]
self.h = self.bbox[3] - bbox[2]
#if self.h==0 or self.w==0: print(bbox)
self.A = self.w * self.h
#self.asp = self.w / self.h
self.img = img
self.color = np.mean(self.img[self.region[:, 1], self.region[:, 0]])
def get_rect(self):
'''returns a matplotlib Rectange patch for of component's bbox.'''
return Rectangle((self.y-1, self.x-1), self.h+1, self.w+1, edgecolor='red', fill=False)
def extract(self, padding=1, min_height=0):
'''
extracts section of the component's bbox plus a buffer from the image.
Args:
padding: additional padding of the bbox
min_height:
Returns:
an image of the extracted region
'''
off = max(0, min_height - (self.w + padding))
char_img = self.img[max(0, self.x - padding - off):min(self.x + self.w + padding, self.img.shape[0]),
max(0, self.y - padding):min(self.y + self.h + padding, self.img.shape[1])]
# scale image such that char is approximately 1, background is approximately 0
color = self.color
bg_color = (np.mean(char_img) * char_img.size - color * len(self.region)) / (char_img.size - len(self.region))
if color < bg_color:
char_img = -char_img
char_img = (char_img - np.min(char_img)) / (np.max(char_img) - np.min(char_img))
return char_img
class Components():
'''a class to manage all components found by the mser algorithm.
Attributes:
cadidates (list): list of instances of the Component class
img (ndarray): reference to the image
'''
def __init__(self, boxes, img, regions=None, stencil=None, lines=None):
'''
initializes a Components instance.
Args:
boxes: list of bounding boxes
img: the image
regions: list of regions. A region is a list of coordinate tuples.
stencil: image in shape of img containing region labels as pixel values
lines:
'''
if regions is not None:
self.chars = [Component(r, b, img) for r, b in zip(regions, boxes)]
self.stencil = stencil
else:
assert stencil is not None
self.stencil = stencil
regions = self.regions_from_stencil()
assert len(regions) == len(boxes)
self.chars = [Component(r, b, img) for r, b in zip(regions, boxes)]
self.img = img
self.lines = lines
# print(f'Components object initialized with {len(self.bboxes())} components')
def bboxes(self):
'''returns a list of bboxes of all stored components.'''
return [component.bbox for component in self.chars]
def regions(self):
'''returns a list of regions of all stored components. A region is a list of coordinate tubles.'''
return [component.region for component in self.chars]
def set_lines(self, lines):
''' ... '''
assert sum(len(l) for l in lines) == len(self.chars)
self.lines = lines
def extract(self, args=None, use_line_heights=True):
'''returns a list of rescaled images of the extracted components in the image.
Args:
args: Arguments instance
use_line_heights (bool): whether to use line height as height. if flase the images are rescaled to args.input_shape
Returns:
a list of reshaped images of all components
'''
if args is None:
return [c.extract() for c in self.chars]
else:
if not use_line_heights:
return [processing.rescale(c.extract(), args) for c in self.chars]
else:
return [c for line in self.extract_lines(args, use_line_heights) for c in line]
def extract_lines(self, args=None, use_line_heights=True):
'''
extracts images for lines of text found in the image.
Args:
args: Arguments instance
use_line_heights:
Returns:
a list of image of lines
'''
assert self.lines is not None
if args is None:
return [[self.chars[i].extract() for i in line] for line in self.lines]
if use_line_heights:
line_heights = [max([self.chars[i].w for i in line]) for line in self.lines]
else:
line_heights = [0]*len(self.lines)
return [[processing.rescale(self.chars[i].extract(min_height=line_heights[line_id]), args) for i in line]
for line_id, line in enumerate(self.lines)]
def get_spaces(self, threshold_factor=.25):
'''
finds space positions based on the horizontal distances between components and a threshold computed from the
median horizontal spacing.
Args:
threshold_factor: factor for median to determine threshold
Returns:
list of character positions after which to add spaces
'''
spaces = []
space_threshold = self.median_bbox_width() * threshold_factor
for line in self.lines:
current = []
for i in range(len(line)-1):
c1 = self.chars[line[i]]
c2 = self.chars[line[i+1]]
if c2.y - (c1.y + c1.h) > space_threshold:
current.append(i)
spaces.append(current)
return spaces
def regions_from_stencil(self):
'''
extracts regions from a self.stencil.
Returns:
a list of regions, where a region is a list of coordinate tubles.
'''
regions = []
for i in range(1, np.max(self.stencil) + 1):
region = np.flip(np.argwhere(self.stencil == i), axis=1)
regions.append(region)
return regions
def generate_stencil(self):
'''creates a stencil from self.regions.'''
stencil = np.zeros_like(self.img, dtype=np.int32)
for i, region in enumerate(self.regions()):
stencil[region[:, 1], region[:, 0]] = i + 1
self.stencil = stencil
def get_stencil(self):
'''
creates and returns a stencil from self.regions
Returns:
a stencil of the components
'''
if self.stencil is None:
self.generate_stencil()
return self.stencil
def median_bbox_width(self):
'''returns median of compontent's bbox heights.'''
return np.median([c.h for c in self.chars])
def show_img(self, axes=None):
'''shows self.img with bboxes of components in self.chars
Args:
axes (matplotlib axes instance): if not None plot is added to this axes instance
'''
if axes is None:
ax = plt.gca()
else:
ax = axes
ax.imshow(self.img, cmap='gray')
for c in self.chars:
ax.add_patch(c.get_rect())
if axes is None:
plt.show()
def regions_by_size(self):
'''returns regions in order of their size'''
heap = []
for i, c in enumerate(self.chars):
heappush(heap, (-len(c.region), i, c.region))
return [c[2] for c in heap]
def __len__(self):
return len(self.chars)