<div dir="ltr"><div>Thank you, Mr. Gobbi!</div><div>I owe you one.</div><div>    -M<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Oct 12, 2018 at 2:11 PM David Gobbi <<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I made a typo in the code in my last reply.  It should be:<br>
<br>
if (referenceReader->GetQFac() < 0.0) {<br>
    writer->SetQFac(-1.0);<br>
}<br>
On Fri, Oct 12, 2018 at 12:08 PM David Gobbi <<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>> wrote:<br>
><br>
> For writing a nifti file after registration, it's very important to<br>
> set the sform and qform for the writer to the sform and qform of the<br>
> reference image (I.e. _not_ the qform, sform of the image that was<br>
> resliced!)<br>
><br>
> writer->SetSFormMatrix(referenceReader->GetSFormMatrix());<br>
> writer->SetQFormMatrix(referenceReader->GetQFormMatrix());<br>
> if (referenceReader->GetQFac() < 1) {<br>
>     writer->SetQFac(-1.0);<br>
> }<br>
><br>
> The QFac setting ensures that the slices are written in the same order<br>
> as the reference image.<br>
><br>
> It's possible to copy other parts of the header, too, but you have to<br>
> be careful when you do so, since some parts of the header relate to<br>
> the reference image, other parts of the header relate to the image<br>
> that was resliced, and some parts relate to neither.  Just copying the<br>
> qform and sform is usually sufficient.<br>
><br>
>   David<br>
><br>
> On Fri, Oct 12, 2018 at 11:47 AM The Merper <<a href="mailto:msmerps@gmail.com" target="_blank">msmerps@gmail.com</a>> wrote:<br>
> ><br>
> > Thanks very much for the speedy help, David & Jon.<br>
> > Applying the inverse of the landmark transform derived from the points solved the problem.<br>
> > 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.<br>
> ><br>
> > 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?<br>
> >    thank you again,<br>
> >       -M<br>
> ><br>
> ><br>
> > On Fri, Oct 12, 2018 at 10:31 AM David Gobbi <<a href="mailto:david.gobbi@gmail.com" target="_blank">david.gobbi@gmail.com</a>> wrote:<br>
> >><br>
> >> Hi Jon,<br>
> >><br>
> >> The vtkNIFTIImageReader ensures that the vtkImageData that it produces<br>
> >> follows the right-hand-rule, even if the slices were ordered in<br>
> >> reverse in the NIFTI file itself. As the documentation states:<br>
> >><br>
> >>   ...if QFac is -1, then the first slice in the VTK image data is the<br>
> >> last slice in the NIFTI file...<br>
> >><br>
> >> All the details of qfac are documented in the nifti header file:<br>
> >> <a href="https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h" rel="noreferrer" target="_blank">https://nifti.nimh.nih.gov/pub/dist/src/niftilib/nifti1.h</a><br>
> >><br>
> >> Use of the qform in VTK is optional.  Use it if you want to work in<br>
> >> RAS coordinates.  But often it is both simpler and more appropriate to<br>
> >> work in VTK data coordinates instead, and to me it sounds like this is<br>
> >> the case for the original question.  In any case, vtkImageReslice uses<br>
> >> VTK data coordinates (not VTK world coordinates, nor RAS coordinates).<br>
> >><br>
> >> I know this isn't a very clear explanation, but unfortunately it's all<br>
> >> I have time for (and all that my brain is capable of right now, given<br>
> >> the tiny amount of sleep I've had over the past few days).<br>
> >><br>
> >>  - David<br>
> >><br>
> >><br>
> >> On Fri, Oct 12, 2018 at 7:05 AM Jon Haitz Legarreta Gorroño<br>
> >> <<a href="mailto:jon.haitz.legarreta@gmail.com" target="_blank">jon.haitz.legarreta@gmail.com</a>> wrote:<br>
> >> ><br>
> >> > Hi,<br>
> >> > I'd dare to say that the short answer is yes.<br>
> >> ><br>
> >> > Long answer is that VTK deals with world coordinates, and according to<br>
> >> > the right hand rule. NIfTI uses RAS convention. According to the<br>
> >> > vtkNIFTIImageReader::GetQFormMatrix documentation, NIfTI does not<br>
> >> > ensure that coordinates are stored following that convention, so you<br>
> >> > should definitely use the matrix given.<br>
> >> ><br>
> >> > The following Slicer 3D wiki page and David Gobbi's document be<br>
> >> > helpful to this end:<br>
> >> > <a href="https://www.slicer.org/wiki/Coordinate_systems" rel="noreferrer" target="_blank">https://www.slicer.org/wiki/Coordinate_systems</a><br>
> >> > <a href="http://calgaryimageanalysis.ca/wiki/images/5/52/Image-orientation.pdf" rel="noreferrer" target="_blank">http://calgaryimageanalysis.ca/wiki/images/5/52/Image-orientation.pdf</a><br>
> >> ><br>
> >> > May be David himself can chime in and be able to provide a better or<br>
> >> > cleaner explanation.<br>
> >> ><br>
> >> > HTH,<br>
> >> > JON HAITZ<br>
> >> ><br>
> >> > On Thu, Oct 11, 2018 at 11:05 AM The Merper <<a href="mailto:msmerps@gmail.com" target="_blank">msmerps@gmail.com</a>> wrote:<br>
> >> > ><br>
> >> > > Hi VTK experts,<br>
> >> > >    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.<br>
> >> > >     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).<br>
> >> > >    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?<br>
> >> > >        thank you for any advice you can provide,<br>
> >> > >             -Ms. Merps<br>
</blockquote></div>