Created
May 13, 2020 11:06
-
-
Save Ddedalus/048537d7a576364457f9f0f0b6962c0f to your computer and use it in GitHub Desktop.
Remove background from images of black text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import glob | |
import sys | |
import cv2 | |
import numpy as np | |
# %% | |
def remove_background(frame): | |
# convert the BGR color space of image to HSV color space | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
# Threshold of black-ish in HSV space | |
lower_blue = np.array([0, 0, 0]) | |
upper_blue = np.array([360, 150, 150]) | |
# mask contains the text, but not the background | |
mask = cv2.inRange(hsv, lower_blue, upper_blue) | |
# smooth the edges a little bit | |
mask = cv2.GaussianBlur(mask, (7, 7), sigmaX=0.2) | |
# invert colors, mask to cut out the background, inverse again | |
n_frame = cv2.bitwise_not(frame) # white text, black bg | |
result = cv2.bitwise_or(n_frame, n_frame, mask=mask) | |
result = cv2.bitwise_not(result) # text is black again | |
return result | |
# %% convert all files in a directory | |
folder = "./doc/word/media" | |
for p in glob.glob(f"{folder}/*.png"): | |
frame = cv2.imread(p) | |
res = remove_background(frame) | |
cv2.imwrite(p, res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment