From 58e2489c408cfd95a85697e9f265515958391419 Mon Sep 17 00:00:00 2001 From: Lars Bittrich Date: Sun, 28 Apr 2019 22:22:34 +0200 Subject: [PATCH] Zeiss Importer with permutation variation on the marker positions to allow changing marker sequence between Zeiss measurement and at Raman microscope -> minimize human error; but at the cost of limiting maximum angle rotations to -45,45 degrees in each euler angle --- zeissimporter.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/zeissimporter.py b/zeissimporter.py index 20d4af4..761e697 100644 --- a/zeissimporter.py +++ b/zeissimporter.py @@ -26,6 +26,7 @@ from helperfunctions import cv2imread_fix, cv2imwrite_fix from ramancom.ramancontrol import defaultPath from dataset import DataSet from scipy.optimize import least_squares +from itertools import permutations import cv2 import numpy as np @@ -228,20 +229,34 @@ class ZeissImporter(QtWidgets.QDialog): [s1*c3+c1*c2*s3, -s1*s3+c1*c2*c3, -c1*s2], [s1*s3, s2*c3, c2]]) - def err(angles_shift): - T = getRotMat(angles_shift[:3]).T.A - return (np.dot(zpoints, T)-angles_shift[np.newaxis,3:]-points).ravel() + # find the transformation matrix with best fit for small angles in + # [-45°,45°] for all permutation of markers + permbest = None + pointsbest = None + for perm in permutations(range(points.shape[0])): + ppoints = points[perm,:] + + def err(angles_shift): + T = getRotMat(angles_shift[:3]).T.A + return (np.dot(zpoints, T) - angles_shift[np.newaxis,3:] \ + - ppoints).ravel() - best = None - for i in range(100): - angle = np.pi*np.random.rand(3)-np.pi/2. - opt = least_squares(err, np.concatenate((angle, np.zeros(3))), method='lm') - if best is None or best.cost>opt.cost: - best = opt - optangles = best.x[:3] - shift = best.x[3:] + angle = np.zeros(3) + opt = least_squares(err, np.concatenate((angle, np.zeros(3))), + bounds=(np.array([-np.pi/4]*3+[-np.inf]*3), + np.array([np.pi/4]*3+[np.inf]*3)), + method='dogbox') + + if permbest is None or \ + permbest.cost>opt.cost: + print("Current best permutation:", perm, flush=True) + permbest = opt + pointsbest = ppoints + + optangles = permbest.x[:3] + shift = permbest.x[3:] T = getRotMat(optangles).T.A - e = (np.dot(zpoints, T)-shift[np.newaxis,:]-points) + e = (np.dot(zpoints, T)-shift[np.newaxis,:]-pointsbest) print("Transformation angles:", optangles, flush=True) print("Transformation shift:", shift, flush=True) print("Transformation err:", e, flush=True) -- 2.24.1