Exemplo n.º 1
0
def line_extraction_v1(
        probs: np.ndarray,
        low_threshold: float,
        high_threshold: float,
        sigma: float = 0.0,
        filter_width: float = 0.00,
        vertical_maxima: bool = False) -> Tuple[List[np.ndarray], np.ndarray]:
    # Smooth
    probs2 = cleaning_probs(probs, sigma=sigma)

    lines_mask = hysteresis_thresholding(
        probs2,
        low_threshold,
        high_threshold,
        candidates_mask=vertical_local_maxima(probs2)
        if vertical_maxima else None)
    # Remove lines touching border
    # lines_mask = remove_borders(lines_mask)

    # Extract polygons from line mask
    contours = find_lines(lines_mask)

    filtered_contours = []
    page_width = probs.shape[1]
    for cnt in contours:
        centroid_x, centroid_y = np.mean(cnt, axis=0)[0]
        if centroid_x < filter_width * page_width or centroid_x > (
                1 - filter_width) * page_width:
            continue
        # if cv2.arcLength(cnt, False) < filter_width*page_width:
        #    continue
        filtered_contours.append(cnt)

    return filtered_contours, lines_mask
Exemplo n.º 2
0
def line_extraction_v1(
        probs: np.ndarray,
        low_threshold: float,
        high_threshold: float,
        sigma: float = 0.0,
        filter_width: float = 0.00,
        vertical_maxima: bool = False) -> Tuple[List[np.ndarray], np.ndarray]:
    """
    Given a probability map, returns the contour of lines and the corresponding mask

    :param probs: probability map (numpy array)
    :param low_threshold: hysteresis low threshold
    :param high_threshold: hysteresis high threshold
    :param sigma: sigma value for gaussian filtering
    :param filter_width: percentage of the image width to filter out lines that are close to borders (default 0.0)
    :param vertical_maxima: set to True to use vertical local maxima as candidates for the hysteresis thresholding
    :return:
    """
    # Smooth
    probs2 = cleaning_probs(probs, sigma=sigma)

    lines_mask = hysteresis_thresholding(
        probs2,
        low_threshold,
        high_threshold,
        candidates_mask=vertical_local_maxima(probs2)
        if vertical_maxima else None)
    # Remove lines touching border
    # lines_mask = remove_borders(lines_mask)

    # Extract polygons from line mask
    contours = find_lines(lines_mask)

    filtered_contours = []
    page_width = probs.shape[1]
    for cnt in contours:
        centroid_x, centroid_y = np.mean(cnt, axis=0)[0]
        if centroid_x < filter_width * page_width or centroid_x > (
                1 - filter_width) * page_width:
            continue
        # if cv2.arcLength(cnt, False) < filter_width*page_width:
        #    continue
        filtered_contours.append(cnt)

    return filtered_contours, lines_mask