Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions cmd/transformcalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Eigen/Geometry>
#include <unsupported/Eigen/MatrixFunctions>
#include <algorithm>
#include "axes.h"
#include "command.h"
#include "math/math.h"
#include "math/average_space.h"
Expand Down Expand Up @@ -67,8 +68,20 @@ void usage ()
"")

+ Example ("Calculate the transformation matrix from an original image and an image with modified header",
"transformcalc mov mapmovhdr header output",
"")
"transformcalc original.mif modified.mif header output.txt",
"This transformation can be used for instance: mrtransform original_too.mif -linear output modified_too.mif. "
"The interpretation of the difference between two image header transforms, "
"and therefore the requisite transform to move between the two, "
"can be influenced by whether MRtrix is configured to automatically realign images "
"to approximately orient the three spatial axes sequentially along the Right-Anterior-Superior directions. "
"This behaviour is activate by default. "
"This however means that the result of this particular calculation "
"may not correspond with the header transforms as they are stored on the file system. "
"If the transform being sought is specifically the difference between two header transforms "
"as they are stored on the filesystem, "
"without the confound of MRtrix3 interpretation of such data, "
"then the transformcalc command can be run with option \"-config RealignTransform false\""
"to override the software default / any system or user configuration.")

+ Example ("Calculate the average affine matrix of a set of input matrices",
"transformcalc input1.txt ... inputN.txt average matrix_out.txt",
Expand Down Expand Up @@ -202,11 +215,23 @@ void run ()
case 3: { // header
if (num_inputs != 2)
throw Exception ("header requires 2 inputs");
auto orig_header = Header::open (argument[0]);
auto modified_header = Header::open (argument[1]);

transform_type forward_transform = Transform(modified_header).voxel2scanner * Transform(orig_header).voxel2scanner.inverse();
save_transform (forward_transform.inverse(), output_path);
Comment on lines -208 to -209
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this composed the transform from modified header to original header and then inverted it, as opposed to just computing the transform from original heder to modified header...

auto first_header = Header::open (argument[0]);
auto second_header = Header::open (argument[1]);
const transform_type transform = Transform(first_header).voxel2scanner * Transform(second_header).scanner2voxel;
if (Header::do_realign_transform) {
if (!first_header.realignment().is_identity() || !second_header.realignment().is_identity()) {
WARN("Computed transform between headers influenced by MRtrix3 internal image realignment;"
" if result is incorrect consider re-trying with \"-config RealignTransform false\"");
}
} else {
const Axes::Shuffle first_shuffle = Axes::get_shuffle_to_make_RAS(first_header.transform());
const Axes::Shuffle second_shuffle = Axes::get_shuffle_to_make_RAS(second_header.transform());
if (!first_shuffle.is_identity() || !second_shuffle.is_identity()) {
WARN("Computed transform between headers omits default MRtrix3 internal image realignment;"
" if result is incorrect consider re-trying with \"-config RealignTransform true\"");
}
}
save_transform (transform, output_path);
break;
}
case 4: { // average
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/commands/transformcalc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Example usages

- *Calculate the transformation matrix from an original image and an image with modified header*::

$ transformcalc mov mapmovhdr header output
$ transformcalc original.mif modified.mif header output.txt

This transformation can be used for instance: mrtransform original_too.mif -linear output modified_too.mif. The interpretation of the difference between two image header transforms, and therefore the requisite transform to move between the two, can be influenced by whether MRtrix is configured to automatically realign images to approximately orient the three spatial axes sequentially along the Right-Anterior-Superior directions. This behaviour is activate by default. This however means that the result of this particular calculation may not correspond with the header transforms as they are stored on the file system. If the transform being sought is specifically the difference between two header transforms as they are stored on the filesystem, without the confound of MRtrix3 interpretation of such data, then the transformcalc command can be run with option "-config RealignTransform false"to override the software default / any system or user configuration.

- *Calculate the average affine matrix of a set of input matrices*::

Expand Down
2 changes: 2 additions & 0 deletions testing/binaries/tests/transformcalc
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ transformcalc transformcalc/out.txt moving2templateFSL.txt average tmp2.txt -for
transformcalc moving2templateFSL.txt moving2template.txt 1 interpolate tmp.txt -force && testing_diff_matrix tmp.txt moving2template.txt -abs 1e-3
transformcalc moving2templateFSL.txt moving2template.txt 0 interpolate tmp.txt -force && testing_diff_matrix tmp.txt moving2templateFSL.txt -abs 1e-3
transformcalc template.mif.gz dwi_mean.mif header tmp.txt -force && testing_diff_matrix tmp.txt transformcalc/out3.txt -abs 1e-3
rm -f tmp1.txt && echo $'0 1 0 0\n1 0 0 0\n0 0 1 0\n0 0 0 1' > tmp1.txt && mrtransform dwi_mean.mif -linear tmp1.txt tmp.mif -force && transformcalc dwi_mean.mif tmp.mif header tmp2.txt -force -config RealignTransform true 2>&1 | grep "Computed transform between headers influenced by MRtrix3 internal image realignment"
rm -f tmp1.txt && echo $'0 1 0 0\n1 0 0 0\n0 0 1 0\n0 0 0 1' > tmp1.txt && mrtransform dwi_mean.mif -linear tmp1.txt tmp.mif -force && transformcalc dwi_mean.mif tmp.mif header tmp2.txt -force -config RealignTransform false 2>&1 | grep "Computed transform between headers omits default MRtrix3 internal image realignment" && testing_diff_matrix tmp1.txt tmp2.txt -abs 1e-13
Loading