# NIFTI image to tensor/3d matrix representation

**URL:** <https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237>\
**Category:** Development\
**Created:** [December 17, 2022, 10:57am UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237 "2022-12-17T10:57:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![genzo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/genzo/32/6279_2.png) [@genzo](https://discourse.vtk.org/u/genzo)\
**Post date:** [December 17, 2022, 10:57am UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/1 "2022-12-17T10:57:02Z")

</div>

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](https://stackoverflow.com/questions/32137726/vtk-how-to-read-tensors-matrix-per-cell-from-a-nifti-image), 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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [December 17, 2022, 8:35pm UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/2 "2022-12-17T20:35:59Z")

</div>

> [@genzo](#):
>
> I need to apply a FFT on a 3D image (.nii.gz .nii files).

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.

---

<div class="post-metadata">

**Author:** ![genzo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/genzo/32/6279_2.png) [@genzo](https://discourse.vtk.org/u/genzo)\
**Post date:** [December 17, 2022, 9:29pm UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/3 "2022-12-17T21:29:12Z")

</div>

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

```auto
    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

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [December 17, 2022, 10:51pm UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/4 "2022-12-17T22:51:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![genzo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/genzo/32/6279_2.png) [@genzo](https://discourse.vtk.org/u/genzo)\
**Post date:** [December 17, 2022, 10:59pm UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/5 "2022-12-17T22:59:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [December 18, 2022, 12:12am UTC](https://discourse.vtk.org/t/nifti-image-to-tensor-3d-matrix-representation/10237/6 "2022-12-18T00:12:17Z")

</div>

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.
