Ticket #66942: python_inpaint_text.py

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