Skip to content

Commit

Permalink
feat: add cross section visualization option
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Apr 11, 2024
1 parent 4115597 commit e34fb09
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions kimimaro/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def find_objects(labels):
all_slices = scipy.ndimage.find_objects(labels.T)
return [ (slcs and slcs[::-1]) for slcs in all_slices ]

def add_property(skel, prop):
needs_prop = True
for skel_prop in skel.extra_attributes:
if skel_prop["id"] == prop["id"]:
needs_prop = False
break

if needs_prop:
skel.extra_attributes.append(prop)

def cross_sectional_area(
all_labels:np.ndarray,
skeletons:Union[Dict[int,Skeleton],List[Skeleton],Skeleton],
Expand All @@ -49,6 +59,7 @@ def cross_sectional_area(
in_place:bool = False,
fill_holes:bool = False,
repair_contacts:bool = False,
visualize_section_planes:bool = False,
) -> Union[Dict[int,Skeleton],List[Skeleton],Skeleton]:
"""
Given a set of skeletons, find the cross sectional area
Expand Down Expand Up @@ -79,6 +90,9 @@ def cross_sectional_area(
that have a nonzero value for
skel.cross_sectional_area_contacts. This is intended
to be used as a second pass after widening the image.
visualize_section_planes: For debugging, paint section planes
and display them using microviewer.
"""
prop = {
"id": "cross_sectional_area",
Expand Down Expand Up @@ -127,6 +141,10 @@ def cross_sectional_area(

binimg = np.asfortranarray(all_labels[slices] == label)

cross_sections = None
if visualize_section_planes:
cross_sections = np.zeros(binimg.shape, dtype=np.uint32, order="F")

if fill_holes:
binimg = fill_voids.fill(binimg, in_place=True)

Expand Down Expand Up @@ -177,16 +195,18 @@ def cross_sectional_area(
normal, anisotropy,
return_contact=True,
)

needs_prop = True
for skel_prop in skel.extra_attributes:
if skel_prop["id"] == "cross_sectional_area":
needs_prop = False
break

if needs_prop:
skel.extra_attributes.append(prop)

if visualize_section_planes:
img = xs3d.cross_section(
binimg, vert,
normal, anisotropy,
)
cross_sections[img > 0] = idx

if visualize_section_planes:
import microviewer
microviewer.view(cross_sections, seg=True)

add_property(skel, prop)
skel.cross_sectional_area = areas
skel.cross_sectional_area_contacts = contacts

Expand Down

0 comments on commit e34fb09

Please sign in to comment.