Here I will explain how to perform 3D Thinning with Wolfram Language. Though I will admit there’s a lot of room for improvement. In this post I’ll refer to the medial surface of a 3D object O as the surface comprising the points equidistant from at least 2 points on ∂O the medial axis of a 3D object as the singular (or non-manifold) edges of the medial surface. The main idea of this answer is that for a sufficiently dense sampling of points on the boundary of a volume, the vertices, edges, and faces of the Voronoi diagram gives an approximation to the medial surface. Since OP is interested in lines, I will use this to find the medial axis. — Since the medial surface is geometrically unstable, this answer might be a dead end. — First here’s the code to find Voronoi faces for a set of points:
The following function takes in a binary Image3D object, converts it into a BoundaryMeshRegion, approximates the medial surface, finds the approximate medial axis, then converts back into an image. To approximate the medial surface, I simply take the Voronoi faces completely contained in the object.
We can see there are some heuristic parameters. I haven’t found a way to automatically determine appropriate values. Note too that this routine can be slow. I think there’s a lot of room for improvement. Some examples:
Based on papers of Amenta and Attali, a better approximation can be found, but we need to be able to find intersections of spheres efficiently.
One approach is to apply a topology preserving morphological thinning method. I’ll implement an algorithm by Lee. The main idea is to look at every 3x3x3 cube in the image and to decide which pixels are valid to delete. For the most part I stuck to his approach, except I did not implement the function Octree_Labeling. It’s wildly recursive and Compile does not allow for recursion. Instead I took a transitive closure approach. The compiled routines are quite messy looking since a lot of things are baked into precomputed lookup tables:
The main routine:
The second argument represents how many iterations of thinning to perform. The default is Infinity: