From f428bc2671d928c5547c2b0d03e123e87294e039 Mon Sep 17 00:00:00 2001 From: Alex Liberzon Date: Thu, 28 Mar 2024 23:14:36 +0200 Subject: [PATCH] added notebook to test calibration using Ron code improved scipy_optimize to work also with 3D body and not only multiplane --- pyptv/calibration_gui.py | 12 +++--- pyptv/ptv.py | 11 ++++- pyptv/pyptv_gui.py | 6 ++- pyptv/test_calibration.ipynb | 83 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 pyptv/test_calibration.ipynb diff --git a/pyptv/calibration_gui.py b/pyptv/calibration_gui.py index d757bce..4882734 100644 --- a/pyptv/calibration_gui.py +++ b/pyptv/calibration_gui.py @@ -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]) @@ -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, ) @@ -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) diff --git a/pyptv/ptv.py b/pyptv/ptv.py index c499d15..2465bae 100644 --- a/pyptv/ptv.py +++ b/pyptv/ptv.py @@ -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): diff --git a/pyptv/pyptv_gui.py b/pyptv/pyptv_gui.py index 2acf058..98efe93 100644 --- a/pyptv/pyptv_gui.py +++ b/pyptv/pyptv_gui.py @@ -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) @@ -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(): diff --git a/pyptv/test_calibration.ipynb b/pyptv/test_calibration.ipynb new file mode 100644 index 0000000..85ffa42 --- /dev/null +++ b/pyptv/test_calibration.ipynb @@ -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": [ + "(
,\n", + " )" + ] + }, + "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": [ + "(
, )" + ] + }, + "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 +}