refactoring

This commit is contained in:
Matthias Bisping 2022-02-06 14:42:45 +01:00
parent 89a99d3586
commit e8863d67aa
2 changed files with 39 additions and 9 deletions

View File

@ -2,8 +2,15 @@ import cv2
import numpy as np
from pdf2image import pdf2image
from vidocp.utils import show_mpl, draw_rectangles, remove_included, remove_primary_text_regions, \
__detect_large_coherent_structures, is_large_enough, has_acceptable_format
from vidocp.utils import (
show_mpl,
draw_rectangles,
remove_included,
remove_primary_text_regions,
__detect_large_coherent_structures,
is_large_enough,
has_acceptable_format,
)
def is_likely_figure(cont, min_area=5000, max_width_to_hight_ratio=6):

View File

@ -144,8 +144,8 @@ def vec_rect_to_xywh(rect):
return x, y, w, h
def remove_primary_text_regions(image):
"""Removes regions of primary text, meaning no figure descriptions for example, but main text body paragraphs.
def find_primary_text_regions(image):
"""Finds regions of primary text, meaning no figure descriptions for example, but main text body paragraphs.
Args:
image: Image to remove primary text from.
@ -174,7 +174,26 @@ def remove_primary_text_regions(image):
cnts, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in filter(is_likely_primary_text_segments, cnts):
cnts = filter(is_likely_primary_text_segments, cnts)
return cnts
def remove_primary_text_regions(image):
"""Removes regions of primary text, meaning no figure descriptions for example, but main text body paragraphs.
Args:
image: Image to remove primary text from.
Returns:
Image with primary text removed.
"""
image = image.copy()
cnts = find_primary_text_regions(image)
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), -1)
@ -206,17 +225,21 @@ def is_large_enough(cont, min_area):
return cv2.contourArea(cont, False) > min_area
def has_acceptable_format(cont, max_width_to_hight_ratio):
def has_acceptable_format(cont, max_width_to_height_ratio):
_, _, w, h = cv2.boundingRect(cont)
return max_width_to_hight_ratio >= w / h >= (1 / max_width_to_hight_ratio)
return max_width_to_height_ratio >= w / h >= (1 / max_width_to_height_ratio)
def is_filled(hierarchy):
# See https://stackoverflow.com/questions/60095520/how-to-distinguish-filled-circle-contour-and-unfilled-circle-contour-in-opencv
"""Checks whether a hierarchy is filled.
References:
https://stackoverflow.com/questions/60095520/how-to-distinguish-filled-circle-contour-and-unfilled-circle-contour-in-opencv
"""
return hierarchy[3] <= 0 and hierarchy[2] == -1
def is_boxy(contour):
epsilon = 0.01 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
return len(approx) <= 10
return len(approx) <= 10