How to remove borders from image without losing image quality?

95 Views Asked by At

I have an image which contains borders for column headers and no border for data, now I want to remove the borders or lines from the image without losing its quality.

Here is the image

1

Here is the code I tried but I'm getting a black image where nothing is visible.

import cv2
import numpy as np

image = cv2.imread('your_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
_, thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY_INV)
kernel = np.ones((5, 5), np.uint8)
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel, iterations=2)
result = cv2.bitwise_and(image, image, mask=opened)

cv2.imshow('Original', image)
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is the outcome

2

I tried a few answers on Stack Overflow but they aren't fixing the problem; instead, they make the text lighter. How can I remove the border frames around the text?

1

There are 1 best solutions below

0
On

You can achieve that using different methods:

  1. Detect the text region using (Opencv Text detector module or other deep learning lib) , mask them and set other pixels to 255.

  2. detect the lines or segments using Hough line or LSD and then set their pixels to 255.

  3. Iterate horizontally and vertically checking for continuous unchanged pixels value pattern and set them to 255.