Skip to content

Commit ee9f365

Browse files
committed
removed multiple prints of multilayer
1 parent 82474b1 commit ee9f365

4 files changed

Lines changed: 226 additions & 5697 deletions

File tree

openptv_python/multimed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def fast_multimed_r_nlay(
109109
it += 1
110110

111111
if it >= n_iter:
112-
print("multimed_r_nlay stopped after", n_iter, "iterations")
112+
# print("multimed_r_nlay stopped after", n_iter, "iterations")
113113
return 1.0
114114

115115
return 1.0 if r == 0 else float(rq / r)

openptv_python/orientation.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,13 @@ def orient(
495495
)
496496

497497
# Interpret the results
498-
print(
499-
f"Coefficients (beta): {beta} \n \
500-
Residuals: {residuals} \n \
501-
singular_values: {singular_values} \n \
502-
rank: {rank} \n \
503-
"
504-
)
498+
# print(
499+
# f"Coefficients (beta): {beta} \n \
500+
# Residuals: {residuals} \n \
501+
# singular_values: {singular_values} \n \
502+
# rank: {rank} \n \
503+
# "
504+
# )
505505

506506
# stopflag
507507
stopflag = True
@@ -571,9 +571,19 @@ def orient(
571571
omega = np.sum(resi * P * resi)
572572
sigmabeta[NPAR] = np.sqrt(omega / (n_obs - numbers))
573573

574+
575+
# if np.any(np.isnan(sigmabeta)):
576+
# pdb.set_trace()
577+
578+
# if np.any(np.isnan(X)):
579+
# pdb.set_trace()
580+
574581
XPX = np.linalg.inv(np.dot(X[:, :numbers].T, X[:, :numbers]))
575582

583+
584+
# import pdb; pdb.set_trace()
576585
for i in range(numbers):
586+
# print(f"{i=}, {np.sqrt(XPX[i][i]) = }")
577587
sigmabeta[i] = sigmabeta[NPAR] * np.sqrt(XPX[i][i])
578588

579589
if stopflag:
@@ -645,10 +655,10 @@ def raw_orient(
645655
) # , rcond=None)
646656

647657
# Interpret the results
648-
print("Coefficients (beta):", beta)
649-
print("Residuals:", residuals)
650-
print("rank:", rank)
651-
print("singular_values:", singular_values)
658+
# print("Coefficients (beta):", beta)
659+
# print("Residuals:", residuals)
660+
# print("rank:", rank)
661+
# print("singular_values:", singular_values)
652662

653663
stopflag = True
654664
for i in range(6):
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# %%
2+
# test calibration using scipy.optimize
3+
4+
# %%
5+
import copy
6+
7+
import numpy as np
8+
import scipy.optimize as opt
9+
10+
from openptv_python.calibration import Calibration
11+
from openptv_python.imgcoord import image_coordinates, img_coord
12+
from openptv_python.orientation import external_calibration, full_calibration
13+
from openptv_python.parameters import OrientPar, read_control_par
14+
from openptv_python.tracking_frame_buf import Target
15+
from openptv_python.trafo import arr_metric_to_pixel, pixel_to_metric
16+
17+
18+
def print_cal(cal: Calibration):
19+
print(cal.get_pos())
20+
print(cal.get_angles())
21+
print(cal.get_primary_point())
22+
print(cal.added_par)
23+
24+
control_file_name = "tests/testing_folder/corresp/control.par"
25+
# self.control = ControlPar(4)
26+
control = read_control_par(control_file_name)
27+
28+
# orient_par_file_name = "tests/testing_folder/corresp/orient.par"
29+
# orient_par = OrientPar().from_file(orient_par_file_name)
30+
31+
cal = Calibration().from_file(
32+
"tests/testing_folder/calibration/cam1.tif.ori",
33+
"tests/testing_folder/calibration/cam1.tif.addpar",
34+
)
35+
orig_cal = Calibration().from_file(
36+
"tests/testing_folder/calibration/cam1.tif.ori",
37+
"tests/testing_folder/calibration/cam1.tif.addpar",
38+
)
39+
40+
41+
42+
# def test_external_calibration(self):
43+
"""External calibration using clicked points."""
44+
ref_pts = np.array(
45+
[
46+
[-40.0, -25.0, 8.0],
47+
[40.0, -15.0, 0.0],
48+
[40.0, 15.0, 0.0],
49+
[40.0, 0.0, 8.0],
50+
]
51+
)
52+
53+
# Fake the image points by back-projection
54+
targets = arr_metric_to_pixel(
55+
image_coordinates(ref_pts, cal, control.mm),
56+
control,
57+
)
58+
59+
# Jigg the fake detections to give raw_orient some challenge.
60+
targets[:, 1] -= 0.1
61+
62+
external_calibration(cal, ref_pts, targets, control)
63+
64+
np.testing.assert_array_almost_equal(
65+
cal.get_angles(), orig_cal.get_angles(), decimal=3
66+
)
67+
np.testing.assert_array_almost_equal(
68+
cal.get_pos(), orig_cal.get_pos(), decimal=3
69+
)
70+
71+
tmp_orient_par = OrientPar()
72+
73+
_, _, _ = full_calibration(
74+
cal,
75+
ref_pts,
76+
targets,
77+
control,
78+
tmp_orient_par
79+
)
80+
81+
np.testing.assert_array_almost_equal(
82+
cal.get_angles(), orig_cal.get_angles(), decimal=3
83+
)
84+
np.testing.assert_array_almost_equal(
85+
cal.get_pos(), orig_cal.get_pos(), decimal=3
86+
)
87+
88+
print_cal(cal)
89+
90+
91+
print("with added par")
92+
tmp_orient_par = OrientPar()
93+
tmp_orient_par.k1flag = 1
94+
tmp_orient_par.k2flag = 0
95+
tmp_orient_par.k3flag = 0
96+
97+
tmp_orient_par.p1flag = 1
98+
tmp_orient_par.p2flag = 0
99+
100+
tmp_orient_par.scxflag = 1
101+
tmp_orient_par.sheflag = 0
102+
# tmp_orient_par.k3flag = 1
103+
104+
_, _, _ = full_calibration(
105+
cal,
106+
ref_pts,
107+
targets,
108+
control,
109+
tmp_orient_par
110+
)
111+
print_cal(cal)
112+
113+
# # %%
114+
# control_file_name = "tests/testing_folder/corresp/control.par"
115+
# control = read_control_par(control_file_name)
116+
117+
# orient_par_file_name = "tests/testing_folder/corresp/orient.par"
118+
# orient_par = OrientPar().from_file(orient_par_file_name)
119+
120+
# cal = Calibration().from_file(
121+
# "tests/testing_folder/calibration/cam1.tif.ori",
122+
# "tests/testing_folder/calibration/cam1.tif.addpar",
123+
# )
124+
# orig_cal = Calibration().from_file(
125+
# "tests/testing_folder/calibration/cam1.tif.ori",
126+
# "tests/testing_folder/calibration/cam1.tif.addpar")
127+
128+
# # %%
129+
# ref_pts = np.array(
130+
# [
131+
# [-40.0, -25.0, 8.0],
132+
# [40.0, -15.0, 0.0],
133+
# [40.0, 15.0, 0.0],
134+
# [40.0, 0.0, 8.0],
135+
# ]
136+
# )
137+
138+
# # Fake the image points by back-projection
139+
# targets = arr_metric_to_pixel(
140+
# image_coordinates(ref_pts, cal, control.mm),
141+
# control,
142+
# )
143+
144+
# cal.set_pos(np.array([0, 0, 100]))
145+
# cal.set_angles(np.array([0, 0, 0]))
146+
147+
# # Jigg the fake detections to give raw_orient some challenge.
148+
# targets[:, 1] -= 0.1
149+
150+
# # %%
151+
targs = [Target() for _ in targets]
152+
153+
for ptx, pt in enumerate(targets):
154+
targs[ptx].x = pt[0]
155+
targs[ptx].y = pt[1]
156+
targs[ptx].pnr = ptx
157+
158+
def added_par_residual(added_par_array, ref_pts, targs, control, cal):
159+
c = copy.deepcopy(cal)
160+
c.added_par = added_par_array
161+
162+
residual = 0
163+
for i, t in enumerate(targs):
164+
xc, yc = pixel_to_metric(t.x, t.y, control)
165+
xp, yp = img_coord(ref_pts[i], c, control.mm)
166+
residual += ((xc - xp)**2 + (yc - yp)**2)
167+
168+
return residual
169+
170+
171+
172+
np.seterr(all='raise')
173+
174+
x0 = np.array(cal.added_par.tolist())
175+
sol = opt.minimize(added_par_residual, x0, args=(ref_pts, targs, control, cal), \
176+
method='Nelder-Mead', tol=1e-6)
177+
print(f"{sol.x=}")
178+
# print(sol.x - np.hstack([orig_cal.get_pos(), orig_cal.get_angles()]))
179+
180+
181+
182+
# # # %%
183+
# # # print(sol.x)
184+
# # print(cal.added_par)
185+
cal.set_added_par(sol.x)
186+
print_cal(cal)
187+
# # print(cal.added_par)
188+
189+
full_calibration(cal, ref_pts, targets, control, tmp_orient_par)
190+
print_cal(cal)
191+
192+
# # # %%
193+
194+
195+
# print(added_par_residual(cal.added_par, ref_pts, targs, control, cal))

0 commit comments

Comments
 (0)