Skip to content

Commit e34fb09

Browse files
feat: add cross section visualization option
1 parent 4115597 commit e34fb09

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

kimimaro/utility.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ def find_objects(labels):
4040
all_slices = scipy.ndimage.find_objects(labels.T)
4141
return [ (slcs and slcs[::-1]) for slcs in all_slices ]
4242

43+
def add_property(skel, prop):
44+
needs_prop = True
45+
for skel_prop in skel.extra_attributes:
46+
if skel_prop["id"] == prop["id"]:
47+
needs_prop = False
48+
break
49+
50+
if needs_prop:
51+
skel.extra_attributes.append(prop)
52+
4353
def cross_sectional_area(
4454
all_labels:np.ndarray,
4555
skeletons:Union[Dict[int,Skeleton],List[Skeleton],Skeleton],
@@ -49,6 +59,7 @@ def cross_sectional_area(
4959
in_place:bool = False,
5060
fill_holes:bool = False,
5161
repair_contacts:bool = False,
62+
visualize_section_planes:bool = False,
5263
) -> Union[Dict[int,Skeleton],List[Skeleton],Skeleton]:
5364
"""
5465
Given a set of skeletons, find the cross sectional area
@@ -79,6 +90,9 @@ def cross_sectional_area(
7990
that have a nonzero value for
8091
skel.cross_sectional_area_contacts. This is intended
8192
to be used as a second pass after widening the image.
93+
94+
visualize_section_planes: For debugging, paint section planes
95+
and display them using microviewer.
8296
"""
8397
prop = {
8498
"id": "cross_sectional_area",
@@ -127,6 +141,10 @@ def cross_sectional_area(
127141

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

144+
cross_sections = None
145+
if visualize_section_planes:
146+
cross_sections = np.zeros(binimg.shape, dtype=np.uint32, order="F")
147+
130148
if fill_holes:
131149
binimg = fill_voids.fill(binimg, in_place=True)
132150

@@ -177,16 +195,18 @@ def cross_sectional_area(
177195
normal, anisotropy,
178196
return_contact=True,
179197
)
180-
181-
needs_prop = True
182-
for skel_prop in skel.extra_attributes:
183-
if skel_prop["id"] == "cross_sectional_area":
184-
needs_prop = False
185-
break
186-
187-
if needs_prop:
188-
skel.extra_attributes.append(prop)
189-
198+
if visualize_section_planes:
199+
img = xs3d.cross_section(
200+
binimg, vert,
201+
normal, anisotropy,
202+
)
203+
cross_sections[img > 0] = idx
204+
205+
if visualize_section_planes:
206+
import microviewer
207+
microviewer.view(cross_sections, seg=True)
208+
209+
add_property(skel, prop)
190210
skel.cross_sectional_area = areas
191211
skel.cross_sectional_area_contacts = contacts
192212

0 commit comments

Comments
 (0)