# Fill holes in mesh

**URL:** <https://discourse.slicer.org/t/fill-holes-in-mesh/13719>\
**Category:** Support\
**Created:** [September 27, 2020, 3:33pm UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719 "2020-09-27T15:33:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![loubna](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@loubna](https://discourse.slicer.org/u/loubna)\
**Post date:** [September 27, 2020, 3:33pm UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719/1 "2020-09-27T15:33:01Z")

</div>

Hi;  
I have a 3D image (vtkImageData with holes) on which I applied Marching cubes and I get a 3d surface with small holes (see attached figure).

![mesh](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/0/005bf892cfe88406749d0ea3d5caf3cbc2aba47e.png)

To deal with this problem, I have resampled the 3D image with sampledFactor of 0.5 before applying the marching cubes

here is the code:

```auto
		double resampledFactor =0.5;
		
		vtkSmartPointer<vtkImageReslice> resampledImage =
		vtkSmartPointer<vtkImageReslice>::New();
		resampledImage->SetInputData(imageData);
		
		int extent[6];
		extent[0] = ext[0];
		extent[1] = ext[1] /resampledFactor;
		extent[2] = ext[2];
		extent[3] = ext[3] /resampledFactor;
		extent[4] = ext[4];
		extent[5] = ext[5] / resampledFactor;
		
		resampledImage->SetOutputSpacing(0.5,0.5,0.5);
		resampledImage->SetOutputOrigin(0,0,0);
		resampledImage->SetOutputExtent(extent);
		resampledImage->SetInterpolationModeToLinear();
		resampledImage->Update();

```

The resulted surface is enhenced but it is still contains smalll holes. My question is how can I fill in holes in vtkImageData or in mesh in order to get a closed 3D surface without holes.

Thank’s in advance

---

<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:** [September 27, 2020, 7:27pm UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719/2 "2020-09-27T19:27:57Z")

</div>

Isosurface extracted from an unprocessed image is expected to be noisy. You can greatly improve quality by applying Gaussian smoothing, but that will also remove fine details. You can try non-linear filters, such as median or various anisotropic smoothing, which may preserve details while removing certain noise types. These filters (and many more) are available in “Simple filters” module.

However, most of the time you cannot extract structures by simple thresholding/isosurface extraction but you need to use various segmentation tools. You can find many of them in Segment Editor (and there are several extensions that installs provides additional ones).

---

<div class="post-metadata">

**Author:** ![Nina](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@Nina](https://discourse.slicer.org/u/Nina)\
**Post date:** [September 28, 2020, 10:43am UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719/3 "2020-09-28T10:43:26Z")

</div>

-Thank you for response.

Im trying to create the model from the 3D coordinate points that are stored in the txt file (and not a segment). I use the Marching cubes algorithm. It looks like it´s not able to link individual points, and therefore holes are created in the model.

I tried vtkImageOpenClose3D.h to close the holes but it seems do not give good results .

```cpp
        vtkSmartPointer<vtkImageOpenClose3D> openClose =
        vtkSmartPointer<vtkImageOpenClose3D>::New();
        openClose->SetInputData(imageData);
        openClose->SetOpenValue(255);
        openClose->SetCloseValue(0);
        openClose->SetKernelSize(5, 5, 5);
        //openClose->ReleaseDataFlagOff();
        openClose->GetOutput();
        openClose->GetCloseValue();
        openClose->GetOpenValue();
        openClose->Update();

```

---

<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:** [September 28, 2020, 1:30pm UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719/4 "2020-09-28T13:30:15Z")

</div>

> [@Nina](#):
>
> Im trying to create the model from the 3D coordinate points that are stored in the txt file

What software produces this text file?  
Do the coordinate correspond to a list of planar contours (coming from a DICOM RT structure set)?  
Or some software lists each individual voxel value of a binary labelmap (that would be extremely inefficient)?

---

<div class="post-metadata">

**Author:** ![Nina](https://avatars.discourse-cdn.com/v4/letter/n/e68b1a/32.png) [@Nina](https://discourse.slicer.org/u/Nina)\
**Post date:** [September 30, 2020, 12:57pm UTC](https://discourse.slicer.org/t/fill-holes-in-mesh/13719/5 "2020-09-30T12:57:39Z")

</div>

Hi Mr;

I have created points from segmentation and generated surface to do some tests. I have resapmled and used vtkImageOpenClose3D. Now it works fine (all holes are filled

thank’s
