-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrext.py
166 lines (127 loc) · 4.56 KB
/
rext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
'''
REXT - V 1.0
Text Remover Library
A Python library to remove text from images
First version of this software thats object is remove
text from specific images of an competition dataset
Use as Script:
- Use rext.py -h to see how to use this script
- You can call this script by terminal with this command line:
- OBS: Without '<' '>'
python3 rext.py -i <input_image>
Use as Library:
- For library uses, import this Library and call the clean function
passing an image as an argument. The return is a clean image.
- Params: clean(image, show)
- If show = True, you can see all processing
import rext.py
img = X
new_img = rext.clean(img)
'''
import numpy as np
from matplotlib import pyplot as plt
import cv2
import sys
import getopt
# Clean Function
# The core funcition of this script
def clean(img, show = False):
# Option defines which InPainting method will be used
# If option = 1, will use TELEA method
# If option = 2, will use NS method
option = 1
# Defines the radio of InPainting
radius = 3
# Some Params
# for LaPlace operator
ddepth = cv2.CV_16S
kernel_size = 15
# Kernel for morfoligy operations
# 3 by 3 and 8-conected
kernel = np.ones((3,3),np.uint8)
## IMAGE PROCESSING
# Converting image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Canny Edge Implementation
canny = cv2.Canny(gray,900,1000)
# Implementation of LaPlace Operator
lp = cv2.Laplacian(canny, ddepth, ksize=kernel_size)
# Image binary convertion
_, bc = cv2.threshold(lp, 127, 255, cv2.THRESH_BINARY)
#Modifying the morphology of the image
#Dilating
di = cv2.dilate(bc, kernel, iterations = 4)
# 8-bit transform
final_mask = np.uint8(di)
# InPaint implementation
ip1 = cv2.inpaint(img, final_mask, radius, cv2.INPAINT_TELEA)
ip2 = cv2.inpaint(img, final_mask, radius, cv2.INPAINT_NS)
if option == 2:
final = cv2.cvtColor(ip2, cv2.COLOR_RGB2BGR)
else:
final = cv2.cvtColor(ip1, cv2.COLOR_RGB2BGR)
# Show the result (optional)
# To show with matplotlib
# an visual result before
# a final image
if show == True:
plt.subplot(421),plt.imshow(img, cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(422),plt.imshow(gray, cmap = 'gray')
plt.title('Grayscale Image'), plt.xticks([]), plt.yticks([])
plt.subplot(423),plt.imshow(canny, cmap = 'gray')
plt.title('Canny Edge'), plt.xticks([]), plt.yticks([])
plt.subplot(424),plt.imshow(lp, cmap = 'gray')
plt.title('LaPlace Operator'), plt.xticks([]), plt.yticks([])
plt.subplot(425),plt.imshow(bc, cmap = 'gray')
plt.title('Binary Mask'), plt.xticks([]), plt.yticks([])
plt.subplot(426),plt.imshow(di, cmap = 'gray')
plt.title('Final Mask'), plt.xticks([]), plt.yticks([])
plt.subplot(427),plt.imshow(ip1, cmap = 'gray')
plt.title('InPainting "TELEA"'), plt.xticks([]), plt.yticks([])
plt.subplot(428),plt.imshow(ip2, cmap = 'gray')
plt.title('InPainting "NS"'), plt.xticks([]), plt.yticks([])
plt.show()
return final
def main(argv):
# Path to the image to be processed
image_path = ''
# Name of final image
filename = ''
# If True, shows all steps of image processing
show = False
# Recieving
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile="])
except getopt.GetoptError:
print('python3 rext.py -i <input_img_path>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('python3 rext.py -i <input_image>')
sys.exit()
elif opt in ("-i", "--ifile"):
image_path = arg
filename = 'new_' + image_path
# Using cv2.imread() method
# to read the image
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
if image is None:
print("Use rext.py -h to help")
sys.exit("Could not read the image.")
else:
# Init program print's
print("REXT - A image remove text script")
print("")
print("Image to process:", image_path)
print("Image shape: ", image.shape)
print("Final file name: ", filename)
# Converting from BGR to RGB
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Calling the function
final_img = clean(img, show)
# Saving the final image
cv2.imwrite(filename, final_img)
if __name__ == "__main__":
main(sys.argv[1:])