Skip to content

Commit 985f9ae

Browse files
committed
DOC: Update LabelMaps example
1 parent a21e4b7 commit 985f9ae

File tree

4 files changed

+235
-266
lines changed

4 files changed

+235
-266
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Interactive Jupyter_ widgets to visualize images, point sets, and meshes.
7878
- Exquisite volume rendering
7979
- Tri-plane volume slicing
8080
- Innovative, powerful opacity transfer function / window / level widget
81+
- Label map segmentation 2D and 3D rendering
8182
- Anisotropic voxel spacing supported
8283
- Image line profile widget
8384
- Compare images widget
@@ -108,6 +109,7 @@ Data types:
108109

109110
- `Binder: 2D ITK Images <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples%2F2DImage.ipynb>`_
110111
- `Binder: 3D ITK Images <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples%2F3DImage.ipynb>`_
112+
- `Binder: 3D Label Maps <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples%2FLabelMaps.ipynb>`_
111113
- `Binder: Dask Array images <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples/DaskArray.ipynb>`_
112114
- `Binder: Large volumes <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples/LargeVolumes.ipynb>`_
113115
- `Binder: NumPy array images (processed with SciPy) <https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?urlpath=lab/tree/examples/NumPyArrayImage.ipynb>`_
@@ -262,6 +264,7 @@ After installation, try the following examples that demonstrate how to visualize
262264

263265
- `2D ITK Images <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/2DImage.ipynb>`_
264266
- `3D ITK Images <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/3DImage.ipynb>`_
267+
- `3D Label maps <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/LabelMaps.ipynb>`_
265268
- `Dask Array images <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/DaskArray.ipynb>`_
266269
- `Large volumes <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/LargeVolumes.ipynb>`_
267270
- `ImageJ ImgLib2 images <https://github.com/InsightSoftwareConsortium/itkwidgets/blob/master/examples/ImageJImgLib2.ipynb>`_ (requires `conda <https://conda.io/>`_ and a local `Fiji <https://fiji.sc/>`_ installation)

examples/LabelMap.ipynb

Lines changed: 0 additions & 127 deletions
This file was deleted.

