Skip to content

Commit 4e19b6e

Browse files
Update mito segmentation
1 parent a051d85 commit 4e19b6e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

synaptic_reconstruction/inference/mitochondria.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,43 @@ def _run_segmentation(
1414
block_shape=(128, 256, 256),
1515
halo=(48, 48, 48)
1616
):
17-
18-
# get the segmentation via seeded watershed
1917
t0 = time.time()
20-
seeds = parallel.label((foreground - boundaries) > 0.5, block_shape=block_shape, verbose=verbose)
18+
boundary_threshold = 0.25
19+
dist = parallel.distance_transform(
20+
boundaries < boundary_threshold, halo=halo, verbose=verbose, block_shape=block_shape
21+
)
2122
if verbose:
22-
print("Compute connected components in", time.time() - t0, "s")
23+
print("Compute distance transform in", time.time() - t0, "s")
2324

25+
# Get the segmentation via seeded watershed.
2426
t0 = time.time()
25-
dist = parallel.distance_transform(seeds == 0, halo=halo, verbose=verbose, block_shape=block_shape)
27+
seed_distance = 6
28+
seeds = np.logical_and(foreground > 0.5, dist > seed_distance)
29+
seeds = parallel.label(seeds, block_shape=block_shape, verbose=verbose)
2630
if verbose:
27-
print("Compute distance transform in", time.time() - t0, "s")
31+
print("Compute connected components in", time.time() - t0, "s")
32+
33+
# import napari
34+
# v = napari.Viewer()
35+
# v.add_image(boundaries)
36+
# v.add_image(dist)
37+
# v.add_labels(seeds)
38+
# napari.run()
2839

2940
t0 = time.time()
41+
hmap = boundaries + ((dist.max() - dist) / dist.max())
3042
mask = (foreground + boundaries) > 0.5
43+
3144
seg = np.zeros_like(seeds)
3245
seg = parallel.seeded_watershed(
33-
dist, seeds, block_shape=block_shape,
46+
hmap, seeds, block_shape=block_shape,
3447
out=seg, mask=mask, verbose=verbose, halo=halo,
3548
)
3649
if verbose:
3750
print("Compute watershed in", time.time() - t0, "s")
3851

3952
seg = apply_size_filter(seg, min_size, verbose=verbose, block_shape=block_shape)
40-
seg = _postprocess_seg_3d(seg)
53+
seg = _postprocess_seg_3d(seg, area_threshold=5000)
4154
return seg
4255

4356

synaptic_reconstruction/inference/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ def apply_size_filter(
472472
return segmentation
473473

474474

475-
def _postprocess_seg_3d(seg):
475+
def _postprocess_seg_3d(seg, area_threshold=1000, iterations=4, iterations_3d=8):
476476
# Structure lement for 2d dilation in 3d.
477477
structure_element = np.ones((3, 3)) # 3x3 structure for XY plane
478478
structure_3d = np.zeros((1, 3, 3)) # Only applied in the XY plane
@@ -485,9 +485,9 @@ def _postprocess_seg_3d(seg):
485485
mask = seg[bb] == prop.label
486486

487487
# Fill small holes and apply closing.
488-
mask = remove_small_holes(mask, area_threshold=1000)
489-
mask = np.logical_or(binary_closing(mask, iterations=4), mask)
490-
mask = np.logical_or(binary_closing(mask, iterations=8, structure=structure_3d), mask)
488+
mask = remove_small_holes(mask, area_threshold=area_threshold)
489+
mask = np.logical_or(binary_closing(mask, iterations=iterations), mask)
490+
mask = np.logical_or(binary_closing(mask, iterations=iterations_3d, structure=structure_3d), mask)
491491
seg[bb][mask] = prop.label
492492

493493
return seg

0 commit comments

Comments
 (0)