-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanImage.py
136 lines (89 loc) · 2.22 KB
/
cleanImage.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
minWordLength = 30
minCharLength = 10
maxCharLength = 25
optBrightness = 0.96119084618392
optThresh = 200#change this to 200
# In[3]:
import cv2
import numpy as np
# In[4]:
def getCleaned(file):
def seperateCols(img):
blue = []
green = []
red = []
for i in range(len(img)):
tBlue = []
tGreen = []
tRed = []
for j in range(len(img[0])):
col = img[i,j]
tBlue.append(col[0])
tGreen.append(col[1])
tRed.append(col[2])
blue.append(tBlue)
green.append(tGreen)
red.append(tRed)
return (np.array(blue),np.array(green),np.array(red))
# In[5]:
def getThresh(img):
isColor = False
if(len(img[0,0])==3):
isColor = True
br = getBrightness(img, isColor)
thresh = optThresh/optBrightness*br
return thresh
# In[6]:
def getBrightness(img,isColor = False):
total = 0
if(isColor):
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
for i in img:
total+=sum(i)
avg = total/(255*len(img)*len(img[0]))
return avg
# In[7]:
# In[8]:
def threshhold(img, tc, mc=(0,0,0), gc=(255,255,255)):
#first is the threshhold value, the second is the one to demote all
#the values lesser than thresh hold and the last is for the one to promote to if it is greater
(t_r,t_g,t_b) = tc
(mr,mg,mb) = mc
(gr,gg,gb) = gc
for i in range(len(img)):
for j in range(len(img[0])):
col = img[i,j]
if (col[2]<=t_r) and (col[1]<=t_g) and (col[0]<=t_b):
img[i,j] = [mb,mg,mr]
else:
img[i,j] = [gb,gg,gr]
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return img
# In[10]:
def notEmpty(img):
emp = True
for i in img:
for j in i:
if j == 255:
emp = False
return not emp
# In[11]:
def op(tmp):
tmp = cv2.erode(tmp,np.ones((3,3)),iterations = 1)
tmp = cv2.dilate(tmp,np.ones((2,2)),iterations = 1)
return tmp
# In[12]:
img = cv2.imread(file)
thresh = getThresh(img)
img = threshhold(img,(thresh,thresh,thresh))
skel = np.zeros([len(img),len(img[0])],dtype = np.uint8)
tmp = 255 - img
print(tmp.shape,skel.shape)
while notEmpty(tmp):
skel = cv2.bitwise_or(skel, cv2.bitwise_and(tmp,cv2.bitwise_not(op(tmp))))
tmp = cv2.erode(tmp,np.ones((3,3)),iterations = 1)
img = 255-skel
return img