Skip to content

Commit a328447

Browse files
committed
Update test_calibration_optimize.ipynb
1 parent 04bfaa8 commit a328447

1 file changed

Lines changed: 170 additions & 12 deletions

File tree

tests/test_calibration_optimize.ipynb

Lines changed: 170 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 5,
5+
"execution_count": 21,
66
"metadata": {},
77
"outputs": [],
88
"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"
1310
]
1411
},
1512
{
1613
"cell_type": "code",
17-
"execution_count": 6,
14+
"execution_count": 22,
1815
"metadata": {},
1916
"outputs": [],
2017
"source": [
2118
"import unittest\n",
2219
"\n",
2320
"import numpy as np\n",
21+
"import scipy.optimize as opt\n",
2422
"\n",
2523
"from openptv_python.calibration import (\n",
2624
" Calibration,\n",
@@ -31,12 +29,15 @@
3129
")\n",
3230
"from openptv_python.parameters import ControlPar, OrientPar, VolumePar, read_control_par\n",
3331
"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"
3536
]
3637
},
3738
{
3839
"cell_type": "code",
39-
"execution_count": 7,
40+
"execution_count": 23,
4041
"metadata": {},
4142
"outputs": [],
4243
"source": [
@@ -92,7 +93,7 @@
9293
},
9394
{
9495
"cell_type": "code",
95-
"execution_count": 8,
96+
"execution_count": 24,
9697
"metadata": {},
9798
"outputs": [
9899
{
@@ -103,7 +104,7 @@
103104
"External calibration using clicked points. ... ok\n",
104105
"\n",
105106
"----------------------------------------------------------------------\n",
106-
"Ran 1 test in 0.120s\n",
107+
"Ran 1 test in 0.014s\n",
107108
"\n",
108109
"OK\n"
109110
]
@@ -122,17 +123,174 @@
122123
{
123124
"data": {
124125
"text/plain": [
125-
"<unittest.main.TestProgram at 0x7f10eb2886a0>"
126+
"<unittest.main.TestProgram at 0x7f946fcd6e90>"
126127
]
127128
},
128-
"execution_count": 8,
129+
"execution_count": 24,
129130
"metadata": {},
130131
"output_type": "execute_result"
131132
}
132133
],
133134
"source": [
134135
"unittest.main(argv=[''], verbosity=2, exit=False)"
135136
]
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": []
136294
}
137295
],
138296
"metadata": {

0 commit comments

Comments
 (0)