users > Applying transformation matrix from dof2mat to a traced neuron (swc) file
Showing 1-6 of 6 posts
Jun 17, 2019 07:06 PM | Zane Aldworth
Applying transformation matrix from dof2mat to a traced neuron (swc) file
Hi,
I'm having problems applying the transform matrix from dof2mat to traced neurons.
A little background for my problem: we're trying to trace neurons from many different insect brains in different alignments. Since brain registration usually involves a decrease in image resolution, we'd like to trace the neurons in the original images, perform the registration to a template brain, and then apply the transformation matrix from said registration to the traced neuron.
The dof2mat seems to be the correct tool, however when I apply the matrix to my swc file (in Matlab), the traced neuron is significantly off from transformed image. The misalignment is beyond just a simple translation, and appears to consist of both rotation and shear errors.
Are there any known issues that would cause the error with this workflow? Should I instead be trying to sequentially apply the translation/rotation/scale/shear matrices from the xform file? If so, what are the units on these sub matrices (radians on the rotation matrix, microns on the translation, etc?)
Thanks!
I'm having problems applying the transform matrix from dof2mat to traced neurons.
A little background for my problem: we're trying to trace neurons from many different insect brains in different alignments. Since brain registration usually involves a decrease in image resolution, we'd like to trace the neurons in the original images, perform the registration to a template brain, and then apply the transformation matrix from said registration to the traced neuron.
The dof2mat seems to be the correct tool, however when I apply the matrix to my swc file (in Matlab), the traced neuron is significantly off from transformed image. The misalignment is beyond just a simple translation, and appears to consist of both rotation and shear errors.
Are there any known issues that would cause the error with this workflow? Should I instead be trying to sequentially apply the translation/rotation/scale/shear matrices from the xform file? If so, what are the units on these sub matrices (radians on the rotation matrix, microns on the translation, etc?)
Thanks!
Jun 18, 2019 10:06 AM | Greg Jefferis
RE: Applying transformation matrix from dof2mat to a traced neuron (swc) file
Dear Zane,
Originally posted by Zane Aldworth:
This is generally the best thing to do, but can be a little bit more fiddly.
I take it you are only trying to apply affine transformations?
It is most likely that you have not interpreted the matrix correctly (or at least in the same way as CMTK!). The two most common problems are:
1. a difference in the convention for row-major and column-major representation of the matrix. Essentially you may need to transpose.
2. a difference in your expectations about the transformation direction. You may actually want the inverse of what you have tried to apply.
I have regularly encountered these issues when working with different transform tools / representations. I actually use an R-based toolkit (https://jefferis.github.io/nat/) to look after the details. The minimal approach would look like this:
If you need to apply different transforms for each neuron then a little more effort is required to match neurons to transform. If you are interested then we would be happy to support your use of this toolkit over at https://groups.google.com/forum/#!forum/nat-user.
No.
No. Best to use a single matrix multiplication of the homogenous affine transformation matrix.
Best wishes,
Greg Jefferis.
Originally posted by Zane Aldworth:
Since brain registration usually involves a
decrease in image resolution, we'd like to trace the neurons in the
original images, perform the registration to a template brain, and
then apply the transformation matrix from said registration to the
traced neuron.
This is generally the best thing to do, but can be a little bit more fiddly.
The dof2mat seems to be the correct tool,
I take it you are only trying to apply affine transformations?
however when I apply the matrix to my swc file
(in Matlab), the traced neuron is significantly off from
transformed image. The misalignment is beyond just a simple
translation, and appears to consist of both rotation and shear
errors.
It is most likely that you have not interpreted the matrix correctly (or at least in the same way as CMTK!). The two most common problems are:
1. a difference in the convention for row-major and column-major representation of the matrix. Essentially you may need to transpose.
2. a difference in your expectations about the transformation direction. You may actually want the inverse of what you have tried to apply.
I have regularly encountered these issues when working with different transform tools / representations. I actually use an R-based toolkit (https://jefferis.github.io/nat/) to look after the details. The minimal approach would look like this:
library(nat)
nl=read.neurons('/path/to/inputneurons/')
nlt=xform(nl, 'path/to/cmtk/transform')
write.neurons(nlt, 'path/to/outputneurons/', format='swc')
nl=read.neurons('/path/to/inputneurons/')
nlt=xform(nl, 'path/to/cmtk/transform')
write.neurons(nlt, 'path/to/outputneurons/', format='swc')
If you need to apply different transforms for each neuron then a little more effort is required to match neurons to transform. If you are interested then we would be happy to support your use of this toolkit over at https://groups.google.com/forum/#!forum/nat-user.
Are there any known issues that would cause the
error with this workflow?
No.
Should I instead be trying to sequentially apply
the translation/rotation/scale/shear matrices from the xform
file?
No. Best to use a single matrix multiplication of the homogenous affine transformation matrix.
Best wishes,
Greg Jefferis.
Jun 18, 2019 04:06 PM | Torsten Rohlfing
RE: Applying transformation matrix from dof2mat to a traced neuron (swc) file
Thanks Greg - I think you pretty much covered the possible sources
of errors :)
Just to add a bit of theoretical background why often one would want the inverse transformation:
Say you align image A to image B via transform X such that you can reslice B to match A.
Then if your neuron model is in the coordinates of image B, in order to get it transformed into the space of A, you need to apply the inverse of X.
That is because for reslicing, one takes the coordinate of each pixel in A, maps it to B via X, then pulls back the interpolated image intensity from B. So intensities move the opposite direction between coordinate systems as do geometries (e.g., pixel or model coordinates).
Best,
Torsten
Just to add a bit of theoretical background why often one would want the inverse transformation:
Say you align image A to image B via transform X such that you can reslice B to match A.
Then if your neuron model is in the coordinates of image B, in order to get it transformed into the space of A, you need to apply the inverse of X.
That is because for reslicing, one takes the coordinate of each pixel in A, maps it to B via X, then pulls back the interpolated image intensity from B. So intensities move the opposite direction between coordinate systems as do geometries (e.g., pixel or model coordinates).
Best,
Torsten
Jun 18, 2019 05:06 PM | Zane Aldworth
RE: Applying transformation matrix from dof2mat to a traced neuron (swc) file
Originally posted by Greg Jefferis:
Dear
Zane,
1. a difference in the convention for row-major
and column-major representation of the matrix. Essentially you may
need to transpose.
2. a difference in your expectations about the transformation direction. You may actually want the inverse of what you have tried to apply.
2. a difference in your expectations about the transformation direction. You may actually want the inverse of what you have tried to apply.
Thanks, Greg. I had previously been trying
to transpose the matrix, but it ended up being the latter case of
needing the inverse of the transformation matrix.
And I'm specifically happy to get advice from
you, as now that I have a set of registered, traced neurons, I
would like to compare them to each other using NBLAST.
I imagine this is a conversation we could/should
have elsewhere, but I'd love some advice on how to run NBLAST on
pairs of my own (non-fly) neurons. Is there a best manner to
contact you for such a query?
Cheers,
Zane
Jun 18, 2019 05:06 PM | Zane Aldworth
RE: Applying transformation matrix from dof2mat to a traced neuron (swc) file
Thanks, Torsten.
Jun 18, 2019 06:06 PM | Greg Jefferis
RE: Applying transformation matrix from dof2mat to a traced neuron (swc) file
Originally posted by Zane Aldworth:
Glad that was quickly sorted out.
Queries welcome
at https://groups.google.com/forum/#!forum/nat-user
Best, Greg.
Originally posted by Greg Jefferis:
Thanks, Greg. I had previously been trying
to transpose the matrix, but it ended up being the latter case of
needing the inverse of the transformation matrix.
Glad that was quickly sorted out.
And I'm specifically happy to get advice from
you, as now that I have a set of registered, traced neurons, I
would like to compare them to each other using NBLAST.
I imagine this is a conversation we could/should
have elsewhere, but I'd love some advice on how to run NBLAST on
pairs of my own (non-fly) neurons. Is there a best manner to
contact you for such a query?
Best, Greg.