Multiply two images using "Multiply Scalar Volumes" or "Mask Scalar Volume" and save the resulting image (using Python)

I am a beginner trying to automate some tasks in 3D Slicer using Python.

I have a mask image and another input volume image both in nifti format.
how can I use python to automate the multiplication between two images and save the output to a folder?

so far I was able to load the two images.

[success, volume] = slicer.util.loadVolume(filename = 'stripped_AX_FLAIR_stretched.nii',returnNode=True)
[success, labelmapVolumeNode] = slicer.util.loadLabelVolume(filename = 'Edema_AX_FLAIR_label.nii', returnNode=True)

You can use arrayFromVolume function as shown in this example in the script repository.

1 Like

Thanks for the reply Lasso, Certainly this is the way to proceed. but while executing the script. i get an error that the segment volume dimension is not aligned.

ValueError: shapes (26,512,512) and (13,179,150) not aligned: 512 (dim 2) != 179 (dim 1)

what can i do to overcome this error?

It seems that the two volumes have different sizes. How did you create them?

initially, I had a master_volume file and a label_file (which contains multiple labels).

to extract segment_volumes of each individual label from the label_file, I separated each label from label_file using the segment editor module and converted them back to individual label files and saved them to my folder directory.
(this process has been executed completely in Python)

Currently, I am trying to automate the task of masking the individual labels (extracted from label_file) on to the master_volume to extract the segment_volumes using python and save them as a nifti file.

for example
the first image is the individual label separated from label_file.
the second image is the result when masked with master_volume.

This should not be necessary. Instead, export the segmentation to labelmap volume (you can specify the volume node that you want to combine it with as reference volume) and then this exported labelmap volume will have the correct geometry. Using numpy, you can very easily extract a single label value, combine it with another array, etc.

1 Like

i am using slicer 4.10.2
in MacOS Catalina

seg_node = slicer.vtkMRMLSegmentationNode()
        seg = seg_node.GetSegmentation()
        lab_node = slicer.vtkMRMLLabelMapVolumeNode()

the code works fine till i reach the last line, when i execute ExportVisibleSegmentsTo LabelMapNode, my jupyter notebook kernel dies and 3d slicer crashes giving me a prompt to reopen 3d slicer.

Got the desired result using ‚ÄúExportSegmentsToLabelmapNode‚ÄĚ and by declaring ‚Äúreference volume‚ÄĚ dimensions are preserved.
As the dimensions are equal now, I was able to perform the multiplication operation.

Thanks for your guidance…! :slight_smile:

arr_vt = vtk.vtkStringArray()
result = arr_vt.GetValue(0)


a = slicer.util.arrayFromVolume(vol_1)
b = slicer.util.arrayFromVolume(vol_2)
c = a * b

volumeNode_result = slicer.modules.volumes.logic().CloneVolume(volume, ‚Äúmultiply‚ÄĚ)
slicer.util.updateVolumeFromArray(volumeNode_result, c)

1 Like