# Minimum Distance Between Segmentations

**URL:** <https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [November 16, 2020, 6:46pm UTC](https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646 "2020-11-16T18:46:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahopf](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@ahopf](https://discourse.slicer.org/u/ahopf)\
**Post date:** [November 16, 2020, 6:46pm UTC](https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646/1 "2020-11-16T18:46:41Z")

</div>

I am trying to calculate the minimum Hausdorff distance between two segmentations. I am new to coding and wondering if someone can tell me if the code (from the Support discussion thread “Distance between two segments”) as I have it is correct according to the pictured scene:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/e/3edbfd4861958f9883433367e17a2e224e9a5cf5.png)

```auto
import vtkSlicerSegmentComparisonModuleLogicPython
s=getNode('Left Excised Volume') 
p1=s.GetClosedSurfaceRepresentation('Left Excised Volume') 
p2=s.GetClosedSurfaceRepresentation('Left Structures')
pdf=vtkSlicerSegmentComparisonModuleLogicPython.vtkPolyDataDistanceHistogramFilter()
pdf.SetInputReferencePolyData(p1)
pdf.SetInputComparePolyData(p2)
pdf.Update()
pdf.GetNthPercentileHausdorffDistance(0)

```

As I am using it, Slicer crashes after “pdf.SetInputReferencePolyData(p1)."

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 16, 2020, 7:36pm UTC](https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646/2 "2020-11-16T19:36:13Z")

</div>

Segment comparison is for comparing segments, so it assumes that segments are similar, therefore it computes maximum distance.

If you want to compute maximum Hausdorff distance: The methods you found are for low-level manipulations only. Instead, I would recommend to add a new [vtkMRMLSegmentComparisonNode](https://github.com/SlicerRt/SlicerRT/blob/master/SegmentComparison/Logic/vtkMRMLSegmentComparisonNode.h) to the scene, set all the inputs into that node, and call `slicer.modules.segmentcomparison.logic().ComputeDiceStatistics(mySegmentComparisonNode)` to compute the results.

If you want to compute minimum distance (e.g., asses safety margin) then you can use ModelToModelDistance extension for that. You can get computed distance values from the model by using [slicer.util.arrayFromModelPointData](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromModelPointData) (you can then get the minimum value, a histogram of the distance distribution, etc. using numpy).

---

<div class="post-metadata">

**Author:** ![ahopf](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@ahopf](https://discourse.slicer.org/u/ahopf)\
**Post date:** [November 17, 2020, 9:41pm UTC](https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646/3 "2020-11-17T21:41:02Z")

</div>

Thank you, Dr. Lasso. I wonder if you can help me to understand how to properly use slicer.util.arrayFromModelPointData and numpy to generate these values.

I assigned the model node:  
modelNode = slicer.util.getNode(‘facialnerveoutput’)  
#facialnerveoutput is the output model from the “model to model” extension

Attempted to run:  
slicer.util.arrayFromModelPointData(modelNode, ‘array1’)  
AND  
distances = slicer.util.arrayFromModelPointData (modelNode, ‘Array1’)

In both cases, using ‘modelNode’ or the assigned ‘facialnerveoutput’ node, I receive this error:  
Traceback (most recent call last):  
File “”, line 1, in   
File “C:\Program Files\Slicer 4.10.2\bin\Python\slicer\util.py”, line 855, in arrayFromModelPointData  
narray = vtk.util.numpy\_support.vtk\_to\_numpy(arrayVtk)  
File “C:\Program Files\Slicer 4.10.2\bin\Lib\site-packages\vtkmodules\util\numpy\_support.py”, line 216, in vtk\_to\_numpy  
typ = vtk\_array.GetDataType()  
AttributeError: ‘NoneType’ object has no attribute ‘GetDataType’

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 19, 2020, 3:14am UTC](https://discourse.slicer.org/t/minimum-distance-between-segmentations/14646/4 "2020-11-19T03:14:04Z")

</div>

The distance array name is not `Array1`. Latest Slicer Preview Release provides more meaningful error message if you give incorrect input to `arrayFromModelPointData`.
