[vtkusers] using vtkLandmarkTransform to align two nii volumes

David Gobbi david.gobbi at gmail.com
Fri Oct 12 14:11:45 EDT 2018


I made a typo in the code in my last reply.  It should be:

if (referenceReader->GetQFac() < 0.0) {
    writer->SetQFac(-1.0);
}
On Fri, Oct 12, 2018 at 12:08 PM David Gobbi <david.gobbi at gmail.com> wrote:
>
> For writing a nifti file after registration, it's very important to
> set the sform and qform for the writer to the sform and qform of the
> reference image (I.e. _not_ the qform, sform of the image that was
> resliced!)
>
> writer->SetSFormMatrix(referenceReader->GetSFormMatrix());
> writer->SetQFormMatrix(referenceReader->GetQFormMatrix());
> if (referenceReader->GetQFac() < 1) {
>     writer->SetQFac(-1.0);
> }
>
> The QFac setting ensures that the slices are written in the same order
> as the reference image.
>
> It's possible to copy other parts of the header, too, but you have to
> be careful when you do so, since some parts of the header relate to
> the reference image, other parts of the header relate to the image
> that was resliced, and some parts relate to neither.  Just copying the
> qform and sform is usually sufficient.
>
>   David
>
> On Fri, Oct 12, 2018 at 11:47 AM The Merper <msmerps at gmail.com> wrote:
> >
> > Thanks very much for the speedy help, David & Jon.
> > Applying the inverse of the landmark transform derived from the points solved the problem.
> > I still don't understand why for polydata you apply the landmark transform and for image data you apply the inverse, but that's Ok for now.
> >
> > I also want to write the transformed nii volume to file. In addition to using SetInformationInput to set the transformed nii volume's spacing, origin, and extent, is there any other information I should be modifying in the transformed Nii's header?
> >    thank you again,
> >       -M
> >
> >
> > On Fri, Oct 12, 2018 at 10:31 AM David Gobbi <david.gobbi at gmail.com> wrote:
> >>
> >> Hi Jon,
> >>
> >> The vtkNIFTIImageReader ensures that the vtkImageData that it produces
> >> follows the right-hand-rule, even if the slices were ordered in
> >> reverse in the NIFTI file itself. As the documentation states:
> >>
> >>   ...if QFac is -1, then the first slice in the VTK image data is the
> >> last slice in the NIFTI file...
> >>
> >> All the details of qfac are documented in the nifti header file:
> >> https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h
> >>
> >> Use of the qform in VTK is optional.  Use it if you want to work in
> >> RAS coordinates.  But often it is both simpler and more appropriate to
> >> work in VTK data coordinates instead, and to me it sounds like this is
> >> the case for the original question.  In any case, vtkImageReslice uses
> >> VTK data coordinates (not VTK world coordinates, nor RAS coordinates).
> >>
> >> I know this isn't a very clear explanation, but unfortunately it's all
> >> I have time for (and all that my brain is capable of right now, given
> >> the tiny amount of sleep I've had over the past few days).
> >>
> >>  - David
> >>
> >>
> >> On Fri, Oct 12, 2018 at 7:05 AM Jon Haitz Legarreta Gorroño
> >> <jon.haitz.legarreta at gmail.com> wrote:
> >> >
> >> > Hi,
> >> > I'd dare to say that the short answer is yes.
> >> >
> >> > Long answer is that VTK deals with world coordinates, and according to
> >> > the right hand rule. NIfTI uses RAS convention. According to the
> >> > vtkNIFTIImageReader::GetQFormMatrix documentation, NIfTI does not
> >> > ensure that coordinates are stored following that convention, so you
> >> > should definitely use the matrix given.
> >> >
> >> > The following Slicer 3D wiki page and David Gobbi's document be
> >> > helpful to this end:
> >> > https://www.slicer.org/wiki/Coordinate_systems
> >> > http://calgaryimageanalysis.ca/wiki/images/5/52/Image-orientation.pdf
> >> >
> >> > May be David himself can chime in and be able to provide a better or
> >> > cleaner explanation.
> >> >
> >> > HTH,
> >> > JON HAITZ
> >> >
> >> > On Thu, Oct 11, 2018 at 11:05 AM The Merper <msmerps at gmail.com> wrote:
> >> > >
> >> > > Hi VTK experts,
> >> > >    I am trying to using vtkLandmarkTransform  to align two nii volumes and am getting puzzling results. I identify the landmarks in the two 3D volumes by visualizing the volumes in VTK and using the GetPickPosition method. The coordinates I get are in units of mm with the origin being one corner of 3D volume.
> >> > >     I then use vtkLandmarkTransform to get an affine transformation to align the points. The points align great, but when I apply that same transform to the analogous nii volume via vtkImageReslice, the results are wildly off (e.g., the volume is flipped upside down).
> >> > >    Is the qform matrix of each nii file somehow causing this? Do I need to first apply the qform matrix to the landmark points to get them in scanner coordinates before using vtkLandmarkTransform?
> >> > >        thank you for any advice you can provide,
> >> > >             -Ms. Merps


More information about the vtkusers mailing list