def binary_objects(binary: np.array) -> np.array: """ Labels features in an array and segments them into objects. """ labels, _ = morph.label(binary) objects = morph.find_objects(labels) return objects
def compute_lines(segmentation, spread, scale): """Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.""" lobjects = morph.find_objects(segmentation) lines = [] for i, o in enumerate(lobjects): if o is None: continue if sl.dim1(o) < 2 * scale or sl.dim0(o) < scale: continue mask = (segmentation[o] == i + 1) if np.amax(mask) == 0: continue result = record() result.label = i + 1 result.bounds = o polygon = [] if ((segmentation[o] != 0) == (segmentation[o] != i + 1)).any(): ppoints = draw_polygon(spread[o], i + 1) ppoints = ppoints[1:] if ppoints else [] polygon = [(o[0].start + p[0], o[1].start + p[1]) for p in ppoints] if not polygon: polygon = [(o[0].start, o[1].start), (o[0].stop, o[1].start), (o[0].stop, o[1].stop), (o[0].start, o[1].stop)] result.polygon = polygon result.mask = mask lines.append(result) return lines
def compute_lines(segmentation, scale): """Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.""" lobjects = morph.find_objects(segmentation) lines = [] for i, o in enumerate(lobjects): if o is None: continue if sl.dim1(o) < 2 * scale or sl.dim0(o) < scale: continue mask = (segmentation[o] == i + 1) if np.amax(mask) == 0: continue result = record() result.label = i + 1 result.bounds = o result.mask = mask lines.append(result) return lines
def remove_hlines(binary, scale, maxsize=10): """ Removes horizontal black lines that only interfere with page segmentation. Args: binary (numpy.array): scale (float): maxsize (int): maximum size of removed lines Returns: numpy.array containing the filtered image. """ labels, _ = morph.label(binary) objects = morph.find_objects(labels) for i, b in enumerate(objects): if sl.width(b) > maxsize * scale: labels[b][labels[b] == i + 1] = 0 return np.array(labels != 0, 'B')
def compute_lines(segmentation, scale): """Given a line segmentation map, computes a list of tuples consisting of 2D slices and masked images.""" lobjects = morph.find_objects(segmentation) lines = [] for i, o in enumerate(lobjects): if o is None: continue if sl.dim1(o) < 2*scale or sl.dim0(o) < scale: continue mask = (segmentation[o] == i+1) if np.amax(mask) == 0: continue result = record() result.label = i+1 result.bounds = o result.mask = mask lines.append(result) return lines
def remove_hlines(binary, scale, maxsize=10): """ Removes horizontal black lines that only interfere with page segmentation. Args: binary (numpy.array): scale (float): maxsize (int): maximum size of removed lines Returns: numpy.array containing the filtered image. """ labels, _ = morph.label(binary) objects = morph.find_objects(labels) for i, b in enumerate(objects): if sl.width(b) > maxsize*scale: labels[b][labels[b] == i+1] = 0 return np.array(labels != 0, 'B')
def remove_hlines(binary: np.ndarray, scale: float, maxsize: int = 10) -> np.ndarray: """ Removes horizontal black lines that only interfere with page segmentation. Args: binary: scale: maxsize: maximum size of removed lines Returns: numpy.ndarray containing the filtered image. """ logger.debug('Filtering horizontal lines') labels, _ = morph.label(binary) objects = morph.find_objects(labels) for i, b in enumerate(objects): if sl.width(b) > maxsize * scale: labels[b][labels[b] == i + 1] = 0 return np.array(labels != 0, 'B')
def binary_objects(binary): labels, n = morph.label(binary) objects = morph.find_objects(labels) return objects