Search code examples
pythonopencvimage-processingscikit-image

Removing White Text with Black Borders From Image


I am trying to remove text from images that has a black border with white fill. Take the image below as an example.

enter image description here

I have tried a few options utilizing opencv and skimage inpaint

import cv2
from skimage.restoration import inpaint
img = cv2.imread('Documents/test_image.png')
mask = cv2.threshold(img, 210, 255, cv2.THRESH_BINARY)[1][:,:,0]
dst = cv2.inpaint(img, mask, 7, cv2.INPAINT_TELEA)
image_result = inpaint.inpaint_biharmonic(img, mask,
                                          multichannel=True)
cv2.imshow('image',img)
cv2.imshow('mask',mask)
cv2.imshow('dst',dst)
cv2.imshow('image_result',image_result)
cv2.waitKey(0)

enter image description here

enter image description here

enter image description here

It seems like the inpainting is just trying to fill with black as that is what it is identifying as being around the areas of interest. What I would like to do is remove the white text and black borders completely, or secondarily try to fill the white with more information from surrounding colors than just the black.


Solution

  • Here are two inpainting methods in Python/OpenCV. Note that I use the saturation channel to create the threshold, since white and black have zero saturation, in principle.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read input
    img = cv2.imread('white_black_text.png')
    
    # convert to hsv and extract saturation
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    sat = hsv[:,:,1]
    
    # threshold and invert
    thresh = cv2.threshold(sat, 10, 255, cv2.THRESH_BINARY)[1]
    thresh = 255 - thresh
    
    # apply morphology dilate
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
    thresh = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
    
    # do inpainting
    result1 = cv2.inpaint(img,thresh,11,cv2.INPAINT_TELEA)
    result2 = cv2.inpaint(img,thresh,11,cv2.INPAINT_NS)
    
    # save results
    cv2.imwrite('white_black_text_threshold.png', thresh)
    cv2.imwrite('white_black_text_inpainted1.png', result1)
    cv2.imwrite('white_black_text_inpainted2.png', result1)
    
    # show results
    cv2.imshow('thresh',thresh)
    cv2.imshow('result1',result1)
    cv2.imshow('result2',result2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Threshold and morphology cleaned result:

    enter image description here

    Result 1 (Telea):

    enter image description here

    Result 2 (Navier Stokes):

    enter image description here