vtkNIFTIWriter changes imagedata coordinate automatically

Hi all,
I’m having a trouble of imagedata transforming, it seems will change coordinate information automatically after I save it as .nii file and read it again. Here is my steps:

  1. read dicom series.
  2. use vtkImageReslice to relocate the image data.
    –visualize as actor1(black)
  3. write is as .nii format via vtkNIFTIWriter.
  4. read the generated .nii file by vtkNIFTIReader
    –visualize as actorResult2(blue)

Then I found that the imagedata’s postion got changed automatically between actor1 and actorResult2.
the detail steps is in the picture below:


(the black actor is before saving, the blue actor is after save and read as .nii)

It would be nice if I could know how to save as .nii file without changing the imagedata position,
or maybe if there’s a way to know how exactly(matrix) did it change when saving as NIFTI file.
Could someone help me to take a look for it?
Thanks!

@dgobbi :slight_smile:

here’s the source code, could use any dicom directory path to replace “3D-T1_Directory_Path”
and change IF_TRANSFORM to see the result with if reslice(relocate).

testTransform.cpp (5.4 KB)

When you read an image with vtkNIFTIImageReader, the image orientation and offset are stored in a separate matrix (either the QFormMatrix of the SFormMatrix). So to properly deal with the orientations of these files, you must deal with the matrix as well as the image.

vtkNew<vtkNIFTImageReader> reader;
reader->SetFileName("something.nii");
reader->Update();
vtkMatrix4x4* niftiMatrix = reader->GetQFormMatrix();

The NIFTI writer has a matching SetQFormMatrix() method.

If you ignore the matrix, then both the orientation and the offset will be lost if you simply read the NIFTI file and then write it out again. So my first advice is, try using the matrix and see how that changes the results.

Another important thing is: DICOM and NIFTI use different definitions for their coordinate systems.

1 Like

Hey David,

This matrix works well in my case, I successfully restored the imagedata’s postion by it. Thanks for the solution!

Then I tried to check this part too: read dicom → write as nii1 → read nii1 → write nii2 → read nii2.
I’ll lose the matrix there If I didn’t call SetQFormMatrix() method when writing nii2.

And yes I do realize they use different coordinate now, going to check some more about this, thanks for your help again!