|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": 5, |
| 5 | + "execution_count": 21, |
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | | - "# test calibration using scipy.optimize\n", |
10 | | - "\n", |
11 | | - "import numpy as np\n", |
12 | | - "import scipy.optimize as opt" |
| 9 | + "# test calibration using scipy.optimize\n" |
13 | 10 | ] |
14 | 11 | }, |
15 | 12 | { |
16 | 13 | "cell_type": "code", |
17 | | - "execution_count": 6, |
| 14 | + "execution_count": 22, |
18 | 15 | "metadata": {}, |
19 | 16 | "outputs": [], |
20 | 17 | "source": [ |
21 | 18 | "import unittest\n", |
22 | 19 | "\n", |
23 | 20 | "import numpy as np\n", |
| 21 | + "import scipy.optimize as opt\n", |
24 | 22 | "\n", |
25 | 23 | "from openptv_python.calibration import (\n", |
26 | 24 | " Calibration,\n", |
|
31 | 29 | ")\n", |
32 | 30 | "from openptv_python.parameters import ControlPar, OrientPar, VolumePar, read_control_par\n", |
33 | 31 | "from openptv_python.tracking_frame_buf import TargetArray\n", |
34 | | - "from openptv_python.trafo import arr_metric_to_pixel" |
| 32 | + "from openptv_python.trafo import arr_metric_to_pixel\n", |
| 33 | + "from openptv_python.imgcoord import img_coord\n", |
| 34 | + "from openptv_python.trafo import pixel_to_metric\n", |
| 35 | + "from openptv_python.tracking_frame_buf import Target\n" |
35 | 36 | ] |
36 | 37 | }, |
37 | 38 | { |
38 | 39 | "cell_type": "code", |
39 | | - "execution_count": 7, |
| 40 | + "execution_count": 23, |
40 | 41 | "metadata": {}, |
41 | 42 | "outputs": [], |
42 | 43 | "source": [ |
|
92 | 93 | }, |
93 | 94 | { |
94 | 95 | "cell_type": "code", |
95 | | - "execution_count": 8, |
| 96 | + "execution_count": 24, |
96 | 97 | "metadata": {}, |
97 | 98 | "outputs": [ |
98 | 99 | { |
|
103 | 104 | "External calibration using clicked points. ... ok\n", |
104 | 105 | "\n", |
105 | 106 | "----------------------------------------------------------------------\n", |
106 | | - "Ran 1 test in 0.120s\n", |
| 107 | + "Ran 1 test in 0.014s\n", |
107 | 108 | "\n", |
108 | 109 | "OK\n" |
109 | 110 | ] |
|
122 | 123 | { |
123 | 124 | "data": { |
124 | 125 | "text/plain": [ |
125 | | - "<unittest.main.TestProgram at 0x7f10eb2886a0>" |
| 126 | + "<unittest.main.TestProgram at 0x7f946fcd6e90>" |
126 | 127 | ] |
127 | 128 | }, |
128 | | - "execution_count": 8, |
| 129 | + "execution_count": 24, |
129 | 130 | "metadata": {}, |
130 | 131 | "output_type": "execute_result" |
131 | 132 | } |
132 | 133 | ], |
133 | 134 | "source": [ |
134 | 135 | "unittest.main(argv=[''], verbosity=2, exit=False)" |
135 | 136 | ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": 25, |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [ |
| 144 | + "control_file_name = \"testing_folder/corresp/control.par\"\n", |
| 145 | + "control = read_control_par(control_file_name)\n", |
| 146 | + "\n", |
| 147 | + "orient_par_file_name = \"testing_folder/corresp/orient.par\"\n", |
| 148 | + "orient_par = OrientPar().from_file(orient_par_file_name)\n", |
| 149 | + "\n", |
| 150 | + "cal = Calibration().from_file(\n", |
| 151 | + " \"testing_folder/calibration/cam1.tif.ori\",\n", |
| 152 | + " \"testing_folder/calibration/cam1.tif.addpar\",\n", |
| 153 | + ")\n", |
| 154 | + "orig_cal = Calibration().from_file(\n", |
| 155 | + " \"testing_folder/calibration/cam1.tif.ori\",\n", |
| 156 | + " \"testing_folder/calibration/cam1.tif.addpar\")" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 26, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "ref_pts = np.array(\n", |
| 166 | + " [\n", |
| 167 | + " [-40.0, -25.0, 8.0],\n", |
| 168 | + " [40.0, -15.0, 0.0],\n", |
| 169 | + " [40.0, 15.0, 0.0],\n", |
| 170 | + " [40.0, 0.0, 8.0],\n", |
| 171 | + " ]\n", |
| 172 | + ")\n", |
| 173 | + "\n", |
| 174 | + "# Fake the image points by back-projection\n", |
| 175 | + "targets = arr_metric_to_pixel(\n", |
| 176 | + " image_coordinates(ref_pts, cal, control.mm),\n", |
| 177 | + " control,\n", |
| 178 | + ")\n", |
| 179 | + "\n", |
| 180 | + "cal.set_pos(np.array([0, 0, 100]))\n", |
| 181 | + "cal.set_angles(np.array([0, 0, 0]))\n", |
| 182 | + "\n", |
| 183 | + "# Jigg the fake detections to give raw_orient some challenge.\n", |
| 184 | + "targets[:, 1] -= 0.1" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": 27, |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [], |
| 192 | + "source": [ |
| 193 | + "targs = [Target() for _ in targets]\n", |
| 194 | + "\n", |
| 195 | + "for ptx, pt in enumerate(targets):\n", |
| 196 | + " targs[ptx].x = pt[0]\n", |
| 197 | + " targs[ptx].y = pt[1]" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "code", |
| 202 | + "execution_count": 40, |
| 203 | + "metadata": {}, |
| 204 | + "outputs": [], |
| 205 | + "source": [ |
| 206 | + "\n", |
| 207 | + "\n", |
| 208 | + "def residual(calibration_array, ref_pts, targs, control, cc):\n", |
| 209 | + " # print(calibration_array)\n", |
| 210 | + " # print(ref_pts)\n", |
| 211 | + " # print(targs)\n", |
| 212 | + " # print(control)\n", |
| 213 | + " # print(calibration_array)\n", |
| 214 | + " \n", |
| 215 | + " c = Calibration()\n", |
| 216 | + " c.set_pos(calibration_array[:3])\n", |
| 217 | + " c.set_angles(calibration_array[3:])\n", |
| 218 | + " c.int_par.cc = cc\n", |
| 219 | + " c.update_rotation_matrix()\n", |
| 220 | + " \n", |
| 221 | + " \n", |
| 222 | + " # print(f\"{c.get_pos()=}\")\n", |
| 223 | + " \n", |
| 224 | + " residual = 0\n", |
| 225 | + " for i in range(len(targs)):\n", |
| 226 | + " xc, yc = pixel_to_metric(targs[i].x, targs[i].y, control)\n", |
| 227 | + " # print(f\"{xc=}, {yc=} mm\")\n", |
| 228 | + " \n", |
| 229 | + " xp, yp = img_coord(ref_pts[i], c, control.mm)\n", |
| 230 | + " # print(f\"{xp=}, {yp=} mm\")\n", |
| 231 | + " residual += ((xc - xp)**2 + (yc - yp)**2)\n", |
| 232 | + " \n", |
| 233 | + " # print(f\"{residual=}\")\n", |
| 234 | + " \n", |
| 235 | + " return residual" |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "code", |
| 240 | + "execution_count": 41, |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [ |
| 244 | + "x0 = np.hstack([cal.get_pos(), cal.get_angles()])\n", |
| 245 | + "cc = orig_cal.int_par.cc\n", |
| 246 | + "\n", |
| 247 | + "sol = opt.minimize(residual, x0, args=(ref_pts, targs, control, cc), method='Nelder-Mead', tol=1e-6)" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": 43, |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "name": "stdout", |
| 257 | + "output_type": "stream", |
| 258 | + "text": [ |
| 259 | + "sol.x=array([-3.79876331e+00, 3.77309709e+01, 4.03575316e+02, -8.61982784e-02,\n", |
| 260 | + " -6.33837589e-02, 5.14334685e-02])\n", |
| 261 | + "[-1.09061963e+02 -6.50148291e+01 -3.06884099e-01 1.52130822e-01\n", |
| 262 | + " -3.07664759e-01 -3.82423146e-03]\n" |
| 263 | + ] |
| 264 | + } |
| 265 | + ], |
| 266 | + "source": [ |
| 267 | + "print(f\"{sol.x=}\")\n", |
| 268 | + "print(sol.x - np.hstack([orig_cal.get_pos(), orig_cal.get_angles()]))" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": 39, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [ |
| 276 | + { |
| 277 | + "name": "stdout", |
| 278 | + "output_type": "stream", |
| 279 | + "text": [ |
| 280 | + "93.58853214427855\n" |
| 281 | + ] |
| 282 | + } |
| 283 | + ], |
| 284 | + "source": [ |
| 285 | + "print( residual(np.hstack([orig_cal.get_pos(), orig_cal.get_angles()]), ref_pts, targs, control, orig_cal.int_par.cc))" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "code", |
| 290 | + "execution_count": null, |
| 291 | + "metadata": {}, |
| 292 | + "outputs": [], |
| 293 | + "source": [] |
136 | 294 | } |
137 | 295 | ], |
138 | 296 | "metadata": { |
|
0 commit comments