-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
executable file
·235 lines (160 loc) · 7.83 KB
/
test.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
import numpy as np
import matplotlib.pyplot as plt
import cv2
from os import listdir
import os
import scipy.io as sio
from learned_filters import learnedFilters as LRFS
import time
def normalize(image):
#print(np.min(image), np.max(image))
minVal = np.min(image)
if minVal < 0.0:
image += np.abs(minVal)
maxVal = np.max(image)
image /= maxVal
return image
def processSingleImage(count, fileName, allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, flagSaveVisuals):
filterSzHalf = int(filterSz/2.0)
image_org = cv2.imread(fileName)
image = cv2.copyMakeBorder(image_org,filterSzHalf,filterSzHalf,filterSzHalf,filterSzHalf,cv2.BORDER_REFLECT)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny_mask = cv2.Canny(np.array(image_gray, dtype=np.uint8), 50.0,220.0)
# intermediate output for debugging/visualization
if flagSaveVisuals:
cv2.imwrite(dirForImageOutput + str(count) + '_0.png',np.array(image, dtype=np.uint8))
cv2.imwrite(dirForImageOutput + str(count) + '_1.png',np.array(canny_mask, dtype=np.uint8))
lrfObj = LRFS(image, image_gray, canny_mask, coBins, srBins, orBins, filterSz)
mask = lrfObj.generateOutput(allFilters)
# gradient strength -- computed as part of ST
strImage = lrfObj.strength.copy()
strImage = normalize(strImage)
mask[mask < 0.0] = 0.0
mask[mask > 255.0] = 255.0
# intermediate output for debugging/visualization
if flagSaveVisuals:
maskRGB = generateHeatMap(mask)
cv2.imwrite(dirForImageOutput + str(count) + '_2.png',np.array(maskRGB, dtype=np.uint8))
strImage_orgSz = strImage.copy()
strImage_orgSz *= 255.0
cv2.imwrite(dirForImageOutput + str(count) + '_3.png',np.array(strImage_orgSz, dtype=np.uint8))
# saving the original size DCSI on which need to run shortest path
maskDCSI = generateDenseMap(mask,strImage)
H, W = maskDCSI.shape
maskDCSI_orgSz = maskDCSI[filterSzHalf:H-filterSzHalf, filterSzHalf:W-filterSzHalf].copy()
sio.savemat(dirForMatFiles + str(count) + '.mat', {'maskDCSI_orgSz':maskDCSI_orgSz})
# intermediate output for debugging/visualization
if flagSaveVisuals:
maskDCSI /= 2.0
maskDCSI *= 255.0
cv2.imwrite(dirForImageOutput + str(count) + '_4.png',np.array(maskDCSI, dtype=np.uint8))
return
def runInference(allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, datasetName, flagSaveVisuals):
if datasetName == 'Basalt':
totalTime = 0.0
numImages = 45
for imgIndex in range(1,numImages+1):
print(imgIndex)
if imgIndex < 10:
fileName = './data/Basalt/images/marsim000' + str(imgIndex) + '.pgm'
else:
fileName = './data/Basalt/images/marsim00' + str(imgIndex) + '.pgm'
start = time.time()
processSingleImage(imgIndex, fileName, allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, flagSaveVisuals)
end = time.time()
totalTime += (end - start)
print('Total Time: ', totalTime)
print('Average Time: ', totalTime/numImages)
elif datasetName == 'Web':
totalTime = 0.0
numImages = 80
for imgIndex in range(1,numImages+1):
print(imgIndex)
if imgIndex < 10:
fileName = './data/web_dataset/images/R_GImag000' + str(imgIndex) + '.bmp'
else:
fileName = './data/web_dataset/images/R_GImag00' + str(imgIndex) + '.bmp'
start = time.time()
processSingleImage(imgIndex, fileName, allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, flagSaveVisuals)
end = time.time()
totalTime += (end - start)
print('Total Time: ', totalTime)
print('Average Time: ', totalTime/numImages)
elif datasetName == 'CH1':
totalTime = 0.0
numImages = 203
imgIndex = 1
basePathNameList = ['./data/CH1/panoramio/images', './data/CH1/cvg/images', './data/CH1/poor_edge_images/images']
for index in range(0,3):
basePathName = basePathNameList[index]
listOfFiles = listdir(basePathName)
for fName in listOfFiles:
fileName = os.path.join(basePathName, fName)
print(imgIndex, fileName)
start = time.time()
processSingleImage(imgIndex, fileName, allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, flagSaveVisuals)
end = time.time()
totalTime += (end - start)
imgIndex += 1
print('Total Time: ', totalTime)
print('Average Time: ', totalTime/numImages)
else:
# for GeoPose3K dataset
totalTime = 0.0
numImages = 2895
mat_contents = sio.loadmat('./misc/NamesOf2895Files.mat')
basePathName = './data/geoPose3K_rescaled/'
for imgIndex in range(0,numImages):
dirName = os.path.join(basePathName,str(mat_contents["B"][imgIndex][0][0][0]))
fileName = os.path.join(dirName,"photo.jpg")
print(imgIndex+1, fileName)
start = time.time()
processSingleImage(imgIndex+1, fileName, allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, flagSaveVisuals)
end = time.time()
totalTime += (end - start)
print('Total Time: ', totalTime)
print('Average Time: ', totalTime/numImages)
return
def generateDenseMap(image, srImage):
H, W = image.shape
hMap = 255.0 * np.zeros(shape=(H,W), dtype=np.float32)
for row in range(0,H):
for col in range(0,W):
tempVal = 0.5 * (1.0 - image[row,col] / 255.0) + 0.5 * (1.0 - srImage[row,col])
hMap[row,col] = tempVal
return hMap
def generateHeatMap(image):
H, W = image.shape
hMap = 255.0 * np.zeros(shape=(H,W,3), dtype=np.float32)
for row in range(0,H):
for col in range(0,W):
if image[row,col] > 0.0:
tempVal = image[row,col] / 255.0 - 0.5
if tempVal <= 0.0:
hMap[row,col,0] = np.abs(tempVal+0.5) * 510.0
hMap[row,col,1] = 0.0
hMap[row,col,2] = 0.0
else:
hMap[row,col,2] = np.abs(tempVal) * 510.0
hMap[row,col,0] = 0.0
hMap[row,col,1] = 0.0
return hMap
def main():
srBins = 6
coBins = 3
orBins = 16
filterSz = 7 #9 #11 #13 #15
datasetName_Filter = 'Basalt' #'Basalt' #'Web' #'CH1'
datasetName_Inference = 'Web' #'Basalt' #'Web' #'CH1' #'GeoPose3K'
fileNameFilters = './filterBank/' + datasetName_Filter + '_filterSz_' + str(filterSz) + '_filters.npy'
allFilters = np.load(fileNameFilters)
dirForImageOutput = './output/images_for_' + datasetName_Inference + '_FilterSz_7_trained_on_' + datasetName_Filter + '/'
dirForMatFiles = './output/mats_for_' + datasetName_Inference + '_FilterSz_7_trained_on_' + datasetName_Filter + '/'
if not os.path.exists(dirForImageOutput):
os.makedirs(dirForImageOutput)
if not os.path.exists(dirForMatFiles):
os.makedirs(dirForMatFiles)
runInference(allFilters, dirForImageOutput, dirForMatFiles, srBins, coBins, orBins, filterSz, datasetName_Inference, 1)
return
if __name__ == '__main__':
main()