Export segmentation axial slices and matching unsegmented axial slices as .png in different folders

Dear community,
i hope you are doing well. I have a T2 TSE TRA MRI dicom of the spine. I used the axial plane to segment the dural sack in 3 different slices. now i want to accomplish the following task:
1)export the segmented axial slices in the folder “segmented” as .png image
2)export the unsegmented corresponding axial slices (same name as segmented slices) to the folder “unsegmented” as .png image

I was able to build the following Python code which was supposed to do this task, however, when accessing the folders they show only black images. Maybe you have a better automated solution for me. I thought such automated way would be faster then manually exporting the slices. I am confident that you as experts will have a better solution for me. I also attached the image of my subject hierarchy. Thank you very much in advance.

Code:

import os
import vtk

# Set the export directories
non_segmented_export_directory = "C:\\Users\\Babak\\Desktop\\Automated_Dural_sack_measurement\\own_study\\non_segmented"
segmented_export_directory = "C:\\Users\\Babak\\Desktop\\Automated_Dural_sack_measurement\\own_study\\segmented"

# Get the original MRI volume node and the segmentation node
volume_node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')
segmentation_node = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode')

def capture_slice_screenshot(slice_widget, filename):
    # Capture the screenshot using vtkWindowToImageFilter
    window_to_image_filter = vtk.vtkWindowToImageFilter()
    window_to_image_filter.SetInput(slice_widget.sliceView().renderWindow())
    window_to_image_filter.Update()

    # Save the screenshot as a PNG file
    writer = vtk.vtkPNGWriter()
    writer.SetFileName(filename)
    writer.SetInputConnection(window_to_image_filter.GetOutputPort())
    writer.Write()

if volume_node is None or segmentation_node is None:
    print("Error: MRI volume or segmentation node not found. Please check the names and try again.")
else:
    # Create a labelmap from the segmentation
    labelmap_volume_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
    slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(segmentation_node, labelmap_volume_node, volume_node)

    # Get the slice widget and slice node for the red slice view
    slice_widget = slicer.app.layoutManager().sliceWidget("Red")
    slice_logic = slice_widget.sliceLogic()
    slice_node = slice_logic.GetSliceNode()
    slice_composite_node = slice_logic.GetSliceCompositeNode()

    # Get the RAS to IJK matrix
    ras_to_ijk = vtk.vtkMatrix4x4()
    volume_node.GetRASToIJKMatrix(ras_to_ijk)

    # Loop through the axial slices
    for slice_index in range(volume_node.GetImageData().GetDimensions()[2]):

        # Get the RAS coordinates for the current slice
        ijk_coordinates = [0, 0, slice_index, 1]
        ras_coordinates = ras_to_ijk.MultiplyPoint(ijk_coordinates)

        # Set the current slice index
        slice_node.SetSliceOffset(ras_coordinates[2])

        # Save the non-segmented slice
        non_segmented_filename = os.path.join(non_segmented_export_directory, f"slice_{slice_index:03d}_non_segmented.png")
        slice_composite_node.SetBackgroundVolumeID(volume_node.GetID())
        slice_composite_node.SetLabelVolumeID(None)
        slice_widget.sliceView().forceRender()
        capture_slice_screenshot(slice_widget, non_segmented_filename)

        # Save the segmented slice
        segmented_filename = os.path.join(segmented_export_directory, f"slice_{slice_index:03d}_segmented.png")
        slice_composite_node.SetLabelVolumeID(labelmap_volume_node.GetID())
        slice_widget.sliceView().forceRender()
        capture_slice_screenshot(slice_widget, segmented_filename)

Maybe you can just use the ScreenCapture module. If you need to script the behavior you can look at how it’s done in the Animator module.

1 Like