Skip to content

Commit

Permalink
allow creating labeled surface from label images
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Apr 12, 2023
1 parent 389e46e commit 1b046b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 14 additions & 2 deletions napari_process_points_and_surfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,15 @@ def invert_faces(surface: "napari.types.SurfaceData", viewer: "napari.Viewer" =

@register_function(menu="Surfaces > Create surface from all labels (marching cubes, scikit-image, nppas)")
@time_slicer
def all_labels_to_surface(labels: "napari.types.LabelsData", viewer: "napari.Viewer" = None) -> "napari.types.SurfaceData":
def all_labels_to_surface(labels: "napari.types.LabelsData", add_label_id_as_value:bool = False, viewer: "napari.Viewer" = None) -> "napari.types.SurfaceData":
"""
Turn a set of labels into a surface using the marching cubes algorithm
Parameters
----------
labels_data:napari.types.LabelsData
add_label_id_as_value: bool, optional
if True, will give the same label value to the surface vertices it had in the label image
"""
import vedo
from skimage.measure import marching_cubes
Expand All @@ -281,15 +283,25 @@ def all_labels_to_surface(labels: "napari.types.LabelsData", viewer: "napari.Vie

# Create a surface for every label
mesh_list = []
all_values = []
for label in np.unique(labels)[:-1]:
print("label", label)
verts, faces, normals, values = marching_cubes(labels==label)
if add_label_id_as_value:
all_values = all_values + (np.ones_like(verts[:,0]) * label).tolist()
print("unique labels:", np.unique(all_values))
mesh = vedo.mesh.Mesh((verts, faces))
mesh_list.append(mesh)

# merge the meshes; label is stored in `mesh.pointdata['OriginalMeshID']`
mesh = vedo.merge(mesh_list, flag=True)

return to_napari_surface_data(mesh)
surface = to_napari_surface_data(mesh)

if add_label_id_as_value:
surface = set_vertex_values(surface, all_values)

return surface
#(mesh.points(), np.asarray(mesh.faces()), mesh.pointdata['OriginalMeshID'])

# alias
Expand Down
2 changes: 2 additions & 0 deletions napari_process_points_and_surfaces/_quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ def set_vertex_values(surface: "napari.types.SurfaceData", values) -> "napari.ty
"""
from ._vedo import SurfaceTuple

values = np.asarray(values)

num_vertices = len(surface[0])
num_values = len(values)
if num_vertices != num_values:
Expand Down
2 changes: 2 additions & 0 deletions napari_process_points_and_surfaces/_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_something():
labels = label(nuclei > 20000)

surface = all_labels_to_surface(labels)
surface = all_labels_to_surface(labels, add_label_id_as_value=False)
surface = all_labels_to_surface(labels, add_label_id_as_value=True)
surface = label_to_surface(labels, 3)

surface = largest_label_to_surface(labels)
Expand Down

0 comments on commit 1b046b8

Please sign in to comment.