Commit 58e2489c authored by Lars Bittrich's avatar Lars Bittrich
Browse files

Zeiss Importer with permutation variation on the marker positions to allow...

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
parent 5fd1cb0c
Loading
Loading
Loading
Loading
+27 −12
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ from helperfunctions import cv2imread_fix, cv2imwrite_fix
from ramancom.ramancontrol import defaultPath
from ramancom.ramancontrol import defaultPath
from dataset import DataSet
from dataset import DataSet
from scipy.optimize import least_squares
from scipy.optimize import least_squares
from itertools import permutations
import cv2
import cv2
import numpy as np
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*c3+c1*c2*s3, -s1*s3+c1*c2*c3, -c1*s2],
                           [s1*s3, s2*c3, c2]])
                           [s1*s3, s2*c3, c2]])
        
        
        # 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):
            def err(angles_shift):
                T = getRotMat(angles_shift[:3]).T.A
                T = getRotMat(angles_shift[:3]).T.A
            return (np.dot(zpoints, T)-angles_shift[np.newaxis,3:]-points).ravel()
                return (np.dot(zpoints, T) - angles_shift[np.newaxis,3:] \

                        - ppoints).ravel()
        best = None

        for i in range(100):
            angle = np.zeros(3)
            angle = np.pi*np.random.rand(3)-np.pi/2.
            opt = least_squares(err, np.concatenate((angle, np.zeros(3))), 
            opt = least_squares(err, np.concatenate((angle, np.zeros(3))), method='lm')
                                bounds=(np.array([-np.pi/4]*3+[-np.inf]*3),
            if best is None or best.cost>opt.cost:
                                        np.array([np.pi/4]*3+[np.inf]*3)),
                best = opt
                                method='dogbox')
        optangles = best.x[:3]
            
        shift = best.x[3:]
            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
        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 angles:", optangles, flush=True)
        print("Transformation shift:", shift, flush=True)
        print("Transformation shift:", shift, flush=True)
        print("Transformation err:", e, flush=True)
        print("Transformation err:", e, flush=True)