6 from matplotlib
import pyplot
as plt
9 filename =
"D:/dataset/gt-4622.png" 10 if (len(sys.argv) > 1):
11 filename =
str(sys.argv[1])
13 fname, file_extension = os.path.splitext(filename)
19 if file_extension.lower() ==
".raw":
20 f = open(filename,
"r") 21 i = np.fromfile(f, dtype='uint16', sep=
"")
25 if (size / 1280 == 720):
28 elif (size / 848 == 480):
31 elif (size / 640 == 480):
35 print(
"Unknown dimentions")
36 if file_extension.lower() ==
".png":
37 i = cv.imread(filename, -1).astype(np.uint16)
41 m = np.percentile(i, 5)
42 M = np.percentile(i, 95)
44 i = i.reshape([height, width])
72 if event == cv.EVENT_LBUTTONDOWN:
75 elif event == cv.EVENT_LBUTTONUP:
100 cv.namedWindow(
"image")
101 cv.setMouseCallback(
"image", click_and_crop)
103 i = np.divide(i, np.array([M - m], dtype=np.float)).astype(np.float)
104 i = (i - m).astype(np.float)
106 i8 = (i * 255.0).astype(np.uint8)
109 i8 = cv.cvtColor(i8, cv.COLOR_BGRA2GRAY)
111 i8 = cv.equalizeHist(i8)
113 colorized = cv.applyColorMap(i8, cv.COLORMAP_JET)
115 colorized[i8 == int(m)] = 0
117 font = cv.FONT_HERSHEY_COMPLEX_SMALL
118 colorized = cv.putText(colorized,
str(m) +
" .. " +
str(M), (20,50), font, 1, (255, 255, 255), 2, cv.LINE_AA)
120 while cv.getWindowProperty(
'image', 0) >= 0:
121 im = colorized.copy()
123 if (x0 > 0
and x1 == 0):
124 im = cv.rectangle(im, (x0, y0), (xx, yy), (255, 255, 255), 2)
125 if (x0 > 0
and x1 > 0):
126 im = cv.rectangle(im, (x0, y0), (x1, y1), (255, 255, 255), 2)
129 crop = orig[int(my):int(My), int(mx):int(Mx)].astype(np.float)
135 Xcrop = np.zeros_like(crop).astype(np.float)
136 Ycrop = np.zeros_like(crop).astype(np.float)
137 Zcrop = np.zeros_like(crop).astype(np.float)
139 for i
in range(my, My):
140 for j
in range(mx, Mx):
141 z = crop[i - my, j - mx] * 0.001
142 x = (float(j) / width - 0.5) * z
143 y = (float(i) / height - 0.5) * z
148 Xcrop[i - my, j - mx] = x
149 Ycrop[i - my, j - mx] = y
150 Zcrop[i - my, j - mx] = z
152 xyz = np.dstack((X, Y, Z))
153 xyz = xyz.reshape(xyz.shape[0] * xyz.shape[1], xyz.shape[2])
155 eig_vals, eig_vecs = np.linalg.eig(C_x)
157 variance = np.min(eig_vals)
158 min_eig_val_index = np.argmin(eig_vals)
159 direction_vector = eig_vecs[:, min_eig_val_index].
copy()
161 rmse = math.sqrt(variance)
164 normal = direction_vector / np.linalg.norm(direction_vector)
166 point = np.mean(xyz, axis=0)
171 d = -np.dot(point, normal)
177 e = math.sqrt(a * a + b * b + c * c)
179 Dcrop = np.zeros_like(crop).astype(np.float)
181 for i
in range(my, My):
182 for j
in range(mx, Mx):
183 x = Xcrop[i - my, j - mx]
184 y = Ycrop[i - my, j - mx]
185 z = Zcrop[i - my, j - mx]
187 dist = abs(a * x + b * y + c * z + d) / e
191 Dcrop[i - my, j - mx] = dist
193 Dcrop = np.divide(Dcrop, (3 * rmse) / 255).astype(np.float)
194 Dcrop = np.clip(Dcrop, 0, 255).astype(np.uint8)
195 Dmap = np.dstack((Dcrop, Dcrop, Dcrop))
196 im[int(my):int(My), int(mx):int(Mx)] = Dmap
198 rmse_mm = rmse * 1000
199 im = cv.putText(im,
"%.2f mm" % rmse_mm, (mx + 2,my + 16), font, 1, (255, 255, 255), 2, cv.LINE_AA)
202 cv.imshow(
"image", im)
203 key = cv.waitKey(100) & 0xFF
205 cv.destroyAllWindows()
static std::string print(const transformation &tf)
def click_and_crop(event, x, y, flags, param)
void copy(void *dst, void const *src, size_t size)