@@ -42,10 +42,8 @@ def affine_fit(template_points: np.ndarray, target_points: np.ndarray):
4242 end_index = (j + 1 ) * D
4343 A [D * i + j , start_index :end_index ] = T [i ]
4444 A [D * i + j , D * D + j ] = 1
45- B = target_points .T . flatten ()
45+ B = target_points .flatten ()
4646
47- print (A .shape , B .shape )
48- print (A , B )
4947 # The estimated affine transform params will be flattened
5048 # and there will be D * (D + 1) of them
5149 # Format is x1, x2, ..., b1, b2, ...
@@ -62,9 +60,19 @@ def affine_fit(template_points: np.ndarray, target_points: np.ndarray):
6260 # Round to close decimal
6361 affine = np .round (affine , decimals = 2 )
6462 print (affine )
63+ print (transform_points (affine , template_points ))
6564 return affine
6665
6766
67+ def transform_points (affine : np .ndarray , points : np .ndarray ):
68+ # Apply the current affine transform to the points
69+ transformed = np .zeros_like (points )
70+ padded = np .pad (points , ((0 , 0 ), (0 , 1 )), constant_values = 1 )
71+ for i in range (len (points )):
72+ transformed [i ] = affine @ padded [i ]
73+ return transformed
74+
75+
6876def create_demo_data (size : int | tuple = 60 , radius : float = 20 ):
6977 import numpy as np
7078
@@ -331,7 +339,7 @@ def estimate_affine(self, s: neuroglancer.ViewerState):
331339 template_points , source_points = self .split_points_into_pairs (annotations )
332340
333341 # Estimate transform
334- self .affine = affine_fit (template_points , source_points )
342+ self .affine = affine_fit (source_points , template_points )
335343
336344 # Set the transformation on the layer that is being registered
337345 # Something seems to go wrong with the state updates once this happens
0 commit comments