Use connected-component labeling for finding letters.
This commit is contained in:
parent
74570e6760
commit
b05cc3853e
3 changed files with 52 additions and 22 deletions
|
|
@ -151,22 +151,36 @@ class Image(object):
|
|||
bottom_margin = _get_margin_height(reversed(self.bitmap))
|
||||
return self[top_margin:self.height - bottom_margin, :]
|
||||
|
||||
def _iter_lines(self, min_space, T=False):
|
||||
line_start = None
|
||||
prev_line_end = 0
|
||||
def _iter_lines(self, min_space):
|
||||
def iter_lines():
|
||||
line_start = None
|
||||
prev_line_end = 0
|
||||
|
||||
for i, row in enumerate(self.bitmap):
|
||||
if _is_nonblank(row):
|
||||
if line_start is None:
|
||||
line_start = i
|
||||
height = line_start - prev_line_end
|
||||
if height >= min_space:
|
||||
yield self[prev_line_end:line_start]
|
||||
else:
|
||||
if line_start is not None:
|
||||
yield self[line_start:i,:]
|
||||
line_start = None
|
||||
prev_line_end = i
|
||||
for i, row in enumerate(self.bitmap):
|
||||
if _is_nonblank(row):
|
||||
if line_start is None:
|
||||
line_start = i
|
||||
else:
|
||||
if line_start is not None:
|
||||
yield self[line_start:i,:]
|
||||
line_start = None
|
||||
prev_line_end = i
|
||||
|
||||
def merge_lines(lines):
|
||||
prev_line = None
|
||||
for line in lines:
|
||||
if prev_line is None:
|
||||
prev_line = line
|
||||
else:
|
||||
if line.y1 - prev_line.y2 < min_space:
|
||||
prev_line = self[prev_line.y1:line.y2]
|
||||
else:
|
||||
yield prev_line
|
||||
prev_line = line
|
||||
if prev_line is not None:
|
||||
yield prev_line
|
||||
|
||||
return merge_lines(iter_lines())
|
||||
|
||||
|
||||
class SubImage(Image):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue