Save transformed data

Operating system: windows
Slicer version: 4.8.1

Hello,

I have ~50 sets of images that I have registered to a brain template. Would it be possible to save them after regridding to a common matrix? I would like to open the transformed images in Matlab.

-Nathan

You should be able to do it with a short Python script: load data, run CLI modules to register and resample, and save resampled images.

Is there no way to do it without jumping in and learning Python scripting?

It’s not something I have done before.

Seems like there should be a way to save reformatted data.

You can of course save iamges using the GUI. However, if you do batch processing of 50 data sets then you can save time by writing a few lines of Python.

Learning Python scripting and libraries is well worth the effort anyway, as Python ecosystem nowadays provides everything as Matlab and much more, for free, without relying on any proprietary, closed-source components.

I have tried “hardening” the transformation and saving it. The saved images are in their native coordinate spaces and orientations. Is there a way to save them all regridded to the same 3D matrix?

Sorry if this is an obvious question, but I can’t seem to find how to do this in the documentation.

The data is all registered and ready to go.

I have been meaning to learn Python, but don’t have time today to jump in.

Thanks,
Nathan

@Nathan sorry for the confusion. “Hardening” does not reformat the images, it only modifies the direction cosines matrix in the output. You should instead use Resample image module, setting your atlas image as reference, and the transformation nodes as “Transform files”.

You can also write a bash script to do this operation, since the resample module is a command-line tool. On mac it is in /Applications/Slicer.app/Contents/lib/Slicer-4.9/cli-modules/ResampleScalarVolume, if you run it with --help it will print all flags.

Let us know if this does not answer your question!

Awesome - thank you. I don’t know how I missed that module.

The images have multiple transformations applied to them (rigid registrations from time2 to time1 and from time1 to time0, and then affine + nonrigid registration between time0 and brain template via ANTS). Is there a way to deal with that, or should I harden the transformations before resampling? It seems that hardening sometimes results in cropping of the data.

-Nathan

To have optimal image quality, harden all the transforms in one step. If you have non-linear transforms among the transforms then resampling will be performed automatically, but the extent of the hardened volume will be the same as the original volume, so any parts outside the original extent will be cropped. For most resample modules you can specify an output geometry (by selecting a reference volume), you just have to set that output geometry to avoid any undesired cropping.

2 Likes

Thanks again for the help.

I am getting substantial cropping when I harden the transformations. Is there a way around this?

Not sure if it matters, but I am hardening the transformation in the “Data” module.

Affine Transformation
+Nonrigid Transformation
++Transformation (time1 to time0)
+++Transformation (time2 to time1)
++++Image data

One or more of the transformations moves the data outside its original coordinate space.

As I wrote before, if you harden in Data module, then the original volume’s geometry is used as output geometry. If the volume has moved, you can either use Crop volume module to define a region of interest where you want your volume to be cropped&resampled into, or use one of the resampling modules that accept a reference volume (and create a reference volume that is large enough and positioned so that no undesired cropping occurs).

When I “crop” the data to a bigger ROI, the registration/transformation is no longer valid.

It’s frustrating…I am looking at the data on my screen all nicely lined up and interpolated in Slicer. I just want to export it. I guess you can’t always get what you want.

Guess I will figure out how to regrid nonrigid registrations in Matlab.

You could also try hardening the transforms (harden at one level above the volume), and then use the resulting single transform as input to the resample module. Have you tried that?

I think I’ll run into the same problem since the data I am interested in examining was acquired in a limited volume:

I think it will always be cropped.

If you’re curious, we created a lesion in the thalamus in each patient as a treatment for tremor. You cannot see the thalamic nuclei in the MRI, so I did nonrigid registration to an atlas that has the nuclei segmented.

I was curious to visualize all the lesions superimposed on the atlas, and to calculate the probability for a lesion on the atlas for all patients. This would be trivial in Matlab if I could export the data on the same grid.

Have you considered do the processing in Python, using numpy, VTK, ITK, etc?

By the way, resampling volumes at an arbitrary ROI is very, very easy:

  • if you have a reference volume that defines the geometry, then specify that in a resample module
  • if you don’t have a preferred reference volume then use Crop volume module (then you define geometry using a freehand editable region of interest widget).

The problem is that there are four transformations of the data (2 rigid, nonrigid, than another rigid) before I get to the reference volume that I want to resample to. When I harden the transformation before resampling, the data is cropped.

If I crop the data to a larger ROI before hardening it, the transformations are no longer correct because that procedure puts the data into a new coordinate space.

Does that make sense?

It is not clear to me why that would be. If you register your data to the atlas, and you see the transformed data lining up with the atlas image before resampling, it should show the same way after you harden the transforms and apply the Resample volume module. It is either a bug, or there is some mis-communication.

Since I know you are local to BWH, I can meet with you in person tomorrow to troubleshoot this, if you like. I was hoping we could figure this out in the forum, just to see if we could help you as we would any other Slicer user, but it is probably more expedient to just meet and investigate this on the spot.

That would be great and will probably save us both a lot of time. My schedule is open tomorrow.

Andras,

Thank you for your help on this. I am sure that I am missing something obvious.

Andrey is going to come to my office today. Hopefully we’ll figure it out.

-Nathan

To follow up on this issue, I met with Nathan today, and the suggestion I made earlier was easy to implement and it worked.

We hardened the transform hierarchy at the level immediately above the volume node, and then used Resample Volume module to apply the resulting hardened transform to the moving volume, while specifying the atlas as reference.

FYI, I’ve implemented automatic update of image extent when non-linear transform is hardened on a volume, so clipping will no more occur.

Resampling with a reference volume is still be needed when the goal is to have various volumes with the same geometry. To achieve this, one volume can be transformed using hardening the transform; then this volume can be used as reference volume for resampling all the other volumes.