Extract 'average displacement' of inliers for TrakEM2 batch alignment

Dear Forum,

I am using a script based on this one to batch-align images using TrakEM2, only using the RegularizedAffineLayerAlignment.

Visually this seems to work really great but I would like to get some hard numbers how good the alignment is.

A way to get this would be to extract the ‘average displacement’ of the inliers of the matchs (i.e. the value logged here: https://github.com/trakem2/TrakEM2/blob/e2c46b0851a090310542e58ee2d7afc895b57a1d/src/main/java/mpicbg/trakem2/align/ElasticLayerAlignment.java#L1067).

Unfortunately my BeanShell/Java knowledge is a bit limited - is there a way to modify this script to extract this value from aligned project or from a ‘RegularizedAffineLayerAlignment’ instance?

Thanks already for any suggestion/help!

Hi @votti,
The log window should print lots of information on the min,max,mean alignment error distances, for every pair of sections aligned. Isn’t that the case?
The ElasticLayerAlignment source code may be useful to understand what is done where, and whether there’s any additional reporting that could be turned on.
HTH,
Albert

Additionally, you could always apply the inverse of the image transformations to a grid of points on each section, and then find out how much the points move. When non-linear, the displacement will differ for each point in the grid, but you’ll get an idea.

To extract the transformations from each image, see these examples in jython, the method you’ll want to call on a Patch instance is getCoordinateTransform. See the source code here: Patch.getCoordinateTransform().

Thanks already a lot for your input!

The log window should print lots of information on the min,max,mean alignment error distances, for every pair of sections aligned. Isn’t that the case?

Yes this is the case. As I am aligning 100s of images in a scripted fashion I would like to extract these values automatically.
I have considered parsing the log as a ‘worst case’ approach, but was wondering if there would be a cleaner way.

Additionally, you could always apply the inverse of the image transformations to a grid of points on each section, and then find out how much the points move. When non-linear, the displacement will differ for each point in the grid, but you’ll get an idea.

This would indeed work, but would give me how much the transform displaces points.
However what I want is the ‘average displacement’ of inliers (an estimate of the alignment error).

I updated the title and try to give a bit of background:
The alignment in TrackEM2 is done by identifying landmark points and then matching them by RANSAC (https://en.wikipedia.org/wiki/Random_sample_consensus). This matches points between the two layers that are likely the same (inliers). The matched points (inliers) are typically still not perfectly overlapping after transformation. What I want to know is how far they are apart on average (displacement), as this is a rough estimate for how good the alignment is (i.e. if the transformation method has been sufficiently flexible).

Anyways, I really appreciate your input and think I will settle for the log-parsing approach.
In fact I have now translated the beanshell script to be run via PyImageJ, as this fits my workflow better. I found a working approach to capture the log within pyimagej, so this is working now quite OK (https://github.com/imagej/pyimagej/issues/35#issuecomment-492783346).

Cheers and thanks!

If I remember correctly, @axtimwalde (the original author of the image registration code you are using) also used the log-parsing approach. But there are ways to extract the info, there is an observer class in there somewhere that one of the static methods accepts, and which, unless I am remembering incorrectly, could give you the data you want on inlier displacement.

Ok good to know - I spend quite some time skimming the code but did not find the observer class yet, but will give it another look.

I have implemented the log parsing approach and this works good enough for me.
Cheers!

@votti Just in case it wasn’t clear from the issue you linked: the next release of pyimagej will include the stdout/stderr forwarding from Java to Python automatically, so you won’t need to do it yourself anymore.

1 Like

Awesome!
I actually have been really impressed by pyimagej - the whole experience was already really smooth and integrated seamlessly in my python based workflow.
Really cool how all the major tools are interoperable nowadays :ok_hand:

2 Likes