scikit-shape Image-based Meshing

Image-based Meshing generates triangulations conforming to the regions in a given segmented image. The triangulations can be used to simulate physics based on the image. For example, elastic deformation of material microstructures or biological tissues can be computed with a finite element solution of the elasticity equation on conforming triangulations of their segmented images.

The function triangulation_from_labels takes a labeled image (segmentation) as input, identifies the regions and their boundaries, and generates a high quality triangulation conforming to the region boundaries. The triangulation function returns a list of the boundary curves as well. The region boundaries are smoothed so that they do not follow the staircase shape of the pixel boundaries. The triangulation matches the smoothed boundaries and consists of relatively coarse triangles. All the coordinates in the returned geometry are on the unit square, NOT on the pixel coordinate system. The user can choose to scale them by image size to translate them back to image coordinates.

Download the Python code to Triangulate Regions.


import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skshape.geometry import triangulation_from_labels

image = plt.imread('Triangulation_Input.png')
plt.figure(); plt.imshow(image);
plt.title('Input labeled image regions'); plt.show()

curves, vertices, triangles = triangulation_from_labels( rgb2gray(image) )
scale = min(image.shape[:2]) - 1.0
x = scale * vertices[:,1]  # coordinates of triangle nodes
y = scale * vertices[:,0]
    
plt.figure(); plt.imshow(image);
plt.title('Triangulation of the regions'); 
plt.triplot( x, y, triangles=triangles.T, color='k' )

for c in curves:
    plt.plot( scale*c[1], scale*c[0], 'r.-', linewidth=2, markersize=6 ) 
plt.axis('image'); plt.show()
examples/Triangulation_Input.png examples/Triangulation_Output.png