I am working on a project where I need to compare an initial SVG with its skeleton and decided to work with Trimesh. The goal is to create an output image that combines the initial SVG with skeletonized paths: for the parts where the skeleton is close to the initial on a certain threshold - keep only skeletonized edges, while for the parts where the distance is over the threshold - keep only the initials.
Initial steps were easy
import trimesh
initial_edges = trimesh.load('vectorized_shapes.svg')
skeletonized_edges = initial_edges.medial_axis()
However, I encountered challenges when attempting to compare individual vertices between the initial and skeletonized paths. The approach I tried didn't work well, as vertices are not entities, and I couldn't find a suitable way to compare initial entities with the skeletonized ones.
for i, initial_vertex in enumerate(initial_edges.vertices):
min_distance = float("inf")
# Find the closest skeleton vertex for this initial vertex
for j, skeleton_vertex in enumerate(skeletonized_edges.vertices):
distance_current = np.linalg.norm(initial_vertex - skeleton_vertex)
# If the vertex is closer to inital than any one before -
if distance_current < min_distance:
min_distance = distance_current
closest_skeleton = skeleton_vertex
skeleton_index = j
# If skeleton is closer to initial than threshold - keep skeleton
if min_distance <= threshold:
vertex = closest_skeleton
# If skeleton is not closer to initial than threshold - keep initial
else:
vertex = initial_vertex
final_vertices.append(vertex)

It didn't work very well, since vertices are not entities and I couldn't also find a way to compare initial entities with the skeletonized ones.
Additionally, when I attempted to export the skeletonized edges using the following code:
initial_edges.export(file_obj="svg.svg"))
The output in Jupyter and the exported file differed significantly.

My questions are:
- Is it possible to effectively compare initial paths with skeletonized paths in Trimesh?
- If not, are there alternative libraries or methods that can be recommended for this task?
- What parameters should be passed to the export function to ensure the output image matches the display in Jupyter using the
.show()method?
Any insights or suggestions would be greatly appreciated!
Managed to save it as svg from matplotlib the following way:
Will continue comparing skeleton with initial image without trimesh