examples/LabelMaps.ipynb

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# Install dependencies for this example\n",
10+
"# Note: This does not include itkwidgets, itself\n",
11+
"import sys\n",
12+
"!{sys.executable} -m pip install -U itk itk-totalvariation"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 5,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from urllib.request import urlretrieve\n",
22+
"import os\n",
23+
"\n",
24+
"import itk\n",
25+
"import numpy as np\n",
26+
"\n",
27+
"from itkwidgets import view\n",
28+
"import itkwidgets"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 6,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"# Download data\n",
38+
"# Source: https://data.broadinstitute.org/bbbc/BBBC024/\n",
39+
"file_name = 'HL50_cell_line_c00_03_extraction.tif'\n",
40+
"if not os.path.exists(file_name):\n",
41+
" url = 'https://data.kitware.com/api/v1/file/5b61f16c8d777f06857c1949/download'\n",
42+
" urlretrieve(url, file_name)"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 7,
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"data": {
52+
"application/vnd.jupyter.widget-view+json": {
53+
"model_id": "530d6f97e04e4dab86bbb00ddcc799bb",
54+
"version_major": 2,
55+
"version_minor": 0
56+
},
57+
"text/plain": [
58+
"Viewer(annotations=False, cmap=['BrBG'], geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<…"
59+
]
60+
},
61+
"metadata": {},
62+
"output_type": "display_data"
63+
}
64+
],
65+
"source": [
66+
"image = itk.imread(file_name, itk.F)\n",
67+
"\n",
68+
"view(image, cmap=itkwidgets.cm.BrBG, annotations=False, vmax=800, ui_collapsed=True)"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 17,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"# Segment the cells\n",
78+
"smoothed = itk.prox_tv_image_filter(image, weights=50, maximum_number_of_iterations=3)\n",
79+
"\n",
80+
"LabelMapType = itk.Image[itk.UC, 3]\n",
81+
"\n",
82+
"threshold_filter = itk.MomentsThresholdImageFilter[type(smoothed), LabelMapType].New(smoothed)\n",
83+
"threshold_filter.SetInsideValue(0)\n",
84+
"threshold_filter.SetOutsideValue(1)\n",
85+
"threshold_filter.Update()\n",
86+
"threshold = threshold_filter.GetThreshold()\n",
87+
"thresholded = threshold_filter.GetOutput()"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": 19,
93+
"metadata": {},
94+
"outputs": [
95+
{
96+
"data": {
97+
"application/vnd.jupyter.widget-view+json": {
98+
"model_id": "69dfe9c8df3d41a889e9c7b63f114284",
99+
"version_major": 2,
100+
"version_minor": 0
101+
},
102+
"text/plain": [
103+
"Viewer(geometries=[], gradient_opacity=0.22, interpolation=False, point_sets=[], rendered_label_map=<itk.itkIm…"
104+
]
105+
},
106+
"metadata": {},
107+
"output_type": "display_data"
108+
}
109+
],
110+
"source": [
111+
"view(label_map=thresholded)"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 20,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"application/vnd.jupyter.widget-view+json": {
122+
"model_id": "33c0cb1ad92241a49728735b67d1f83f",
123+
"version_major": 2,
124+
"version_minor": 0
125+
},
126+
"text/plain": [
127+
"Viewer(geometries=[], gradient_opacity=0.22, interpolation=False, point_sets=[], rendered_label_map=<itk.itkIm…"
128+
]
129+
},
130+
"metadata": {},
131+
"output_type": "display_data"
132+
}
133+
],
134+
"source": [
135+
"connected_components = itk.connected_component_image_filter(thresholded)\n",
136+
"\n",
137+
"view(label_map=connected_components)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 22,
143+
"metadata": {
144+
"scrolled": true
145+
},
146+
"outputs": [
147+
{
148+
"data": {
149+
"application/vnd.jupyter.widget-view+json": {
150+
"model_id": "94c8055aff1a4cb89923c78072fa305b",
151+
"version_major": 2,
152+
"version_minor": 0
153+
},
154+
"text/plain": [
155+
"Viewer(geometries=[], gradient_opacity=0.5, interpolation=False, label_map_names=[(0, 'Background'), (1, 'Firs…"
156+
]
157+
},
158+
"metadata": {},
159+
"output_type": "display_data"
160+
}
161+
],
162+
"source": [
163+
"# We can combine the label map with the intensity image, add names for the labels, etc.\n",
164+
"names = [(0, 'Background'), (1, 'First cell'), (2, 'Second cell')]\n",
165+
"viewer = view(smoothed,\n",
166+
" label_map=connected_components,\n",
167+
" label_map_names=names,\n",
168+
" label_map_blend=0.8,\n",
169+
" rotate=True,\n",
170+
" gradient_opacity=0.5,\n",
171+
" slicing_planes=True)\n",
172+
"viewer"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"Clicking on a label in a slice will toggle its weight.\n",
180+
"We can also set label weights programmically."
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 27,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"label_count = len(np.unique(connected_components))\n",
190+
"label_weights = np.ones((label_count,), dtype=np.float32)\n",
191+
"label_weights[1] = 0.1\n",
192+
"label_weights[2] = 0.2\n",
193+
"viewer.label_map_weights = label_weights"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"We can also combine the intensity images, weighted and blended with the label maps, with iso-surface geometry."
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"metadata": {},
207+
"outputs": [],
208+
"source": []
209+
}
210+
],
211+
"metadata": {
212+
"kernelspec": {
213+
"display_name": "Python 3",
214+
"language": "python",
215+
"name": "python3"
216+
},
217+
"language_info": {
218+
"codemirror_mode": {
219+
"name": "ipython",
220+
"version": 3
221+
},
222+
"file_extension": ".py",
223+
"mimetype": "text/x-python",
224+
"name": "python",
225+
"nbconvert_exporter": "python",
226+
"pygments_lexer": "ipython3",
227+
"version": "3.7.6"
228+
}
229+
},
230+
"nbformat": 4,
231+
"nbformat_minor": 4
232+
}

0 commit comments

Comments
 (0)