refactored figure detection once
This commit is contained in:
parent
8432cfe514
commit
d555e86475
@ -18,11 +18,29 @@ def is_likely_figure(cont, min_area=5000, max_width_to_hight_ratio=6):
|
||||
return is_large_enough(cont, min_area) and has_acceptable_format(cont, max_width_to_hight_ratio)
|
||||
|
||||
|
||||
def detect_figures(image: np.array):
|
||||
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.
|
||||
|
||||
References:
|
||||
https://stackoverflow.com/questions/58349726/opencv-how-to-remove-text-from-background
|
||||
"""
|
||||
|
||||
def filter_likely_primary_text_segments(cnts):
|
||||
for c in cnts:
|
||||
area = cv2.contourArea(c)
|
||||
if area > 800 and area < 15000:
|
||||
yield cv2.boundingRect(c)
|
||||
|
||||
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))
|
||||
@ -33,16 +51,19 @@ def detect_figures(image: np.array):
|
||||
|
||||
cnts, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
|
||||
def filter_rects():
|
||||
for c in cnts:
|
||||
area = cv2.contourArea(c)
|
||||
if area > 800 and area < 15000:
|
||||
yield cv2.boundingRect(c)
|
||||
|
||||
for rect in filter_rects():
|
||||
for rect in filter_likely_primary_text_segments(cnts):
|
||||
x, y, w, h = rect
|
||||
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.
|
||||
|
||||
References:
|
||||
https://stackoverflow.com/questions/60259169/how-to-group-nearby-contours-in-opencv-python-zebra-crossing-detection
|
||||
"""
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
thresh = cv2.threshold(gray, 253, 255, cv2.THRESH_BINARY)[1]
|
||||
@ -55,8 +76,18 @@ def detect_figures(image: np.array):
|
||||
|
||||
cnts, _ = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
return cnts
|
||||
|
||||
|
||||
def detect_figures(image: np.array):
|
||||
|
||||
image = image.copy()
|
||||
|
||||
image = remove_primary_text_regions(image)
|
||||
cnts = __detect_large_coherent_structures(image)
|
||||
|
||||
cnts = filter(is_likely_figure, cnts)
|
||||
rects = [cv2.boundingRect(c) for c in cnts]
|
||||
rects = map(cv2.boundingRect, cnts)
|
||||
rects = remove_included(rects)
|
||||
|
||||
return rects
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user