def image_from_segment(self, segment, parent_image, parent_coords, fill='background', transparency=False, feature_selector='', feature_filter=''): """Extract an image for a PAGE-XML hierarchy segment from its parent's image. Given... * ``parent_image``, a PIL.Image of the parent, with * ``parent_coords``, a dict with information about ``parent_image``: - ``transform``: a Numpy array with an affine transform which converts from absolute coordinates to those relative to the image, i.e. after applying all operations (starting with the original image) - ``angle``: the rotation/reflection angle applied to the image so far, - ``features``: the AlternativeImage @comments for the image, i.e. names of all operations that lead up to this result, and * ``segment``, a PAGE segment object logically contained in it (i.e. TextRegionType / TextLineType / WordType / GlyphType), ...extract the segment's corresponding PIL.Image, either from AlternativeImage (if it exists), or producing a new image via cropping from ``parent_image`` (otherwise). If ``feature_selector`` and/or ``feature_filter`` is given, then select/filter among the cropped ``parent_image`` and the available AlternativeImages the last one which contains all of the selected, but none of the filtered features (i.e. @comments classes), or raise an error. (Required and produced features need not be in the same order, so ``feature_selector`` is merely a mask specifying Boolean AND, and ``feature_filter`` is merely a mask specifying Boolean OR.) Cropping uses a polygon mask (not just the bounding box rectangle). Areas outside the polygon will be filled according to ``fill``: - if ``background`` (the default), then fill with the median color of the image; - otherwise, use the given color, e.g. ``white`` or (255,255,255). Moreover, if ``transparency`` is true, and unless the image already has an alpha channel, then add an alpha channel which is fully opaque before cropping and rotating. (Thus, only the exposed areas will be transparent afterwards, for those that can interpret alpha channels). When cropping, compensate any @orientation angle annotated for the parent (from parent-level deskewing) by rotating the segment coordinates in an inverse transformation (i.e. translation to center, then passive rotation, and translation back). Regardless, if any @orientation angle is annotated for the segment (from segment-level deskewing), and the chosen image does not have the feature "deskewed" yet, and unless "deskewed" is being filtered, then rotate it - compensating for any previous ``angle``. (However, if @orientation is above the [-45°,45°] interval, then apply as much transposition as possible first, unless "rotated-90" / "rotated-180" / "rotated-270" is being filtered.) Return a tuple: * the extracted image, * a dictionary with information about the extracted image: - ``transform``: a Numpy array with an affine transform which converts from absolute coordinates to those relative to the image, i.e. after applying all parent operations, and then cropping to the segment's bounding box, and deskewing with the segment's orientation angle (if any) - ``angle``: the rotation/reflection angle applied to the image so far, - ``features``: the AlternativeImage @comments for the image, i.e. names of all operations that lead up to this result. (These can be used to create a new AlternativeImage, or passed down for calls on lower hierarchy levels.) Example: * get a raw (colored) but already deskewed and cropped image: ``image, xywh = workspace.image_from_segment(region, page_image, page_xywh, feature_selector='deskewed,cropped', feature_filter='binarized,grayscale_normalized')`` """ log = getLogger('ocrd.workspace.image_from_segment') # note: We should mask overlapping neighbouring segments here, # but finding the right clipping rules can be difficult if operating # on the raw (non-binary) image data alone: for each intersection, it # must be decided which one of either segment or neighbour to assign, # e.g. an ImageRegion which properly contains our TextRegion should be # completely ignored, but an ImageRegion which is properly contained # in our TextRegion should be completely masked, while partial overlap # may be more difficult to decide. On the other hand, on the binary image, # we can use connected component analysis to mask foreground areas which # originate in the neighbouring regions. But that would introduce either # the assumption that the input has already been binarized, or a dependency # on some ad-hoc binarization method. Thus, it is preferable to use # a dedicated processor for this (which produces clipped AlternativeImage # or reduced polygon coordinates). # get polygon outline of segment relative to parent image: segment_polygon = coordinates_of_segment(segment, parent_image, parent_coords) # get relative bounding box: segment_bbox = bbox_from_polygon(segment_polygon) # get size of the segment in the parent image after cropping # (i.e. possibly different from size before rotation at the parent, but # also possibly different from size after rotation below/AlternativeImage): segment_xywh = xywh_from_bbox(*segment_bbox) # create a mask from the segment polygon: segment_image = image_from_polygon(parent_image, segment_polygon, fill=fill, transparency=transparency) # recrop into segment rectangle: segment_image = crop_image(segment_image, box=segment_bbox) # subtract offset from parent in affine coordinate transform: # (consistent with image cropping) segment_coords = { 'transform': shift_coordinates(parent_coords['transform'], np.array([-segment_bbox[0], -segment_bbox[1]])) } if 'orientation' in segment.__dict__: # region angle: PAGE @orientation is defined clockwise, # whereas PIL/ndimage rotation is in mathematical direction: segment_coords['angle'] = -(segment.get_orientation() or 0) else: segment_coords['angle'] = 0 if segment_coords['angle']: # @orientation is always absolute; if higher levels # have already rotated, then we must compensate: angle = segment_coords['angle'] - parent_coords['angle'] # map angle from (-180,180] to [0,360], and partition into multiples of 90; # but avoid unnecessary large remainders, i.e. split symmetrically: orientation = (angle + 45) % 360 orientation = orientation - (orientation % 90) skew = (angle % 360) - orientation skew = 180 - (180 - skew) % 360 # map to [-45,45] log.debug("segment '%s' has orientation=%d skew=%.2f", segment.id, orientation, skew) else: orientation = 0 skew = 0 segment_coords['angle'] = parent_coords[ 'angle'] # nothing applied yet (depends on filters) if (orientation and not 'rotated-%d' % orientation in feature_filter.split(',')): # Transpose in affine coordinate transform: # (consistent with image transposition or AlternativeImage below) transposition = { 90: Image.ROTATE_90, 180: Image.ROTATE_180, 270: Image.ROTATE_270 }.get(orientation) # no default segment_coords['transform'] = transpose_coordinates( segment_coords['transform'], transposition, np.array([0.5 * segment_xywh['w'], 0.5 * segment_xywh['h']])) segment_xywh['w'], segment_xywh[ 'h'] = adjust_canvas_to_transposition( [segment_xywh['w'], segment_xywh['h']], transposition) segment_coords['angle'] += orientation if (skew and not 'deskewed' in feature_filter.split(',')): # Rotate around center in affine coordinate transform: # (consistent with image rotation or AlternativeImage below) segment_coords['transform'] = rotate_coordinates( segment_coords['transform'], skew, np.array([0.5 * segment_xywh['w'], 0.5 * segment_xywh['h']])) segment_coords['angle'] += skew # initialize AlternativeImage@comments classes from parent, except # for those operations that can apply on multiple hierarchy levels: segment_coords['features'] = ','.join([ feature for feature in parent_coords['features'].split(',') if feature in ['binarized', 'grayscale_normalized', 'despeckled', 'dewarped'] ]) alternative_image = None alternative_images = segment.get_AlternativeImage() if alternative_images: # (e.g. from segment-level cropping, binarization, deskewing or despeckling) if feature_selector or feature_filter: alternative_image = None # search from the end, because by convention we always append, # and among multiple satisfactory images we want the most recent: for alternative_image in reversed(alternative_images): features = alternative_image.get_comments() if (all(feature in features for feature in feature_selector.split(',') if feature) and not any(feature in features for feature in feature_filter.split(',') if feature)): break else: alternative_image = None else: alternative_image = alternative_images[-1] features = alternative_image.get_comments() if alternative_image: log.debug("Using AlternativeImage %d (%s) for segment '%s'", alternative_images.index(alternative_image) + 1, features, segment.id) segment_image = self._resolve_image_as_pil( alternative_image.get_filename()) segment_coords['features'] = features # transpose, if (still) necessary: if (orientation and not 'rotated-%d' % orientation in segment_coords['features'] and not 'rotated-%d' % orientation in feature_filter.split(',')): log.info("Transposing %s for segment '%s' by %d°", "AlternativeImage" if alternative_image else "image", segment.id, orientation) segment_image = transpose_image(segment_image, { 90: Image.ROTATE_90, 180: Image.ROTATE_180, 270: Image.ROTATE_270 }.get(orientation)) # no default segment_coords['features'] += ',rotated-%d' % orientation if (orientation and not 'rotated-%d' % orientation in feature_filter.split(',')): # FIXME we should enforce consistency here (i.e. split into transposition # and minimal rotation) if not (segment_image.width == segment_xywh['w'] and segment_image.height == segment_xywh['h']): log.error( 'segment "%s" image (%s; %dx%d) has not been transposed properly (%dx%d) during rotation', segment.id, segment_coords['features'], segment_image.width, segment_image.height, segment_xywh['w'], segment_xywh['h']) # deskew, if (still) necessary: if (skew and not 'deskewed' in segment_coords['features'] and not 'deskewed' in feature_filter.split(',')): log.info("Rotating %s for segment '%s' by %.2f°", "AlternativeImage" if alternative_image else "image", segment.id, skew) segment_image = rotate_image(segment_image, skew, fill=fill, transparency=transparency) segment_coords['features'] += ',deskewed' if (skew and not 'deskewed' in feature_filter.split(',')): # FIXME we should enforce consistency here (i.e. rotation always reshapes, # and rescaling never happens) w_new, h_new = adjust_canvas_to_rotation( [segment_xywh['w'], segment_xywh['h']], skew) if not (w_new - 2 < segment_image.width < w_new + 2 and h_new - 2 < segment_image.height < h_new + 2): log.error( 'segment "%s" image (%s; %dx%d) has not been reshaped properly (%dx%d) during rotation', segment.id, segment_coords['features'], segment_image.width, segment_image.height, w_new, h_new) else: # FIXME: currently unavoidable with line-level dewarping (which increases height) if not (segment_xywh['w'] - 2 < segment_image.width < segment_xywh['w'] + 2 and segment_xywh['h'] - 2 < segment_image.height < segment_xywh['h'] + 2): log.error( 'segment "%s" image (%s; %dx%d) has not been cropped properly (%dx%d)', segment.id, segment_coords['features'], segment_image.width, segment_image.height, segment_xywh['w'], segment_xywh['h']) # verify constraints again: if not all(feature in segment_coords['features'] for feature in feature_selector.split(',') if feature): raise Exception( 'Found no AlternativeImage that satisfies all requirements' + 'selector="%s" in segment "%s"' % (feature_selector, segment.id)) if any(feature in segment_coords['features'] for feature in feature_filter.split(',') if feature): raise Exception( 'Found no AlternativeImage that satisfies all requirements ' + 'filter="%s" in segment "%s"' % (feature_filter, segment.id)) segment_image.format = 'PNG' # workaround for tesserocr#194 return segment_image, segment_coords
def image_from_page(self, page, page_id, fill='background', transparency=False, feature_selector='', feature_filter=''): """Extract an image for a PAGE-XML page from the workspace. Given ``page``, a PAGE PageType object, extract its PIL.Image, either from its AlternativeImage (if it exists), or from its @imageFilename (otherwise). Also crop it, if a Border exists, and rotate it, if any @orientation angle is annotated. If ``feature_selector`` and/or ``feature_filter`` is given, then select/filter among the @imageFilename image and the available AlternativeImages the last one which contains all of the selected, but none of the filtered features (i.e. @comments classes), or raise an error. (Required and produced features need not be in the same order, so ``feature_selector`` is merely a mask specifying Boolean AND, and ``feature_filter`` is merely a mask specifying Boolean OR.) If the chosen image does not have the feature "cropped" yet, but a Border exists, and unless "cropped" is being filtered, then crop it. Likewise, if the chosen image does not have the feature "deskewed" yet, but an @orientation angle is annotated, and unless "deskewed" is being filtered, then rotate it. (However, if @orientation is above the [-45°,45°] interval, then apply as much transposition as possible first, unless "rotated-90" / "rotated-180" / "rotated-270" is being filtered.) Cropping uses a polygon mask (not just the bounding box rectangle). Areas outside the polygon will be filled according to ``fill``: - if ``background`` (the default), then fill with the median color of the image; - otherwise, use the given color, e.g. ``white`` or (255,255,255). Moreover, if ``transparency`` is true, and unless the image already has an alpha channel, then add an alpha channel which is fully opaque before cropping and rotating. (Thus, only the exposed areas will be transparent afterwards, for those that can interpret alpha channels). Return a tuple: * the extracted image, * a dictionary with information about the extracted image: - ``transform``: a Numpy array with an affine transform which converts from absolute coordinates to those relative to the image, i.e. after cropping to the page's border / bounding box (if any) and deskewing with the page's orientation angle (if any) - ``angle``: the rotation/reflection angle applied to the image so far, - ``features``: the AlternativeImage @comments for the image, i.e. names of all operations that lead up to this result, * an OcrdExif instance associated with the original image. (The first two can be used to annotate a new AlternativeImage, or be passed down with ``image_from_segment``.) Example: * get a raw (colored) but already deskewed and cropped image: `` page_image, page_coords, page_image_info = workspace.image_from_page( page, page_id, feature_selector='deskewed,cropped', feature_filter='binarized,grayscale_normalized') `` """ log = getLogger('ocrd.workspace.image_from_page') page_image = self._resolve_image_as_pil(page.imageFilename) page_image_info = OcrdExif(page_image) page_coords = dict() # use identity as initial affine coordinate transform: page_coords['transform'] = np.eye(3) # interim bbox (updated with each change to the transform): page_bbox = [0, 0, page_image.width, page_image.height] page_xywh = { 'x': 0, 'y': 0, 'w': page_image.width, 'h': page_image.height } border = page.get_Border() # page angle: PAGE @orientation is defined clockwise, # whereas PIL/ndimage rotation is in mathematical direction: page_coords['angle'] = -(page.get_orientation() or 0) # map angle from (-180,180] to [0,360], and partition into multiples of 90; # but avoid unnecessary large remainders, i.e. split symmetrically: orientation = (page_coords['angle'] + 45) % 360 orientation = orientation - (orientation % 90) skew = (page_coords['angle'] % 360) - orientation skew = 180 - (180 - skew) % 360 # map to [-45,45] page_coords['angle'] = 0 # nothing applied yet (depends on filters) log.debug("page '%s' has %s orientation=%d skew=%.2f", page_id, "border," if border else "", orientation, skew) # initialize AlternativeImage@comments classes as empty: page_coords['features'] = '' alternative_image = None alternative_images = page.get_AlternativeImage() if alternative_images: # (e.g. from page-level cropping, binarization, deskewing or despeckling) if feature_selector or feature_filter: alternative_image = None # search from the end, because by convention we always append, # and among multiple satisfactory images we want the most recent: for alternative_image in reversed(alternative_images): features = alternative_image.get_comments() if (all(feature in features for feature in feature_selector.split(',') if feature) and not any(feature in features for feature in feature_filter.split(',') if feature)): break else: alternative_image = None else: alternative_image = alternative_images[-1] features = alternative_image.get_comments() if alternative_image: log.debug("Using AlternativeImage %d (%s) for page '%s'", alternative_images.index(alternative_image) + 1, features, page_id) page_image = self._resolve_image_as_pil( alternative_image.get_filename()) page_coords['features'] = features # adjust the coord transformation to the steps applied on the image, # and apply steps on the existing image in case it is missing there, # but traverse all steps (crop/reflect/rotate) in a particular order: # - existing image features take priority (in the order annotated), # - next is cropping (if necessary but not already applied), # - next is reflection (if necessary but not already applied), # - next is rotation (if necessary but not already applied). # This helps deal with arbitrary workflows (e.g. crop then deskew, # or deskew then crop), regardless of where images are generated. alternative_image_features = page_coords['features'].split(',') for i, feature in enumerate( alternative_image_features + (['cropped'] if ( border and not 'cropped' in page_coords['features'] and not 'cropped' in feature_filter.split(',')) else []) + (['rotated-%d' % orientation] if (orientation and not 'rotated-%d' % orientation in page_coords['features'] and not 'rotated-%d' % orientation in feature_filter.split(',')) else []) + (['deskewed'] if (skew and not 'deskewed' in page_coords['features'] and not 'deskewed' in feature_filter.split(',')) else []) + # not a feature to be added, but merely as a fallback position # to always enter loop at i == len(alternative_image_features) ['_check']): # image geometry vs feature consistency can only be checked # after all features on the existing AlternativeImage have # been adjusted for in the transform, and when there is a mismatch, # additional steps applied here would only repeat the respective # error message; so we only check once at the boundary between # existing and new features # FIXME we should check/enforce consistency when _adding_ AlternativeImage if (i == len(alternative_image_features) and not (page_xywh['w'] - 2 < page_image.width < page_xywh['w'] + 2 and page_xywh['h'] - 2 < page_image.height < page_xywh['h'] + 2)): log.error( 'page "%s" image (%s; %dx%d) has not been cropped properly (%dx%d)', page_id, page_coords['features'], page_image.width, page_image.height, page_xywh['w'], page_xywh['h']) # adjust transform to feature, possibly apply feature to image if feature == 'cropped': page_points = border.get_Coords().points log.debug( "Using explicitly set page border '%s' for page '%s'", page_points, page_id) # get polygon outline of page border: page_polygon = np.array(polygon_from_points(page_points), dtype=np.int32) page_polygon = transform_coordinates(page_polygon, page_coords['transform']) page_polygon = np.round(page_polygon).astype(np.int32) page_bbox = bbox_from_polygon(page_polygon) # get size of the page after cropping but before rotation: page_xywh = xywh_from_bbox(*page_bbox) # subtract offset in affine coordinate transform: # (consistent with image cropping or AlternativeImage below) page_coords['transform'] = shift_coordinates( page_coords['transform'], np.array([-page_xywh['x'], -page_xywh['y']])) # crop, if (still) necessary: if not 'cropped' in page_coords['features']: log.debug( "Cropping %s for page '%s' to border", "AlternativeImage" if alternative_image else "image", page_id) # create a mask from the page polygon: page_image = image_from_polygon(page_image, page_polygon, fill=fill, transparency=transparency) # recrop into page rectangle: page_image = crop_image(page_image, box=page_bbox) page_coords['features'] += ',cropped' elif feature == 'rotated-%d' % orientation: # Transpose in affine coordinate transform: # (consistent with image transposition or AlternativeImage below) transposition = { 90: Image.ROTATE_90, 180: Image.ROTATE_180, 270: Image.ROTATE_270 }.get(orientation) # no default page_coords['transform'] = transpose_coordinates( page_coords['transform'], transposition, np.array([0.5 * page_xywh['w'], 0.5 * page_xywh['h']])) (page_xywh['w'], page_xywh['h']) = adjust_canvas_to_transposition( [page_xywh['w'], page_xywh['h']], transposition) page_coords['angle'] = orientation # transpose, if (still) necessary: if not 'rotated-%d' % orientation in page_coords['features']: log.info( "Transposing %s for page '%s' by %d°", "AlternativeImage" if alternative_image else "image", page_id, orientation) page_image = transpose_image( page_image, { 90: Image.ROTATE_90, 180: Image.ROTATE_180, 270: Image.ROTATE_270 }.get(orientation)) # no default page_coords['features'] += ',rotated-%d' % orientation elif feature == 'deskewed': # Rotate around center in affine coordinate transform: # (consistent with image rotation or AlternativeImage below) page_coords['transform'] = rotate_coordinates( page_coords['transform'], skew, np.array([0.5 * page_xywh['w'], 0.5 * page_xywh['h']])) page_coords['angle'] += skew # deskew, if (still) necessary: if not 'deskewed' in page_coords['features']: log.info( "Rotating %s for page '%s' by %.2f°", "AlternativeImage" if alternative_image else "image", page_id, skew) page_image = rotate_image(page_image, skew, fill=fill, transparency=transparency) page_coords['features'] += ',deskewed' (page_xywh['w'], page_xywh['h']) = adjust_canvas_to_rotation( [page_xywh['w'], page_xywh['h']], skew) # verify constraints again: if not all(feature in page_coords['features'] for feature in feature_selector.split(',') if feature): raise Exception( 'Found no AlternativeImage that satisfies all requirements ' + 'selector="%s" in page "%s"' % (feature_selector, page_id)) if any(feature in page_coords['features'] for feature in feature_filter.split(',') if feature): raise Exception( 'Found no AlternativeImage that satisfies all requirements ' + 'filter="%s" in page "%s"' % (feature_filter, page_id)) page_image.format = 'PNG' # workaround for tesserocr#194 return page_image, page_coords, page_image_info