Hi,
I don't know why you are using 0 for the last entry in (x,y,z,0) - it
should be (x,y,z,1) and
it is this in img2imgcoord and all other calls in FSL.
Apart from that, what you have written makes sense and describes what
is done in
applyxfm4D or any other FSL tool like FLIRT. However, because you are
trying
to use this in matlab, with a volume that is cropped and another
function (tformarray)
then there are several things which may be going wrong. Firstly, be
aware that all of
the FSL coordinate conventions are like nifti - they start at 0 -
whereas in matlab
will start at 1. Also, be careful that you are giving it the matrix
in the right direction
(FLIRT stores a mapping *from* input *to* reference space, but your
matlab function
may want the opposite). In addition, be careful whether you need to
give it a mapping
between voxel coordinates or between mm coordinates.
The qform is not used at all by FSL programs for spatial resampling
*except* to check
for radiological/neurological storage (former has negative
determinant, the latter has
positive determinant).
It isn't easy to convert between conventions for spatial coordinates
and coordinate
transformations - I know because I've had to do this in the past and
it is painful.
Can you get the correct answers for the actual coordinate locations
(not resampling
of images) between the matlab and fsl representations? For example,
using img2imgcoord
and matrix maths in matlab? If so, then it is probably just a
question of working out
what tformarray requires - and I'm not familiar with this so I cannot
help.
Good luck.
All the best,
Mark
On 29 Jul 2009, at 04:24, Keith Schneider wrote:
> I've been playing around with this, but I'm still not getting it to
> work correctly. I'm simply trying to apply a transformation to
> volume in Matlab and get the same answer that flirt does... It's
> complicated by the fact that I am using cropped volumes, so the
> qoffsets are not 0. I want to reslice in the same voxel dimensions,
> so let's call S the scaling matrix, with the voxel dimensions along
> the diagonal, and A the affine transformation output by flirt.
> Also, both the source and target are in the same radiological
> convention, so we can ignore this.
>
> The program img2imgcoord computes inv(S) * A * S * X, where X is the
> vector (x,y,z,0). Note the 0 on the end there, it computes the
> coordinates ignoring the translations, is that right? I can
> replicate this behavior in Matlab.
>
> However, if I use inv(S) * A * S as the affine transformation in
> Matlab to reslice a volume (say, using tformarray), I'm not getting
> the right answer. Can someone tell me the exact sequence of matrix
> algebra that is used by, say, applyxfm4d? Does it at all involve
> the qform of the image?
>
> Thanks!
>
> keith
>
> On Jul 27, 2009, at 6:53 AM, Mark Jenkinson wrote:
>
>> Hi,
>>
>> Yes, Ged is right - the neurological / radiological change
>> is what makes this difficult. The document that Steve sent
>> is rather out of date now.
>>
>> The simple description of what happens is this:
>> - any "neurological" image has a "swap" applied to the
>> x-direction
>> - the matrix is applied to "scaled voxel" coordinates.
>> That is, multiply the voxel coord by the (absolute)
>> value of the voxel dimension (in mm)
>> - voxel coords are in the nifti convention (starting at 0)
>> - the "swap" in the x-direction is applied to the voxel
>> coordinate
>>
>> So a matrix equation which would map voxel to voxel
>> coordinates (nifti convention) from input to reference
>> image would be:
>> M = Wref * Sref^{-1} * Mflirt * Sin * Win
>> where Mflirt is the flirt matrix, Sref is the scaling matrix
>> for the reference volume (diagonal values equal to
>> the absolute voxel dimensions and all non-diagonals zero),
>> Sin is the same for the input volume, Wref is the swap
>> matrix for the reference and Win is the same for the
>> input volume. The swap matrix is the identity if that
>> image is in "radiological" order, otherwise it is:
>> -1 0 0 Nx-1
>> 0 1 0 0
>> 0 0 1 0
>> 0 0 0 1
>>
>> where Nx is the number of voxels in the x-direction
>> (of the appropriate image).
>>
>> If you want to go to or from nifti mm coordinates, then
>> apply the appropriate sform or qform (your choice) to
>> the front (ref) or back (input) of the above equation.
>>
>> Hope this helps.
>> All the best,
>> Mark
>
|