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.

    References:
        https://stackoverflow.com/questions/58349726/opencv-how-to-remove-text-from-background
    """

    image = apply_threshold_to_image(image)

    threshold_image = image.copy()

    close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (17, 7))  # 20,3
    close = cv2.morphologyEx(image, cv2.MORPH_CLOSE, close_kernel, iterations=1)

    dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 3))  # 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_segment, cnts)

    rects = map(cv2.boundingRect, cnts)

    image = draw_bboxes(threshold_image, rects)
    return image


def apply_threshold_to_image(image):
    """Converts an image to black and white."""
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) > 2 else image
    return cv2.threshold(image, 253, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]


def is_likely_primary_text_segment(cnt):
    x, y, w, h = cv2.boundingRect(cnt)
    return 400 < cv2.contourArea(cnt) < 16000 or w / h > 3


def draw_bboxes(image, bboxes):
    for rect in bboxes:
        x, y, w, h = rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), -1)
    return image
