read Nifti and create transform

Hello,
I have a nii file. I can use SimpleITK to read it in c#.
But it’s didn’t combine the transform information.
It’s seems display the original image.

I think maybe I should use the “Resample” to do this.
But I don’t know how should I create the Transform by using the information in nii.

thanks

Hello @jeffppp,

Please clarify what you mean by “didn’t combine the transformation information”.

If you mean that when the image was read its internal meta-data information (origin, direction cosine matrix) was not applied, then please share the data as that would be a bug in ITK/SimpleITK. Less likely because it is widely used, still always a possibility.

If the scenario is that you performed registration, then that is a different thing. The output of the registration is a transformation. Registration itself does not modify the input images. You can either save the transformation and use it with software that knows how to work with transformations and images and how to combine them (e.g. Slicer) or you can resample the image and save that. The relevant methods are sitk.Resample, sitk.WriteTransform, sitk.WriteImage.

1 Like

Thanks your reply.

I’m not sure if I have some mistake.
So I paste my code at first.
I use SimpleITK to read the image and convert to VTK.

Dictionary<string, object> Dict = new Dictionary<string, object>();
                Image optimg = SimpleITK.ReadImage(FilePath, PixelIDValueEnum.sitkFloat32, "NiftiImageIO");
                Dict.Add("ITK", optimg);
                vtkImageImport Image1 = vtkImageImport.New();
                Image1.SetImportVoidPointer(optimg.GetBufferAsFloat());
                Image1.SetDataScalarTypeToFloat();
                Image1.SetWholeExtent(0, (int)optimg.GetWidth() - 1, 0, (int)optimg.GetHeight() - 1, 0, (int)optimg.GetDepth() - 1);
                Image1.SetDataOrigin(optimg.GetOrigin()[0], optimg.GetOrigin()[1], optimg.GetOrigin()[2]);
                Image1.SetDataSpacing(optimg.GetSpacing()[0], optimg.GetSpacing()[1], optimg.GetSpacing()[2]);
                Image1.SetDataExtentToWholeExtent();
                Dict.Add("VTK", Image1);

And I showed the image with VTK, but the orientation is not correct.

The files were got from other people.
It’s should be a pair registered data.
When I use “Matlab/SPM/Check Reg”, the data seems correct.
But use my code is not correct.

My data link
https://drive.google.com/drive/folders/1ZcgeyexAT7085J0V_mcxEsQ_QXuK8b-X?usp=sharing

thanks

What version of VTK are you using? Support for image orientation is relatively recent in VTK. Also not all filters in VTK actually use the orientation matrix. I know that Marching Cubes does not.

1 Like

My VTK was got from NuGet
Activiz.NET 5.8.0

Your code doesn’t have a call to Image1.SetDataDirection. You need to call that to set the orientation matrix.

There is not SetDataDirection in vtkImageImport.
How should I do this?

I think direction support for vtkImageData was only added with VTK 9. If you don’t have SetDataDirection that suggest that you’re using a version older than that. Calling vtkVersion.vetGetVersion() will tell you.

I haven’t used Activiz.NET, but it wouldn’t surprise me if it isn’t on the latest VTK version.

I wrote a SimpleITK Python function to ‘regularize’ a volume. It resamples the volume so that the orientation matrix is identity and voxels are cubicly spaced. You can see it here:

You’d have to re-write it in C#, obviously.

1 Like

You are right. My vtkversion is 5.8. So I don’t have the SetDataDirection.
And I think the key point is Resample like yours code in python.
I will try to re-write it in C# at first.

Thanks

1 Like