-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrowHead.py
259 lines (215 loc) · 6.62 KB
/
arrowHead.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import argparse
import matplotlib.pyplot as plt
import numpy
from PIL import Image
from PIL import ImageOps
def fn(a):
if a:
return a
return 1
def readMat(input_file):
print "Reading Matrix..."
np = numpy.loadtxt(input_file)
print "Finished Reading Matrix..."
return np
def normalize(np):
print "Normalizing Matrix..."
sumRow = np.sum(axis=0)
sumCol = np.sum(axis=1)
row,col = np.shape
for r in range(row):
for c in range(col):
np[r][c] = np[r][c]/fn(sumRow[r]*sumCol[c])
print "Finished Normalizing Matrix..."
def display(np, filename):
print "About to display image"
# Write image to file
im = Image.fromarray(numpy.uint8(plt.cm.gist_earth(np)*255))
im.save(filename)
def computeArrowHead(np):
print "Computing ArrowHead..."
A = numpy.zeros(np.shape)
row,col = np.shape
for r in range(row):
for c in range(col):
d = c - r
if r-d >= col or r-d<0:
continue
A[r][r+d] = (np[r][r-d]-np[r][r+d])/fn(np[r][r-d]+np[r][r+d])
print "Finished Computing ArrowHead..."
return A
def sgn(x):
if x < 0:
return -1
elif x > 0:
return 1
return 0
def computeUsgn(A):
print "Computing Usgn..."
Usgn = numpy.zeros(A.shape)
row,col = A.shape
for r in range(row):
Usgn[r][r]=A[r][r]
# Compute U[a][b]
for d in range(1,row):
for r in range(row):
if r+d < col:
Usgn[r][r+d] = Usgn[r][r+d-1] + numpy.sign(A[r:r+d/2+1,r+d]).sum()
print "Finished Computing Usgn..."
return Usgn
def computeLsgn(A):
print "Computing Lsgn..."
Lsgn = numpy.zeros(A.shape)
row,col = A.shape
for r in range(row):
Lsgn[r][r]=A[r][r]
# compute L[a][b]
for d in range(1, row):
for r in range(row):
if r+d >= col or 2*(r+d)-r>=col:
break
Lsgn[r][r+d] = Lsgn[r][r+d-1] + numpy.sign(A[r+d, r+d:2*(r+d)-r+1]).sum()
print "Finished Computing Lsgn..."
return Lsgn
def sum_sign(A):
ss = 0
for x in numpy.nditer(A):
if x < 0:
ss = ss - 1
elif x > 0:
ss = ss + 1
return ss
def normalizeS(S):
return S/S.max()
def computeSsign(Usgn, Lsgn):
print "Computing Ssign"
Ssign = Lsgn - Usgn
print "Finished computing Ssign"
return Ssign
def computeU(A):
print "Computing U"
U = numpy.zeros(A.shape)
row,col = A.shape
for r in range(row):
U[r][r]=A[r][r]
# Compute U[a][b]
for d in range(1,row):
for r in range(row):
if r+d < col:
U[r][r+d] = U[r][r+d-1] + A[r:r+d/2+1,r+d].sum()
print "Finished Computing U..."
return U
def computeL(A):
print "Computing L..."
L = numpy.zeros(A.shape)
row,col = A.shape
for r in range(row):
L[r][r]=A[r][r]
# compute L[a][b]
for d in range(1, row):
for r in range(row):
if r+d >= col or 2*(r+d)-r>=col:
break
L[r][r+d] = L[r][r+d-1] + A[r+d, r+d:2*(r+d)-r+1].sum()
print "Finished Computing L..."
return L
def computeSsum(U, L):
print "Computing Ssum..."
Ssum = L - U
print "Finished Computing Ssum..."
return Ssum
def computeSvar(A, U, L):
print "Computing Svar..."
Sx = U + L
countU = numpy.zeros(A.shape)
countL = numpy.zeros(A.shape)
Sx2 = numpy.zeros(A.shape)
Svar = numpy.zeros(A.shape)
sq = A**2
(row,col) = A.shape
for d in range(1,row):
for r in range(row):
if r+d >= col :
continue
countU[r][r+d] = countU[r][r+d-1] + A[r:r+d/2+1, r+d].size
if 2*(r+d)-r >= col:
continue
countL[r][r+d] = countL[r][r+d-1] + A[r+d, r+d:2*(r+d)-r+1].size
Sx2[r][r+d] = Sx2[r][r+d-1] + sq[r:r+d/2+1, r+d].sum() + sq[r+d, r+d:2*(r+d)-r+1].sum()
for d in range(1, row):
for r in range(row):
if r+d >= col or 2*(r+d) >= col:
break
Svar[r][r+d] = (Sx2[r][r+d])/(countU[r][r+d]+countL[r][r+d]) -(Sx[r][r+d]/(countU[r][r+d]+countL[r][r+d]))**2
print "Finished Computing Svar..."
return (Svar,countU,countL)
def getCornerScore(Ssign, Ssum, Svar):
print "Computing Scorner..."
Scorner = Ssign + Ssum + Svar
print "Finished Computing Scorner..."
return Scorner
def getAllMat(A):
Usgn = computeUsgn(A)
Lsgn = computeLsgn(A)
U = computeU(A)
L = computeL(A)
Ssign = computeSsign(Usgn, Lsgn)
Ssum = computeSsum(U,L)
(Svar, countU, countL) = computeSvar(A, U, L)
Ssign = normalizeS(Ssign)
Ssum = normalizeS(Ssum)
Svarn = normalizeS(Svar)
Scorner = getCornerScore(Ssign, Ssum, Svarn)
newFn = numpy.vectorize(fn)
NewCountU = newFn(countU)
NewCountL = newFn(countL)
MeanSgnU = Usgn/NewCountU;
MeanSgnL = Lsgn/NewCountL;
return (Usgn, Lsgn, countU, countL, Svar, Svarn, Scorner, MeanSgnU, MeanSgnL)
def main(args):
np = readMat(args.input_data)
apply_threshold1 = args.apply_threshold1
if apply_threshold1 is 'y':
t1=float(args.t1)
t2=float(args.t2)
t3=float(args.t3)
apply_threshold2 = args.apply_threshold2
if apply_threshold2 is 'y':
t4=float(args.t4)
t5=float(args.t5)
if args.is_normal is 'n':
normalize(np)
A = computeArrowHead(np)
display(A,"A.jpg")
(Usgn, Lsgn, countU, countL, Svar, Svarn, Scorner, MeanSgnU, MeanSgnL) = getAllMat(A)
if apply_threshold1 is 'y':
print "First stage filtering begin :"
for x, y in numpy.ndindex(Scorner.shape):
if Svar[x][y]<t1 and MeanSgnU[x][y]<-t2 and MeanSgnL[x][y]>t3:
continue
else:
Scorner[x][y]=0
print "First stage filtering end."
if apply_threshold2 is 'y':
print "Second stage filtering begin :"
for x, y in numpy.ndindex(Scorner.shape):
if MeanSgnU[x][y]<-t4 and MeanSgnL[x][y]>t5:
continue
else:
Scorner[x][y]=0
print "Second stage filtering end."
display(Scorner,"Scorner.jpg")
numpy.savetxt('ScornerData',Scorner)
if __name__== "__main__":
parser = argparse.ArgumentParser(description="Arrowhead")
parser.add_argument("--input_data")
parser.add_argument("--is_normal")
parser.add_argument("--t1")
parser.add_argument("--t2")
parser.add_argument("--t3")
parser.add_argument("--t4")
parser.add_argument("--t5")
parser.add_argument("--apply_threshold1")
parser.add_argument("--apply_threshold2")
args = parser.parse_args()
main(args)