-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy paththreshold_contour.py
70 lines (59 loc) · 1.77 KB
/
threshold_contour.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
"""
An example demonstrating adding traces.
This shows a volume with contours overlaid on top. The `extra_traces`
property is used to add scatter traces that represent the contours.
"""
import plotly
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
from dash_slicer import VolumeSlicer
import imageio
from skimage import measure
app = dash.Dash(__name__, update_title=None)
server = app.server
vol = imageio.volread("imageio:stent.npz")
mi, ma = vol.min(), vol.max()
slicer = VolumeSlicer(app, vol, clim=(0, 800))
app.layout = html.Div(
[
slicer.graph,
slicer.slider,
dcc.Slider(
id="level-slider",
min=mi,
max=ma,
step=1,
value=mi + 0.2 * (ma - mi),
),
*slicer.stores,
]
)
@app.callback(
Output(slicer.extra_traces.id, "data"),
[Input("level-slider", "value"), Input(slicer.state.id, "data")],
)
def apply_levels(level, state):
if not state:
return dash.no_update
slice = vol[state["index"]]
contours = measure.find_contours(slice, level)
# Create a trace for each contour, each a different color
traces = []
for i, contour in enumerate(contours):
colors = plotly.colors.qualitative.D3
color = colors[i % len(colors)]
traces.append(
{
"type": "scatter",
"mode": "lines",
"line": {"color": color, "width": 3},
"x": contour[:, 1],
"y": contour[:, 0],
}
)
return traces
if __name__ == "__main__":
# Note: dev_tools_props_check negatively affects the performance of VolumeSlicer
app.run_server(debug=True, dev_tools_props_check=False)