diff --git a/napari_process_points_and_surfaces/__init__.py b/napari_process_points_and_surfaces/__init__.py index f97f276..d8994e4 100644 --- a/napari_process_points_and_surfaces/__init__.py +++ b/napari_process_points_and_surfaces/__init__.py @@ -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 @@ -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 diff --git a/napari_process_points_and_surfaces/_quantification.py b/napari_process_points_and_surfaces/_quantification.py index 53f21fc..61e98f1 100644 --- a/napari_process_points_and_surfaces/_quantification.py +++ b/napari_process_points_and_surfaces/_quantification.py @@ -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: diff --git a/napari_process_points_and_surfaces/_tests/test_function.py b/napari_process_points_and_surfaces/_tests/test_function.py index 137a0fb..b72ad58 100644 --- a/napari_process_points_and_surfaces/_tests/test_function.py +++ b/napari_process_points_and_surfaces/_tests/test_function.py @@ -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)