Call "Center Volume" button from python script

Hi all,

In the Volumes module, there is a “Center Volume” button. Unfortunately, I cannot find how to call this button from a python script… I know how to do this e.g. with an ITK workaround (using sitkUtils as sitku):

def centerVolume(volNodeName):
img = sitku.PullFromSlicer(volNodeName)
newCenter=-0.5*np.array(img.GetSize())*np.array(img.GetSpacing())
img.SetOrigin(tuple(newCenter.tolist()))
sitku.PushToSlicer(img,volNodeName,compositeView=0,overwrite=True)

However, this pushes an entirely new volume, and I lose the volumes location in the transformation chain and visualization settings (colormap, window/level/contrast etc.).
The “Center Volume” button just centers the volume coordinates without changing its properties in the mrml scene.

Thanks and cheers,
Ahmad

Use slicer.util.setSliceViewerLayers method. Type help(slicer.util.setSliceViewerLayers) for documentation.

You can always have a look at the source code if you are interested in what a method does internally. In this case, it is here.

Hi Andras,

thanks as always for your fast response!
I think there is a misunderstanding… I am not interesting in centering
the slice views on a certain volume.
Instead, I would like to call the logic of the button “Center Volume” (in
module “Volumes”, sub-panel “Volume Information”). It changes the “Image
Origin” property of the volume.

Cheers,
Ahmad

Code that computes the origin that centers the volume is available here.

However, volume centering should never be used for anything else than troubleshooting! Need for centering is typically a sign of an error in the data processing workflow.

Can you explain why you need to center the volume?

1 Like

Hi Andras,

I agree, usually centering volumes is dangerous, e.g. because it is difficult to undo (without code), and it is better to work with transformations and the transform hierarchy.
However, there are scenarios where it is useful, I have not used this feature in years, but recently, there were two situations where I used hard-centering:

  1. I created a new volume (e.g. a small Gaussian blob, i.e. a voxelized fiducial) from scratch in numpy, converted it to an ITK object via sitk and pushed it to Slicer via sitku. There, I would have liked to center it programatically, such that I can quickly apply transformations to it and accurately position the center of the volume (instead of, say one of the corners). I couldn’t find a way to trigger that button programatically, so I coded the centering on the ITK volume before pushing (with the code I wrote in my previous post). In this case, it’s ok, because I am pushing a new volume to the scene and I don’t care about pre-existing transformations/visualizations etc…
  2. I resampled small ROIs of the same anatomy in brain MRI from left and right hemisphere from which I wanted to create a group template. To do so, “Crop Volume” module resamples in-place (which is good), but to export the ROIs, I wanted to center all of them and L/R mirror all ROIs from left to right side in Slicer(!), so that the external template creation routine can run rightaway without initialization methods (the ROI center-point was carefully chosen for all crops). Before export, I wanted to check overlay quality of these crops, and the centering via sitku’s PullFromSlicer/sitk-center/PushToSlicer does the job, but the coloring and contrast of the ROI crops got lost, because a new volume is created. A bit inconvenient, but nothing dramatic.

These are both very peculiar and rare things to do, and the “Center Volume” button is rarely useful, but in these two cases, it would have done the job nicely.
Since it is not callable from code though, I will implement my own scripted method. Thanks for the link to the sample code!

Cheers,A

1 Like

Yes, the old sitkUtils push/pull methods were very limited and had several side effects (mess up visualization, create new nodes, etc). Use this new function instead, where you can specify an existing target node:

PushVolumeToSlicer(sitkimage, targetNode=None, name=None, className='vtkMRMLScalarVolumeNode')

Also note that for centering the volume, you don’t need to export it to a SimpleITK image. Just adjust the origin of the volume node using its SetOrigin() method.

1 Like

Thanks, Andras! Didn’t know about these new functions?
Where are they included? Can’t find it in sitkUtils… Do I have to download a new nightly version?

Yes, it is in the nightly. As the stable build is almost a year old, I would recommend everybody to use the nightly build. We’ll release a new stable version based on the current nightly version in a couple of weeks.

I do use the nightly, but it’s a couple of weeks old and you guys are just too fast! :slight_smile: So much new functionality on a constant basis, I should make a weekly habit of downloading the nightly. Thanks to you and the entire team for all the great work, Slicer has become a real workhorse in my research, since I learned how to script my workflows!

5 Likes