# Extracting Point Clouds

**URL:** <https://discourse.slicer.org/t/extracting-point-clouds/37548>\
**Category:** Support\
**Tags:** python\
**Created:** [July 24, 2024, 8:01pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548 "2024-07-24T20:01:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jessica\_de\_Kort](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jessica_de_kort/32/70344_2.png) [@Jessica\_de\_Kort](https://discourse.slicer.org/u/Jessica_de_Kort)\
**Post date:** [July 24, 2024, 8:01pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/1 "2024-07-24T20:01:04Z")

</div>

Hello, I have a custom-made module that creates catheters inside of patients from passing through a 3D U-Net to produce a point cloud. The point cloud passes through curve fitting to produce the final predictions. I want to take a look at the point cloud immediately after the U-Net, before the curve fitting. Does anyone have any suggestions?

I have tried this snippet of code:

segmentationNode=getNode(‘Segmentation’)  
segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)  
segmentPolyData=segmentationNode.GetClosedSurfaceInternalRepresentation(segmentId)  
import vtk.util.numpy\_support  
pointData = segmentPolyData.GetPoints().GetData()  
pointCoordinates = vtk.util.numpy\_support.vtk\_to\_numpy(pointData)

The problem with this code is that it only gives a point cloud for the final catheter after the curve fitting. I want it to include the points that were excluded.

---

<div class="post-metadata">

**Author:** ![JASON](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jason/32/13419_2.png) [@JASON](https://discourse.slicer.org/u/JASON)\
**Post date:** [July 24, 2024, 9:41pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/2 "2024-07-24T21:41:44Z")

</div>

@Jessica_de_Kort I’m not 100% sure this will work, but you may try turning off smoothing and recreating the ClosedSurface Representation before getting the points

```auto
segNode.GetSegmentation().SetConversionParameter('Smoothing factor',str(0))
segNode.RemoveClosedSurfaceRepresentation()
segNode.CreateClosedSurfaceRepresentation()

```

---

<div class="post-metadata">

**Author:** ![Jessica\_de\_Kort](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jessica_de_kort/32/70344_2.png) [@Jessica\_de\_Kort](https://discourse.slicer.org/u/Jessica_de_Kort)\
**Post date:** [July 25, 2024, 7:08pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/3 "2024-07-25T19:08:29Z")

</div>

I do not have segmentation nodes before I run the module which is where I run into my issue with that snippet of code. I keep running into this error:

Traceback (most recent call last):  
File “”, line 1, in   
NameError: name ‘segmentationNode’ is not defined

Any suggestions?

---

<div class="post-metadata">

**Author:** ![JASON](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jason/32/13419_2.png) [@JASON](https://discourse.slicer.org/u/JASON)\
**Post date:** [July 25, 2024, 7:20pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/4 "2024-07-25T19:20:47Z")

</div>

Your code assumes there’s a segmentation node in the scene named ‘ **Segmentation** ’, so it will fail if it doesn’t find a node with this name.

Try this, but replace the first line with the name of the node in your scene.

```auto
segmentationName = 'Your Segmentation Name'

segmentationNode=getNode(segmentationName)
segmentationNode.GetSegmentation().SetConversionParameter('Smoothing factor',str(0))
segmentationNode.RemoveClosedSurfaceRepresentation()
segmentationNode.CreateClosedSurfaceRepresentation()
segmentId = segmentationNode.GetSegmentation().GetNthSegmentID(0)
segmentPolyData=segmentationNode.GetClosedSurfaceInternalRepresentation(segmentId)
import vtk.util.numpy_support
pointData = segmentPolyData.GetPoints().GetData()
pointCoordinates = vtk.util.numpy_support.vtk_to_numpy(pointData)

```

---

<div class="post-metadata">

**Author:** ![Jessica\_de\_Kort](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jessica_de_kort/32/70344_2.png) [@Jessica\_de\_Kort](https://discourse.slicer.org/u/Jessica_de_Kort)\
**Post date:** [July 29, 2024, 4:43pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/5 "2024-07-29T16:43:22Z")

</div>

Thank you for your suggestion. I want to get the point clouds before any segmentations have been made which it does not look like that code does. From my understanding, not all of the points in the cloud are present once the predicted catheter has been made. Lines of best fit are made to represent the catheters and the rest are disregarded. I am still unsure on how to view all of the points that have been disregarded.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [July 29, 2024, 6:05pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/6 "2024-07-29T18:05:32Z")

</div>

I think we’d need to know a little more about the software pipeline your data is passing through to be able to help. What format is the point cloud data in before it is suppled to whatever smoothing routine you apply? If it is vtkPolyData, you can probably do the equivalent of the last few lines of the above code, just starting from the vtkPolyData rather than from a segmentation. If it is in some other format, then what you need to do depends on what format it is in.

---

<div class="post-metadata">

**Author:** ![Jessica\_de\_Kort](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jessica_de_kort/32/70344_2.png) [@Jessica\_de\_Kort](https://discourse.slicer.org/u/Jessica_de_Kort)\
**Post date:** [July 29, 2024, 6:27pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/7 "2024-07-29T18:27:00Z")

</div>

It looks like they are vtkMRMLScalarVolumeNode.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [July 29, 2024, 7:00pm UTC](https://discourse.slicer.org/t/extracting-point-clouds/37548/8 "2024-07-29T19:00:39Z")

</div>

vtkMRMLScalarVolumeNode is the format used in Slicer for holding image volume data (i.e. voxels). This might be something like a binary mask holding thresholded classified results from the UNet, or it might be a range of values representing probabilities or likelihoods which are to be turned into classifications by thresholding later. Either way, Slicer is an excellent tool for inspecting image volumes to try to understand what it is you are looking at. If you just modify the code you are using to be interrupted before the smoothing step, you will likely find the image you want to inspect already in the Slicer scene. If you don’t you can add it to the scene in your code using `slicer.mrmlScene.AddNode(yourNodeHere)`
