|
| 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