ADC histogram of segmented brain lesion

Good evening! Could you suggest me the easiest way to calculate the ADC histogram of a segmented brain lesion? I am able to segment a lesion and have its volume but still I don’t find the way to have ADC values.

I believe you will need to do a little scripting for that. Mask the ADC volume with the segmentation and then make a histogram like this:

Is it necessary to do it with Python? or there is an easiest workflow?
Is it possible to find some screenshots or a tutorial?

You can use the Mask Volume effect in Segment Editor to create a volume that only contain the voxels in the segmented region and you can see its histogram in Volumes module. but it will require about 10 clicks and all you get is a visual representation of the histogram.

Since in most cases you need to further process the histogram (e.g., compute a percentile) and might also want to analyze data in batch mode (instead of manually clicking through the GUI), it is usually more convenient to copy-paste a few lines of Python script into the Python console. You don’t need any Python scripting experience, just to be able to copy and paste code the code below into the Python console (hit Ctrl + 3 to show the console).

# Get input data (use the first volume, segmentation, and segment that are found in the scene)
volumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")
segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)

# Get voxel values of the volume in the segmented region
import numpy as np
volumeArray = slicer.util.arrayFromVolume(volumeNode)
segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId, volumeNode)
segmentVoxels = volumeArray[segmentArray != 0]

# Compute and plot histogram
import numpy as np
histogram = np.histogram(segmentVoxels, bins=50)
slicer.util.plot(histogram, xColumnIndex = 1)

(adapted from this code snippet in the script repository)

Thank you very muche for the answer. Unfortunatelly I find an error I can’t solve during the procedure, it’s like we give 3 arguments instead of 2?

volumeNode = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLScalarVolumeNode”)

volumeNode = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLScalarVolumeNode”)

segmentationNode = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLSegmentationNode”)
segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)
import numpy as np
volumeArray = slicer.util.arrayFromVolume(volumeNode)
segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segmentationNode, segmentId, volumeNode)
Traceback (most recent call last):
File “”, line 1, in
TypeError: arrayFromSegmentBinaryLabelmap() takes 2 positional arguments but 3 were given

I’m running into a similar error when pasting the above mentioned code into the python interactor.
What I would like to do is to obtain a histogram from a segmented area from a T2 relaxometry map. So I figured I could use the code in a comparable way. I use Slicer 4.11.0 on Windows 10 Pro.
Any help with this is highly appreciated, unfortunately I’m not very strong in coding…
Thank you a lot!
Please find a screenshot from my scene with the error message attached.

You need to use a recent Slicer Preview Release (Slicer-4.13).

Thanks a lot, it worked!

1 Like

Thank you! now it works!
Is it possible to download the entire dataset of the histogram to export them in excel?

Thanks a lot

The histogram is already in a table that is saved as a .csv or .tsv file.

You can hit Ctrl-S to save all nodes, including the table. Or you can go to Data module and right-click on the table node and choose Export to file....

Great! it works!
thank you very much

1 Like


I have a further question related to histograms.
In a current project, I am looking into qMRI parameters of tumor subregions, which I segmented in Segment Editor (one Segmentation-files contains several sub-segments created with the “add segment”-button). I am able to obtain a reasonable histogram using the code posted above, if there is only one segment. However, if I try the same with my current segmentation, I’m not able to get histograms for each sub-segment. This it how it looks:

I tried by making only one segment visible at a time, which didn’t work.

Is there a way to plot histograms for sub-segments or do I need to create separate segmentations for every sub-region I’m interested in?

Thanks a lot in advance!

segmentId variable selects the segment that you want to compute the histogram for. In the code snippet above, the first segment is used:

segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)

You can change 0 to 1, 2, … to select a different segment, or use GetSegmentIdBySegmentName:

segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName("Some segment name")

Thanks a lot, it worked!

I’m sorry, but I have another question related to this topic. I was able to obtain histograms and .csv-files for all regions I was interested in. What I ultimately want to do is to compare frequency distributions of a given MRI parameter in histologically defined tumor subregions of five samples. I plotted a frequency distribution of all samples using the information from the individual csv-files. However, I think, this is not correct, because I realized that the mean/median derived from this “pooled” frequency distribution are different from mean/median derived from segmentation statistics (I used “Segment Statistics” for that). I believe that this may have something to do with binning of the individual histograms. Could that be the case? If so, would it be better to get the value for each voxel in each segment of all samples separately and then plot a frequency distribution with individual voxel values? Is there a way to export the values for each voxel in a segmented region?

Hi this is really helpful, Thank you so much. Please can you add a code for exporting the data of the histogram for the segment as text or csv files?