Smooth binary masks using `mcubes.smooth`
masterWhen extracting iso-surfaces from binary masks (segmentation results), the resulting mesh often appears jagged. mcubes.smooth can resolve this by taking a 2D or 3D binary embedding function and producing a smooth version.
mcubes.smooth creates a smooth embedding array where areas that were 0 in the binary array become negative, and areas that were 1 become positive. This preserves fine details and thin structures better than standard smoothing methods.
Workflow:
- Create/obtain your binary array.
- Apply
smoothed_array = mcubes.smooth(binary_array). - Extract the iso-surface from the smoothed array at level
0(which corresponds to the0.5level-set of the original binary array).
import numpy as np
import mcubes
x, y, z = np.mgrid[:100, :100, :100]
binary_sphere = (x - 50)**2 + (y - 50)**2 + (z - 50)**2 - 25**2 < 0
# Smooth the binary embedding
smoothed_sphere = mcubes.smooth(binary_sphere)
# Extract the 0-levelset of the smoothed array
vertices, triangles = mcubes.marching_cubes(smoothed_sphere, 0)