# Compute Dice score, Hausdorff distance for Multi-Region segmentation

**URL:** <https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [April 19, 2022, 12:41am UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006 "2022-04-19T00:41:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Js165](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/js165/32/14631_2.png) [@Js165](https://discourse.slicer.org/u/Js165)\
**Post date:** [April 19, 2022, 12:41am UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/1 "2022-04-19T00:41:38Z")

</div>

Hi,  
I have NIFTI files of sizes (512 x 512 x 150) of auto-segmentation and manual segmentation. **Each file/slice contains the segmentation of multiple regions (seven regions).** I want to compute the dice score, Hausdorff distance, Jaccard index, and average surface distance.  
Could you please suggest the easiest method for that purpose? I don’t want to convert NIFTI to PNGs/JPEGs. I tried the [plastimatch dice](http://plastimatch.org/plastimatch.html#plastimatch-dice). It seems that it doesn’t work for multi-region segmentation. Please find the command and relevant output of plastimatch. Please suggest if I am missing anything or if there is a better approach.

plastimatch dice --all manual\_GT.nii Auto\_seg.nii  
CENTER\_OF\_MASS  
ref 15.9689 -56.3064 -32.7915  
cmp 16.4171 -56.3639 -35.121  
TP: 194139  
TN: 40941820  
FN: 10594  
FP: 10055  
DICE: 0.949504  
SE: 0.948255  
SP: 0.999754  
Hausdorff distance = 12.750913  
Avg average Hausdorff distance = 0.131478  
Max average Hausdorff distance = 0.160547  
Percent (0.95) Hausdorff distance = 0.634766  
Hausdorff distance (boundary) = 27.516542  
Avg average Hausdorff distance (boundary) = 1.705389  
Max average Hausdorff distance (boundary) = 1.814141  
Percent (0.95) Hausdorff distance (boundary) = 5.156286

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [April 19, 2022, 8:59am UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/2 "2022-04-19T08:59:19Z")

</div>

The [Segment Comparison](https://www.slicer.org/wiki/Documentation/Nightly/Modules/SegmentComparison) module in the SlicerRT extension allows you to compute Dice and Hausdorff on segmentations containing multiple labels. You’ll need to first convert the labelmap volumes to segmentation nodes (in Segmentations module Import/Export section, or in the Data module by right-clicking the labelmap volume).

---

<div class="post-metadata">

**Author:** ![Js165](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/js165/32/14631_2.png) [@Js165](https://discourse.slicer.org/u/Js165)\
**Post date:** [April 19, 2022, 1:56pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/3 "2022-04-19T13:56:19Z")

</div>

@cpinter Thanks a lot! It worked. I love Slicer! Btw, some features can be improved and can be made more interactive by incorporating some sort of automation. For example, the “Segment Comparison” tool should have the Box/tick option for all segments rather than the drop-down menu.

---

<div class="post-metadata">

**Author:** ![thanuja](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@thanuja](https://discourse.slicer.org/u/thanuja)\
**Post date:** [June 16, 2023, 7:52pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/4 "2023-06-16T19:52:26Z")

</div>

@cpinter Could you please point out where can I find the source code for Segment Comparison module (Version 5.2.2). I’m getting different values of dice score for 3D binary nifti masks when using 3D Slicer and Monai framework. This is the monai function I’m using:

```auto
def calculate_dice_score(predicted, ground_truth):
      dice_metric = monai.metrics.DiceMetric(include_background=False, reduction="mean")
      # Convert the predicted and ground truth masks to PyTorch tensors
      predicted_tensor = torch.from_numpy(predicted)
      ground_truth_tensor = torch.from_numpy(ground_truth)
      # Reshape tensors to have batch size and number of channels
      predicted_tensor = predicted_tensor.unsqueeze(0).unsqueeze(0)
      ground_truth_tensor = ground_truth_tensor.unsqueeze(0).unsqueeze(0)
      # Calculate the Dice score
      dice_score = dice_metric(y_pred =predicted_tensor, y = ground_truth_tensor)
      return dice_score.item()

```

Thanks!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 19, 2023, 12:53pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/5 "2023-06-19T12:53:05Z")

</div>

Please create a new topic for this and I’ll answer there. If we keep reusing year-old topic nobidy will be able to find anything here. Thanks!

---

<div class="post-metadata">

**Author:** ![thanuja](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@thanuja](https://discourse.slicer.org/u/thanuja)\
**Post date:** [June 19, 2023, 3:45pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/6 "2023-06-19T15:45:18Z")

</div>

Noted. Thank you! Created a new topic: [Dice score calculation of 3D binary masks using Segment Comparison module](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124)

---

<div class="post-metadata">

**Author:** ![Young\_Wang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/young_wang/32/13926_2.png) [@Young\_Wang](https://discourse.slicer.org/u/Young_Wang)\
**Post date:** [December 13, 2023, 9:53pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/7 "2023-12-13T21:53:43Z")

</div>

@cpinter thank you. However I have trouble locating Dice and Hausdorff from SlicerRT extension. I’m using Slicer 5.6. Could you please point me where they are.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [December 14, 2023, 10:17am UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/8 "2023-12-14T10:17:44Z")

</div>

It’s in the Segment Comparison module. What operating system do you use? There have been issues on Mac lately with some extensions.

---

<div class="post-metadata">

**Author:** ![Young\_Wang](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/young_wang/32/13926_2.png) [@Young\_Wang](https://discourse.slicer.org/u/Young_Wang)\
**Post date:** [December 14, 2023, 4:58pm UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/9 "2023-12-14T16:58:17Z")

</div>

@cpinter , thanks for getting back to me. I use 14.2 (23C64) on an Apple M1 Pro. I couldn’t find this Segment Comparison module on Slicer 5.6 and Slicer 5.6.1. But I was able to find it on Slicer 5.2 after I revert it back.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 10, 2024, 11:49am UTC](https://discourse.slicer.org/t/compute-dice-score-hausdorff-distance-for-multi-region-segmentation/23006/10 "2024-05-10T11:49:50Z")

</div>

3 posts were merged into an existing topic: [Hausdorff Distance Interval](https://discourse.slicer.org/t/hausdorff-distance-interval/35998/3)
