-
Notifications
You must be signed in to change notification settings - Fork 0
/
napari_test.py
72 lines (48 loc) · 1.61 KB
/
napari_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# %%
'''Based on
https://ilovesymposia.com/2019/10/24/introducing-napari-a-fast-n-dimensional-image-viewer-in-python/
'''
# %%
import numpy as np
from skimage import data, filters
import napari
from skimage import segmentation
from skimage import morphology
import os
from dask import array as da
# %%
blobs_raw = np.stack([
data.binary_blobs(length=256, n_dim=3, volume_fraction=f)
for f in np.linspace(0.05, 0.5, 10)
])
blobs = filters.gaussian(blobs_raw, sigma=(0, 2, 2, 2))
print(blobs.shape)
(10, 256, 256, 256)
# %%
viewer = napari.view_image(blobs)
# %%
coins = data.coins()
viewer = napari.view_image(coins, name='coins')
edges = filters.sobel(coins)
edges_layer = viewer.add_image(edges, colormap='magenta', blending='additive')
pts_layer = viewer.add_points(size=5)
pts_layer.mode = 'add'
# annotate the background and all the coins, in that order
coordinates = pts_layer.data
coordinates_int = np.round(coordinates).astype(int)
markers_raw = np.zeros_like(coins)
markers_raw[tuple(coordinates_int.T)] = 1 + np.arange(len(coordinates))
# raw markers might be in a little watershed "well".
markers = morphology.dilation(markers_raw, morphology.disk(5))
segments = segmentation.watershed(edges, markers=markers)
labels_layer = viewer.add_labels(segments - 1) # make background 0
# %%
def threshold(image, t):
arr = da.from_array(image, chunks=image.shape)
return arr > t
all_thresholds = da.stack([threshold(coins, t) for t in np.arange(255)])
viewer = napari.view_image(coins, name='coins')
viewer.add_image(all_thresholds,
name='thresholded', colormap='magenta', blending='additive'
)
# %%