Histogram of volume in regions defined by segments

Hi.
I am trying to apply a mask (LabelMap) to a volume in slicer to plot histograms of intensities within the masked region of interest. The necessary requisite is that I do all this programmatically using segmentation map (without using the GUI to export it to label Map). So the label map i get from code below is already confined to segmentation map bounding box (and not the entire volume as you would get if you exported the segmentation map):

seg = getNode('Segmentation')
labelMapNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, labelMapNode)

Before applying the mask to the ROI, i am trying to crop the ROI but failing (believe it can be done using VTK filters but using the crop image filter (i have the code) was also failing):

#========= GETTING MASK ARRAY FROM LABELMAP TO NUMPY ==========
maskSize = labelMapNode.GetImageData().GetDimensions()
mask = labelMapNode.GetImageData().GetPointData().GetScalars()
vtk_mask= vtk_to_numpy(mask)
roi_size  = labelMapNode.GetImageData().GetDimensions()
mask = vtk_mask.reshape(roi_size)   

#====COORDINATES OF MASK ARRAY RELATIVE TO THE MAIN IMAGE ===========
location_mask = mat.MultiplyDoublePoint(labelMapNode.GetOrigin()+(1,))
location_mask =  np.array(location_mask).astype(int)[:3]


#============ GETTING BOUNDED REGION OF INTEREST FROM MAIN VOLUME
roi =  img[location_mask[0]:location_mask[0]+roi_size[0],
    location_mask[1]:location_mask[1]+roi_size[1],
    location_mask[2]:location_mask[2]+roi_size[2]]


#=============== CONVERTING ROI INTO A NEW VOLUME NODE========================
outputVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode')

sitkImageOutput = sitk.GetImageFromArray(roi)
sitkUtils.PushVolumeToSlicer(sitkImageOutput, outputVolumeNode)
outputVolumeNode.SetOrigin(inputVolumeNode.GetOrigin())
outputVolumeNode.SetSpacing(inputVolumeNode.GetSpacing())

The resulting ROI inside slicer just looks very strange and seems to have gaps in it. Is there some interpolation step there? I am sure there is an easier way to do this using filters and i will appreciate the help. The goal is to draw a histogram based on ROI.

Thanks

1 Like

Here is a complete example that does exactly what you need (computes histogram for each region of a volume defined by a segment). It use Mask volume effect of SegmentEditorExtraEffects extension, so you need to install that extension.

5 Likes

Hi Andras,

The plotting part of this is really similar to something I was trying to get working in a module I’ve been working on - is vtkMRMLPlotSeriesNode new? When I try to make one in a nightly from January, Slicer crashes.

Thanks!

-Hollister

Yes, Plots module is fully reworked and vastly improved 1-2 weeks ago. The example script above requires these changes.

2 Likes

Hi Lassoan,

Thank you for sharing this, if I want to do it on my own CT dicom images, how can I change the code ?

best
Tenzin

Basically what I would like to ask is how can I use this python file into the 3d slicer and my second question is how can I change the code to use my own volume and segmentation.

best
Tenzin

And also last thing is that I am using segment editor on 3d slicer, so I would like to be able to get the histogram of the segmentation that I just draw.

best
Tenzin

You can use the code by copy-pasting it to Slicer’s Python console (hit Ctrl-3 to display it). If you want to write automated processing code then skip the first part that generates test data and provide your nodes as input instead.

There is now an effect called “Split volume” that extracts each segmented region into a separate volume. You may use this effect and then for each extracted volume you can find the histogram in Volumes module (or compute histogram for these already masked volumes by a few numpy commands).

Hello Prof Lasso,
Thank you so much for this. My students and I have been struggling with doing this for weeks. I will like to install SegmentEditorExtraEffects via Extension Manager in version 4.10.2
Meanwhile, I will like to request for the steps involved in computing histogram for each region of a volume defined by a segment. Secondly, I would not mind any materials that will help me in exploring the full power of SegmentEditorExtraEffects.
Many thanks.
Michael

It is available in the Extension Manager. If you have trouble installing it then please describe what you do, what you expect to happen, and what happens instead.

You can use the script referenced above. if you have your own image and segmentation then skip lines above # Perform analysis and make sure segmentationNode and masterVolumeNode variables are set to your data nodes.

These are just some additional effects for Segment Editor module. You can find tutorials about Segment Editor here.