combine two volumes into one using python

Hi, I need to automate combining two volumes into one. I have DICOM files of whole body CT scans of a body, split into parts. The parts overlap slightly. My aim is to be able to quickly merge the torso and legs to obtain one volume for segmentation. Manually, slicer provides a chance to ‘add’ two volumes arithmetically, as long as one of the volumes has been extended to include the size of the other.

Following this logic, I need to script:

a. ‘cropping’ the torso volume to a larger region of interest so that it includes legs,
b. cropping the leg volume to the lowest point of the Z coordinates in the torso volume to avoid overlap,
c. adding two cropped volumes to make one,
d. pushing the new volume to the slicer.

Thanks to discussions here and to previously published code, the problem does not seem impossible to solve. However, I cannot see an answer for defining XYZ boundaries of my regions of interest. Can you help, please?

You can use Crop volume module to create an ROI that fits the volume, adjust it manually so that it includes the entire region that you are interested in, then created the “cropped” output (it will be actually larger than the input volume). You can then use image "Resample Scalar/Vector/DWI Volumemodule with the "cropped" output volume chosen asReference volume` to get all the images in the same voxel coordinate space.

Hi, thanks for the reply but I need to avoid manual adjustment. It needs to be precise, based on coordinates of the volumes’ top and bottom. Can it be done programmatically?

Yes, this can certainly be done by scripting. Look at the code in CropVolume and you’ll see how to get the bounds of volumes in RAS space and how ROIs are manipulated. Make a new volume big enough for all the parts and copy or add the data into it. You can decide how to deal with any overlaps, maybe crop on side to fit the other. This problem doesn’t come up that often so there’s no module for it, but if you come up with some code please post to help the next person.

Here is a link to a module I created to do this: GitHub - mikebind/SlicerStitchImageVolumes: A 3D Slicer (slicer.org) module for stitching together multiple image volumes into a single larger image volume. The ROI still needs to be created manually because for my workflows it was helpful to be able to cut out large regions of non-patient present in the full scans, but I think this will do everything else you want.

Let me know if you run into problems. It was created a while back, so it initially was set up to use the legacy AnnotationROI objects for the ROI, but I believe it has been updated to work equally well with the newer MarkupsROI objects.

2 Likes

@mikebind The module looks very useful. The image stitching has come up a number of times on the forum, so it could make sense to make it available for users via the extension manager. You could consider submitting the extension to the extensions index but if you find that too much work or looks like too big of a commitment to provide and support an extension, then you can add the module to the Sandbox extension. It contains many experimental modules that are already useful but still incomplete or may need a bit of more polishing.

2 Likes

Thanks @lassoan, I will try to add it to the Sandbox extension soon. I think it isn’t really polished enough for a standalone extension (it should be more configurable), but it is also useful as is, so that sounds like a good place for it to end up.

2 Likes

@mikebind – thank you for the code, really appreciated!

2 Likes

@lassoan, may I return please to the markup ROI? I am playing with this option and it is great in Slicer. However, when trying the script (’ Fit markups ROI to volume’ from script repository) through a Jupiter notebook, the ROI is created but fitting fails. Why so?