1 | import cv2 |
---|
2 | import numpy as np |
---|
3 | |
---|
4 | # read input |
---|
5 | img = cv2.imread('white_black_text_threshold.png') |
---|
6 | |
---|
7 | # convert to hsv and extract saturation |
---|
8 | hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) |
---|
9 | sat = hsv[:,:,1] |
---|
10 | |
---|
11 | # threshold and invert |
---|
12 | thresh = cv2.threshold(sat, 10, 255, cv2.THRESH_BINARY)[1] |
---|
13 | thresh = 255 - thresh |
---|
14 | |
---|
15 | # apply morphology dilate |
---|
16 | kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15)) |
---|
17 | thresh = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel) |
---|
18 | thresh3 = cv2.merge([thresh,thresh,thresh]) |
---|
19 | |
---|
20 | # do inpainting |
---|
21 | result1 = cv2.inpaint(img,thresh,11,cv2.INPAINT_TELEA) |
---|
22 | result2 = cv2.inpaint(img,thresh,11,cv2.INPAINT_NS) |
---|
23 | result3 = np.where(thresh3!=0, img, thresh3) |
---|
24 | cv2.xphoto.inpaint(img, thresh, result3, cv2.xphoto.INPAINT_FSR_FAST) |
---|
25 | |
---|
26 | |
---|
27 | # save results |
---|
28 | cv2.imwrite('white_black_text_threshold.png', thresh) |
---|
29 | cv2.imwrite('white_black_text_inpainted1.png', result1) |
---|
30 | cv2.imwrite('white_black_text_inpainted2.png', result2) |
---|
31 | |
---|
32 | # show results |
---|
33 | cv2.imshow('thresh',thresh) |
---|
34 | cv2.imshow('result1',result1) |
---|
35 | cv2.imshow('result2',result2) |
---|
36 | cv2.imshow('result3',result3) |
---|
37 | cv2.waitKey(0) |
---|
38 | cv2.destroyAllWindows() |
---|