Use x, y, top, bottom instead of x1, y1, x2, y2. Add aliases: left=x, top=y.

This commit is contained in:
Andrey Golovizin 2014-08-13 22:43:55 +02:00
parent 360ed844c2
commit 4ff37720d9
3 changed files with 45 additions and 29 deletions

View file

@ -62,14 +62,14 @@ class PageScene(QGraphicsScene):
letterPen = QPen(QColor(50, 50, 50, 100))
letterBrush = QBrush(QColor(255, 255, 0, 80))
linePen = QPen(QColor(255, 150, 150, 100))
linePen = QPen(QColor(255, 50, 50, 100))
for line in page:
for word in line:
for letter in word:
if not letter.image.isspace:
self.addRect(letter.x1, letter.y1, letter.width - 1, letter.height - 1, letterPen, letterBrush)
self.addLine(line.x1, line.baseline - 1, line.x2, line.baseline - 1, linePen)
# self.addRect(line.x1, line.y1, line.width, line.height, Qt.red)
self.addRect(letter.x - 1, letter.y - 1, letter.width + 1, letter.height + 1, letterPen, letterBrush)
self.addLine(line.left, line.baseline, line.right, line.baseline, linePen)
# self.addRect(line.x, line.y, line.width, line.height, Qt.red)
def addPage(self, page):
qimage = ndimage2qimage(page.image.data)

View file

@ -31,10 +31,10 @@ def _is_nonblank(bitmap):
class Image(object):
"""Basic image class."""
def __init__(self, data, y1=0, x1=0):
def __init__(self, data, x=0, y=0):
self.data = data
self.y1 = y1
self.x1 = x1
self.x = x
self.y = y
def __getitem__(self, key):
"""Return an Image for the specified region."""
@ -59,12 +59,12 @@ class Image(object):
else:
yslice, xslice = key
ystart, yend = indices(yslice, self.height)
xstart, xend = indices(xslice, self.width)
ystart, yend = indices(yslice, self.height)
y1 = self.y1 + ystart
x1 = self.x1 + xstart
return Image(self.data[key], y1, x1)
x = self.x + xstart
y = self.y + ystart
return Image(self.data[key], x, y)
def _repr_png_(self):
buf = BytesIO()
@ -76,12 +76,20 @@ class Image(object):
return cls(imread(filename))
@property
def y2(self):
return self.y1 + self.height
def left(self):
return self.x
@property
def x2(self):
return self.x1 + self.width
def right(self):
return self.x + self.width
@property
def top(self):
return self.y
@property
def bottom(self):
return self.y + self.height
@property
def shape(self):
@ -97,7 +105,7 @@ class Image(object):
@cached_property
def T(self):
return type(self)(self.data.swapaxes(0, 1), y1=self.x1, x1=self.y1)
return type(self)(self.data.swapaxes(0, 1), x=self.y, y=self.x)
@cached_property
def bitmap(self):
@ -175,8 +183,8 @@ class Image(object):
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]
if line.top - prev_line.bottom < min_space:
prev_line = self[prev_line.top:line.bottom]
else:
yield prev_line
prev_line = line

View file

@ -45,20 +45,28 @@ class PageObject(object):
return self.image.width
@property
def x1(self):
return self.image.x1
def x(self):
return self.image.x
@property
def x2(self):
return self.image.x2
def y(self):
return self.image.y
@property
def y1(self):
return self.image.y1
def left(self):
return self.image.left
@property
def y2(self):
return self.image.y2
def right(self):
return self.image.right
@property
def top(self):
return self.image.top
@property
def bottom(self):
return self.image.bottom
class Page(PageObject):
@ -95,7 +103,7 @@ class Line(PageObject):
gradient = filters.correlate1d(histogram, [-1, 1], axis=0)
# top = gradient.argmax()
bottom = gradient.argmin()
return self.y1 + bottom
return self.y + bottom
class Word(PageObject):
@ -113,10 +121,10 @@ class Word(PageObject):
obj_indices = ndimage.find_objects(labels, max_label)
letter_images = (self.image[obj_index] for obj_index in obj_indices)
letters = (
Letter(image, self.baseline - image.y2)
Letter(image, self.baseline - image.bottom)
for image in letter_images
)
sorted_letters = sorted(letters, key=lambda letter: (letter.x1, -letter.y1))
sorted_letters = sorted(letters, key=lambda letter: (letter.left, -letter.bottom))
return iter(sorted_letters)