-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstereoBM.py
More file actions
215 lines (171 loc) · 7.05 KB
/
stereoBM.py
File metadata and controls
215 lines (171 loc) · 7.05 KB
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
import os
from algorithm_template import RangeEstimator
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from matplotlib import colors
import re
class StereoBM(RangeEstimator):
def __init__(self):
""" Initialize and calibrate the algorithm."""
self.isStereo = True
# initialize the stereoBM object
sbmParams = {
'SWS': 5, #SADWindowSize
'PFS': 5, #PreFilterSize
'PFC': 29, #PreFiltCap
'MDS': 0, #MinDisparity
'NOD': 240, #NumDisparities
'TTH': 100, #TxtrThrshld
'UR': 2, #UniquenessRatio
'SR': 15, #SpklRng
'SPWS': 30, #SpklWinSize
}
sbm = cv.StereoBM_create(numDisparities=16, blockSize=15)
sbm.setPreFilterType(1)
sbm.setPreFilterSize(sbmParams['PFS'])
sbm.setPreFilterCap(sbmParams['PFC'])
sbm.setMinDisparity(sbmParams['MDS'])
sbm.setNumDisparities(sbmParams['NOD'])
sbm.setTextureThreshold(sbmParams['TTH'])
sbm.setUniquenessRatio(sbmParams['UR'])
sbm.setSpeckleRange(sbmParams['SR'])
sbm.setSpeckleWindowSize(sbmParams['SPWS'])
self.Algorithm = sbm
def getImage(self, basename):
""" Returns image(s) to be used by range estimator. If the algorithm is stereo, return a tuple of left and right images. If it's monocular, return only the left image.)
(Customize it based on whether it's monocular or stereo but follow this example format:
imgL = cv.imread('images/' + basename + '/im0.png', 0)
)
"""
imgL = cv.imread('images/' + basename + '/im0.png', 0) # type Mat
imgR = cv.imread('images/' + basename + '/im1.png', 0)
return imgL, imgR
def estimateRange(self, basename):
""" Return estimated distance in an nparray"""
estDisp = self.estimateDisp(basename)
estRange = self.dispToRange(basename, estDisp)
return estRange
def getTrueRange(self, basename):
""" Return true distance in an nparray"""
trueDisp = self.read_pfm(basename)
trueDisp = np.asarray(trueDisp)
trueRange = self.dispToRange(basename, trueDisp)
return trueRange
def computeDiff(self, basename):
""" Return the MSE for the image"""
estRange = self.estimateRange(basename)
trueRange = self.getTrueRange(basename)
diff = np.subtract(estRange, trueRange)
return diff
def computeMSE(self, basename):
diff = self.computeDiff(basename)
sqdiff = np.square(diff)
mse = np.ndarray.mean(sqdiff)
return mse
def estimateRangeMany(self, imageFolder):
""" Return estimated distance for all images in a folder
"""
pass
def computeErrorMany(self, imageFolder):
""" Return MSE for all images in a folder
"""
pass
def plot(self, basename):
""" visualize true range, estimated range, and error"""
imgL, imgR = self.getImage(basename)
estRange = self.estimateRange(basename)
trueRange = self.getTrueRange(basename)
print(estRange.dtype)
print(trueRange.dtype)
diff = np.subtract(estRange, trueRange)
mse = self.computeMSE(basename)
images = []
fig, axs = plt.subplots(1, 4)
fig.suptitle(
basename + "; MSE = " + str(mse) +
"; note: pink error is underestimated, green is overestimated")
axs[0].axis('off')
axs[1].axis('off')
axs[2].axis('off')
axs[3].axis('off')
axs[0].imshow(imgL, 'gray')
axs[1].set_title("estRange")
estRangeAx = axs[1]
images.append(estRangeAx.imshow(estRange, cmap='plasma'))
axs[2].set_title("trueRange")
trueRangeAx = axs[2]
images.append(trueRangeAx.imshow(trueRange, cmap='plasma'))
axs[3].set_title("error")
errorAx = axs[3].imshow(diff, 'PiYG')
cax1 = plt.axes([0.3, 0.7, 0.3, 0.01])
cax2 = plt.axes([0.9, 0.4, 0.01, 0.3])
vmin = min(image.get_array().min() for image in images)
vmax = max(image.get_array().max() for image in images)
norm = colors.Normalize(vmin=vmin, vmax=vmax)
for im in images:
im.set_norm(norm)
fig.colorbar(images[0], cax=cax1, orientation='horizontal')
fig.colorbar(errorAx, cax=cax2)
plt.show()
def plotMany(self, imageFolder):
""" visualize true range, estimated range, and error for all images in
a folder.
"""
pass
############################################################################
# Define other helper functions as needed for your algorithm
############################################################################
def getCalibData(self, basename):
calibPath = 'images/' + basename + '/calib.txt'
calibData = {}
with open(calibPath) as f:
for line in f:
(key, val) = line.strip().split('=')
calibData[key] = val
fx = float(calibData['cam0'][1:].split()[0])
baseline = float(calibData['baseline'])
doffs = float(calibData['doffs'])
return fx, baseline, doffs
def estimateDisp(self, basename):
""" Return estimated disparity in an nparray"""
imgL, imgR = self.getImage(basename)
estDisp = self.Algorithm.compute(imgL, imgR) / 16
return estDisp
def dispToRange(self, basename, disp):
fx, baseline, doffs = self.getCalibData(basename)
range = np.zeros(shape=disp.shape).astype(float) # initialize np
range[disp > 0] = (fx * baseline) / (doffs + disp[disp > 0]
) # populate np
return range
def read_pfm(self, basename):
pfm_file_path = 'images/' + basename + '/disp0.pfm'
with open(pfm_file_path, 'rb') as pfm_file:
header = pfm_file.readline().decode().rstrip()
channels = 3 if header == 'PF' else 1
dim_match = re.match(r'^(\d+)\s(\d+)\s$',
pfm_file.readline().decode('utf-8'))
if dim_match:
width, height = map(int, dim_match.groups())
else:
raise Exception("Malformed PFM header.")
scale = float(pfm_file.readline().decode().rstrip())
if scale < 0:
endian = '<' # littel endian
scale = -scale
else:
endian = '>' # big endian
disparity = np.fromfile(pfm_file, endian + 'f')
img = np.reshape(disparity, newshape=(height, width))
img = np.flipud(img).astype('uint8')
#
# plt.show(img, "disparity")
# png_file_path = pfm_file_path[:-4] + ".png"
# plt.imsave(os.path.join(png_file_path), img)
# return disparity, [(height, width, channels), scale]
return img
# for testing purposes
if __name__ == "__main__":
stereoTest = StereoBM()
basename = "Backpack-imperfect"
stereoTest.plot(basename)