Python : how to centre volume on load?

Previously, using the ‘Add data’ menu, we would check the ‘Centre’ widget so that the loaded volume (DICOM series) was centered. The ‘Add data’ menu is no longer maintained for this use, and I’m using the DICOM module instead.

I could not find an option to centre a loaded volume in the DICOM module. I can always go the Volumes module to do that. I’m looking for an automated way however. This is because I have to CROP the volumes after loading them, using fixed ROIs (saved on storage) as templates. Studies from different OEMs have different centre point.

I tried this to no avail in slicerrc.py :

@vtk.calldata_type(vtk.VTK_OBJECT)
def onNodeAdded(caller, event, calldata):
  node = calldata
  if isinstance(node, slicer.vtkMRMLVolumeNode):
    volumesLogic = slicer.modules.volumes.logic()
    volumesLogic.CenterVolume(node)

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.NodeAddedEvent, onNodeAdded)

I could not find more useful code in the repository. (There’s a code snippet to centre the 3D view, but that’s not what I need to do.)

I’m asking for some help here for this task.

Thank you.

Looks like you are on the right track - you might try adding some print statements to confirm the node is what you expect. Maybe it doesn’t have the image data yet or something.

But, for your workflow, I understand you are loading loading dicoms as scalar volumes and you know how you plan to process them, so why not write a custom import feature. Even overload the drag-and-drop so that everything behaves in the most efficient way for you?

I’m not skilled at Python , and even less in Slicer’s internals. I was expecting a few liners solution here, I’ll try a few random inspirations wildly.

Thanks.

Center of the coordinate system is chosen by the scanner’s operator. It often contains somewhat useful information that is should not be simply discarded. Erasing the information would also make the volume spatially misaligned with other data structures that are defined in the same coordinate reference.

“Center volume” irreversibly erases image position and orientation, so it should not be used. This option was added decades ago and we haven’t removed it, because it is quite hidden and so unlikely that people would find it.

If you want to automatically fit templates to an image then image bounds are not ideal for this anyway, but you want to do a simple analysis of the image content instead. For example, if you apply simple thresholding then oriented bounding box or principal axes will tell you the physical location of the chosen object within the image.

This hinted me to modify my custom module, it restores back automated centering like before.

Removing it would be a major drawback to me. It’s deeply hidden, no one is seeing, so it can remain there :slight_smile:

Thanks.

Centering the volume is just a single line of code (set the origin to half the size of the volume), but you can do much better than that, with fully automatic positioning of the ROIs based on image content (using oriented bounding box or centroid/principal axes/moments).