NIFTI image to tensor/3d matrix representation

I need to apply a FFT on a 3D image (.nii.gz .nii files). Therefore I’d need a 3D/4D tensor representation of the vtkNIFTIImageReader in some way.

I’ve found this, but having a tensor for each cell I think is not efficient for my specific problem.

In other words: is there a way to, given a vtkImageReader, retrieve a 3D/4D tensor? If yes, how? I didn’t found much about that and is first time using vtk in c++.

The closest thing I had is: split the image by a verical plane to get the array of 2D images but is not very elegant and maybe not very performant.

vtkNIFTIImageReader can read nii and nii.gz 3D images without problems. These 3D images do not contain tensors, they are simple scalar-valued images with 3 spatial dimensions.

Ok that’s clear.
I try to approach this way:

    reader->Update();
    vtkImageData* data = reader->GetOutput();
    vtkPointData* point = data->GetPointData();

    vtkDataArray *tensors;
    tensors = point->GetScalars();
    tensors->Print(std::cout);
    double tensor[9];
    tensors->GetTuple(28900, tensor);
    for(int i = 0; i < 9 ; i++) std::cout << tensor[i] << std::endl;

But printing out I get that size is 62475, when printing the extent I find out that image is 34x50x34 which is not 62475. Am I getting something conceptual wrong or the code to get the scalar valued images wrong?

And also, in the tensor value for whatever point I put I get always the same numbers except the first one:

27
6.90013e-310
1.97626e-323
6.95261e-310
4.94066e-324
6.95261e-310
6.95261e-310
2.08559e-317
1.97626e-323

Thanks for the patience

Nomally you just use the vtkImageData in filters. There should be no need to use lower-level point/cell data API for images.

What are you trying to achieve?

I need to apply an FFT algorithm of mine that I wrote, not library FFT, on the 3D representation of image. so I’d need the nifti image to be processed pixel by pixel. Storing the pixels in some tensor/3D matrix due to algorithm charateristics.

1 Like

You can use GetScalarPointer to get raw memory pointer to voxel data.

Note that VTK supports different memory layouts, so when you use the memory area check that it has the layout that you expect.