Skip to content

Commit

Permalink
added notebook to test calibration using Ron code
Browse files Browse the repository at this point in the history
improved scipy_optimize to work also with 3D body and not only multiplane
  • Loading branch information
alexlib committed Mar 28, 2024
1 parent 2d8f3e3 commit f428bc2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pyptv/calibration_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def _button_showimg_fired(self):
self.cal_images = []
for i in range(len(self.camera)):
imname = self.calParams.img_cal_name[i]
im = imread(imname)
im = imread(imname, as_gray=True)
# im = ImageData.fromfile(imname).data
if im.ndim > 2:
im = rgb2gray(im[:,:,:3])
Expand Down Expand Up @@ -980,10 +980,10 @@ def _button_fine_orient_fired(self):
print("Before scipy calibration")
print(self.cals[i_cam].get_pos())

ptv.py_calibration(
residuals = ptv.py_calibration(
self.cals[i_cam],
all_known,
all_detected[:, 1:],
self.cal_points["pos"],
targs,
self.cpar,
flags,
)
Expand Down Expand Up @@ -1016,8 +1016,8 @@ def _button_fine_orient_fired(self):
self.camera[i_cam].drawquiver(
x,
y,
x + scale * residuals[: len(x), 0],
y + scale * residuals[: len(x), 1],
x + scale * residuals[:len(x), 0],
y + scale * residuals[:len(x), 1],
"red",
)
# self.camera[i]._plot.index_mapper.range.set_bounds(0, self.h_pixel)
Expand Down
11 changes: 9 additions & 2 deletions pyptv/ptv.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,16 @@ def py_calibration(cal, XYZ, xy, cpar, flags):
""" Performs calibration of the selected cameras using Scipy"""

x0 = calibration_to_array(cal)
sol = minimize(error_function, x0, args=(cal, XYZ, xy, cpar), method='Nelder-Mead', tol=1e-5)
xy = np.array([_.pos() for _ in xy]) # we get targs not arrays
sol = minimize(error_function, x0, args=(cal, XYZ, xy, cpar), method='Nelder-Mead', tol=1e-6)
array_to_calibration(sol.x, cal) # cal is updated in place
# return cal

targets = arr_metric_to_pixel(
image_coordinates(XYZ, cal, cpar.get_multimedia_params()),
cpar,
)

return targets - xy


def py_multiplanecalibration(exp):
Expand Down
6 changes: 4 additions & 2 deletions pyptv/pyptv_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ def init_action(self, info):
getattr(
mainGui.exp1.active_params.m_params,
f"Name_{i+1}_Image",
)
),
as_gray=True,
)
if im.ndim > 2:
im = rgb2gray(im)
Expand Down Expand Up @@ -1517,7 +1518,8 @@ def main():
# exp_path = software_path.parent / "test_cavity"
# exp_path = Path('/home/user/Downloads/one-dot-example/working_folder')
# exp_path = Path('/home/user/Downloads/test_crossing_particle')
exp_path = Path('/home/user/Downloads/rbc300')
# exp_path = Path('/home/user/Downloads/rbc300')
exp_path = Path('/home/user/Downloads/For_Alex_test_34/')
print(f"Without input, PyPTV fallbacks to a default {exp_path} \n")

if not exp_path.is_dir() or not exp_path.exists():
Expand Down
83 changes: 83 additions & 0 deletions pyptv/test_calibration.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# main\n",
"from test_calibration import *\n",
"%matplotlib qt\n",
"\n",
"from pathlib import Path\n",
"working_path = Path('/home/user/Downloads/For_Alex_test_34')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(<Figure size 640x480 with 1 Axes>,\n",
" <Axes3D: xlabel='X', ylabel='Z', zlabel='Y'>)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"calblock = read_calblock(working_path / 'cal' / 'target_on_a_side.txt')\n",
"result = read_dt_lsq(working_path / 'res' / 'rt_is.123456789')\n",
"pairs = pair_cal_points(calblock, result, max_dist = 3.0)\n",
"plot_cal_points(pairs)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(<Figure size 640x480 with 1 Axes>, <Axes: >)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot_cal_err_histogram(pairs)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pyptv25",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit f428bc2

Please sign in to comment.