How to do batch registration?

I want to register a batch of CT and MR volumes automatically using 3DSlicer.For example:
A1——>B1
A2——>B2
A3——>B3
A4——>B4

I’ve briefly read the description for Sequence Registration,but that seems more like 4D registration(i.e. registrate intra-patient), which is different with my problem.
Is there any module in 3DSlicer can satisfy my requirement ?
Thank you very much if anyone can provide some advice~~~ :grinning: :grinning: :grinning:

You can write a short Python script that loads each image pair, runs registration (e.g., using SlicerElastix), and save the result. What would you like to achieve with these registrations?

Thank you for your quik reply, lassoan!
I find the registerVolumes method in the Elastix.ElastixLogic:
Elastix_Module
Next time

  1. I will try to write a script to call this registerVolumes method to batch process the CT-MR pairs and output its corresponding registered CT-MR pairs.
  2. Add my script into 3DSlicer as a Module.
    LoadDIYModule
  3. Run this Module.

Is this procedure right? Or there’re other easier ways?
Again,thank you for your kind reply !! :grinning: :grinning:

If you don’t need GUI for the processing in Slicer then it may be simpler to run scripts directly from Slicer’s Python console or from the command line or use Slicer from a Jupyter notebook (using SlicerJupyter extension).

In fact,I don’t know what’re the functions to call in Slicer’s Python console for batch registration. Any tutorials for this ?
Thank you~~~

Sequence Registration module is a good (but somewhat complex) example of how SlicerElastix module can be used for registration from a script or another module:

https://github.com/moselhy/SlicerSequenceRegistration/blob/e9ac5c24e30ecee6a0f8dfee472aa54ff11497cf/SequenceRegistration/SequenceRegistration.py#L557-L562

1 Like

Thank you ,yes it’s really somewhat complex for me a newbee… :joy:I’ve solved the batch registration problem in windows:

  1. Write a batch registration script: example
## Run the following command in Python Console.
# execfile("path\to\BatchRegister.py")
def Register(Fixedfilename,Movingfilename,OutVolumefilename,ind):
    Nodename = 'Volume_{:02d}'.format(ind)
    RegistrationPresets_ParameterFilenames = 5
    # Load Volume
    [ success,movingVolumeNode ] = slicer.util.loadVolume(Movingfilename,returnNode=True)
    [ success, fixedVolumeNode] = slicer.util.loadVolume(Fixedfilename,returnNode=True)
    

    from Elastix import ElastixLogic
    logic = ElastixLogic()
    parameterFilenames = logic.getRegistrationPresets()[0][RegistrationPresets_ParameterFilenames]
    outputVolume = slicer.vtkMRMLScalarVolumeNode()
    slicer.mrmlScene.AddNode(outputVolume)
    outputVolume.CreateDefaultDisplayNodes()
    outputVolume.SetName(Nodename)
    logic.registerVolumes(fixedVolumeNode, movingVolumeNode, parameterFilenames = parameterFilenames , outputVolumeNode = outputVolume)

    # Create OutputVolume Node.
    myNode = getNode(Nodename)
    myStorageNode = myNode.CreateDefaultStorageNode()
    myStorageNode.SetFileName(OutVolumefilename)
    myStorageNode.WriteData(myNode)
    slicer.mrmlScene.Clear(0)



def BatchRegister():
    path2subjects = 'path\to\dataset_directory'
    import os
    for ind,dir in enumerate(os.listdir(path2subjects)):
        Fixedfilename = os.path.join(path2subjects,dir,'Fixed.ext')
        print(Fixedfilename)
        Movingfilename = os.path.join(path2subjects,dir,'Moving.ext')
        OutVolumefilename = os.path.join(path2subjects,dir,'OutVolume.ext')
        Register(Fixedfilename, Movingfilename, OutVolumefilename,ind)
BatchRegister()

2.Execute the Python script in the Slicer’s Python command as follow:
>>execfile("path\to\BatchRegister.py")

5 Likes

Very nice. You can also launch your script from the command-line using Slicer.exe path\to\BatchRegister.py. You can also pass arguments to the script from the command-line (so that you don’t need to hardcode paths or file names) and access them in the script using sys.argv.

1 Like

Oh,Thank you,that’s a good programming habit.
And thank you for your patient replies recently.
Happy New Year~~~ :grinning:

1 Like

Thanks @lassoan @Scorbinwen for this thread. I’m trying to write a script that co-registers all loaded images to a template image of the same patient and this thread is the closest to such solution.

So my problem is more like:
A1 (3D T1 brain MRI) ← A2 (FLAIR) ← A3 (T1) ← A4 etc…

For simplicity, I’d just add the string FIXEDVOL to the fixed image as I’d identify this images manually anyway (highest res, least motion artifact, etc).

FixedVol = getNodes(‘*FIXEDVOL *’)
PoolOfVolumes = getNodes(‘*vtkMRMLScalarVolumeNode *’)

and I would just run a for loop to process each line in PoolOfVolumes as MovingVolume with new co-regidstered volumes generated named as ‘MovingVolume’&‘_COREG’

What lines should I specify for Elastix to do this coregistration? How can I ensure that the new volume follows the above naming scheme?

(I’m a total noob to python:( )

Thanks/Köszönöm!

Dave

Never mind. Here’s the script using BRAINSfit:

Requires you to add the string FIXED (case sensitive) to the volume you want to use as the Fixed volume.

PoolOfVolumes = getNodesByClass('vtkMRMLScalarVolumeNode')
FixedVol = getNode('*FIXED*')
for MovingVol in PoolOfVolumes:
    slicer.util.selectModule('BRAINSFit')
    brainsFit = slicer.modules.brainsfit   
    parametersRigid = {}
    parametersRigid["fixedVolume"] = FixedVol.GetID()
    parametersRigid["movingVolume"] = MovingVol.GetID()
    parametersRigid["outputVolume"] = MovingVol.GetID()
    parametersRigid["useRigid"] = True
    parametersRigid["initializeTransformMode"] = "useGeometryAlign"
    parametersRigid["samplingPercentage"] = 0.02
    cliBrainsFitRigidNode = slicer.cli.run(brainsFit, None, parametersRigid)

Tested in Slicer 5.0.2 r30822 / a4420c3 on MacOS 12.4, YMMY.

Best,

Dave