Obtain page boundaries
Summary¶
X and Y margins are subtracted from a shrunk black and white copy of the image.
Details¶
The story begins with the WarpedImage class in the
image.py module.
- When initialised, it reads in the image file
- A smaller version is made (stored in the
.smallattribute) by callingresize_to_screen().
def resize_to_screen(self, copy=False):
height, width = self.cv2_img.shape[:2]
scl_x = float(width) / cfg.image_opts.SCREEN_MAX_W
scl_y = float(height) / cfg.image_opts.SCREEN_MAX_H
scl = int(np.ceil(max(scl_x, scl_y)))
if scl > 1.0:
inv_scl = 1.0 / scl
img = cv2.resize(
self.cv2_img, (0, 0), None, inv_scl, inv_scl, cv2.INTER_AREA
)
elif copy:
img = self.cv2_img.copy()
else:
img = self.cv2_img
return img
- The
SCREEN_MAX_WandSCREEN_MAX_Hparameters are configured through the globalcfgobject. They assume a roughly HD screen size for viewing intermediate results (1280x700)cfgis constructed by loading defaults from a TOML config file which ships with the package (seeoptions.py) but all of these settings can be modified using command line flags (seecli.py)
The page boundaries are obtained by the call to calculate_page_extents, where the height of the
shrunk page (to fit a HD screen) is offset by an x and y margin (each on both sides).
- An all-zero page mask is created (8-bit integer numpy array, i.e. \([0,255]\))
- (i.e. to initialise the mask as "non-page" or "off" but as a colour: black),
- a rectangle is overlaid (in-place) onto the pagemask according to the margin size,
with colour \(255\) (i.e. white), with a negative value for
thicknesswhich is a sentinel value meaning 'filled' rather than outline of a rectangle
height, width = self.small.shape[:2]
xmin = cfg.image_opts.PAGE_MARGIN_X
ymin = cfg.image_opts.PAGE_MARGIN_Y
xmax, ymax = (width - xmin), (height - ymin)
self.pagemask = np.zeros((height, width), dtype=np.uint8)
rectangle(self.pagemask, (xmin, ymin), (xmax, ymax), color=255, thickness=-1)
self.page_outline = np.array(
[[xmin, ymin], [xmin, ymax], [xmax, ymax], [xmax, ymin]]
)
Note that the page outline is a list (matrix) of the corners clockwise from bottom left (BL, TL, TR, BR).