refactoring
This commit is contained in:
parent
9d30009dce
commit
d9567da428
@ -6,11 +6,11 @@ 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.text import remove_primary_text_regions
|
||||
|
||||
|
||||
def is_likely_figure(cont, min_area=5000, max_width_to_hight_ratio=6):
|
||||
|
||||
57
vidocp/utils/text.py
Normal file
57
vidocp/utils/text.py
Normal file
@ -0,0 +1,57 @@
|
||||
import cv2
|
||||
|
||||
|
||||
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)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Returns:
|
||||
Image with primary text removed.
|
||||
|
||||
References:
|
||||
https://stackoverflow.com/questions/58349726/opencv-how-to-remove-text-from-background
|
||||
"""
|
||||
|
||||
def is_likely_primary_text_segments(cnt):
|
||||
return 800 < cv2.contourArea(cnt) < 15000
|
||||
|
||||
image = image.copy()
|
||||
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
thresh = cv2.threshold(gray, 253, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
||||
|
||||
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 3))
|
||||
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, close_kernel, iterations=1)
|
||||
|
||||
dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
|
||||
dilate = cv2.dilate(close, dilate_kernel, iterations=1)
|
||||
|
||||
cnts, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
|
||||
cnts = filter(is_likely_primary_text_segments, cnts)
|
||||
|
||||
return cnts
|
||||
@ -144,62 +144,6 @@ def vec_rect_to_xywh(rect):
|
||||
return x, y, w, h
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Returns:
|
||||
Image with primary text removed.
|
||||
|
||||
References:
|
||||
https://stackoverflow.com/questions/58349726/opencv-how-to-remove-text-from-background
|
||||
"""
|
||||
|
||||
def is_likely_primary_text_segments(cnt):
|
||||
return 800 < cv2.contourArea(cnt) < 15000
|
||||
|
||||
image = image.copy()
|
||||
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
thresh = cv2.threshold(gray, 253, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
|
||||
|
||||
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 3))
|
||||
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, close_kernel, iterations=1)
|
||||
|
||||
dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
|
||||
dilate = cv2.dilate(close, dilate_kernel, iterations=1)
|
||||
|
||||
cnts, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
|
||||
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)
|
||||
|
||||
return image
|
||||
|
||||
|
||||
def detect_large_coherent_structures(image: np.array):
|
||||
"""Detects large coherent structures on an image.
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user