Skip to content

Commit 7c9bf87

Browse files
authored
Merge pull request #360 from thewtex/label-image-sk-image
Label image from scikit-image
2 parents 2030d43 + 04b13c0 commit 7c9bf87

File tree

3 files changed

+60
-102
lines changed

3 files changed

+60
-102
lines changed

examples/LabelImages.ipynb

Lines changed: 48 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
"# Install dependencies for this example\n",
1010
"# Note: This does not include itkwidgets, itself\n",
1111
"import sys\n",
12-
"!{sys.executable} -m pip install -U itk itk-totalvariation"
12+
"!{sys.executable} -m pip install -U itk scikit-image"
1313
]
1414
},
1515
{
1616
"cell_type": "code",
17-
"execution_count": 5,
17+
"execution_count": null,
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
@@ -23,14 +23,15 @@
2323
"\n",
2424
"import itk\n",
2525
"import numpy as np\n",
26+
"import skimage.io, skimage.filters, skimage.measure\n",
2627
"\n",
2728
"from itkwidgets import view\n",
2829
"import itkwidgets"
2930
]
3031
},
3132
{
3233
"cell_type": "code",
33-
"execution_count": 6,
34+
"execution_count": null,
3435
"metadata": {},
3536
"outputs": [],
3637
"source": [
@@ -44,121 +45,47 @@
4445
},
4546
{
4647
"cell_type": "code",
47-
"execution_count": 7,
48+
"execution_count": null,
4849
"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-
],
50+
"outputs": [],
6551
"source": [
66-
"image = itk.imread(file_name, itk.F)\n",
52+
"image = itk.imread(file_name)\n",
6753
"\n",
6854
"view(image, cmap=itkwidgets.cm.BrBG, annotations=False, vmax=800, ui_collapsed=True)"
6955
]
7056
},
7157
{
7258
"cell_type": "code",
73-
"execution_count": 17,
59+
"execution_count": null,
7460
"metadata": {},
7561
"outputs": [],
7662
"source": [
7763
"# Segment the cells\n",
78-
"smoothed = itk.prox_tv_image_filter(image, weights=50, maximum_number_of_iterations=3)\n",
64+
"smoothed = itk.smoothing_recursive_gaussian_image_filter(image)\n",
7965
"\n",
80-
"LabelMapType = itk.Image[itk.UC, 3]\n",
66+
"thresholded = itk.moments_threshold_image_filter(smoothed,\n",
67+
" inside_value=0,\n",
68+
" outside_value=1)\n",
8169
"\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_image=<itk.itkIm…"
104-
]
105-
},
106-
"metadata": {},
107-
"output_type": "display_data"
108-
}
109-
],
110-
"source": [
111-
"view(label_image=thresholded)"
70+
"connected_components = itk.connected_component_image_filter(thresholded)"
11271
]
11372
},
11473
{
11574
"cell_type": "code",
116-
"execution_count": 20,
75+
"execution_count": null,
11776
"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_image=<itk.itkIm…"
128-
]
129-
},
130-
"metadata": {},
131-
"output_type": "display_data"
132-
}
133-
],
77+
"outputs": [],
13478
"source": [
135-
"connected_components = itk.connected_component_image_filter(thresholded)\n",
136-
"\n",
13779
"view(label_image=connected_components)"
13880
]
13981
},
14082
{
14183
"cell_type": "code",
142-
"execution_count": 22,
84+
"execution_count": null,
14385
"metadata": {
14486
"scrolled": true
14587
},
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_image_names=[(0, 'Background'), (1, 'Firs…"
156-
]
157-
},
158-
"metadata": {},
159-
"output_type": "display_data"
160-
}
161-
],
88+
"outputs": [],
16289
"source": [
16390
"# We can combine the label map with the intensity image, add names for the labels, etc.\n",
16491
"names = [(0, 'Background'), (1, 'First cell'), (2, 'Second cell')]\n",
@@ -182,7 +109,7 @@
182109
},
183110
{
184111
"cell_type": "code",
185-
"execution_count": 27,
112+
"execution_count": null,
186113
"metadata": {},
187114
"outputs": [],
188115
"source": [
@@ -197,15 +124,43 @@
197124
"cell_type": "markdown",
198125
"metadata": {},
199126
"source": [
200-
"We can also combine the intensity images, weighted and blended with the label maps, with iso-surface geometry."
127+
"The images and labels can, of course, also come from *scikit-image*."
201128
]
202129
},
203130
{
204131
"cell_type": "code",
205132
"execution_count": null,
206133
"metadata": {},
207134
"outputs": [],
208-
"source": []
135+
"source": [
136+
"image = skimage.io.imread(file_name)\n",
137+
"\n",
138+
"view(image, cmap=itkwidgets.cm.BrBG, annotations=False, vmax=800, ui_collapsed=True)"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"# Segment the cells\n",
148+
"smoothed = skimage.filters.gaussian(image)\n",
149+
"\n",
150+
"threshold = skimage.filters.threshold_isodata(smoothed)\n",
151+
"thresholded = smoothed > threshold\n",
152+
"\n",
153+
"connected_components = skimage.measure.label(thresholded)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": [
162+
"view(label_image=connected_components)"
163+
]
209164
}
210165
],
211166
"metadata": {

itkwidgets/_transform_types.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,16 @@ def to_itk_image(image_like):
237237

238238
if is_arraylike(image_like):
239239
array = np.asarray(image_like)
240-
case_use_view = array.flags['OWNDATA']
240+
can_use_view = array.flags['OWNDATA']
241241
if have_dask and isinstance(image_like, dask.array.core.Array):
242-
case_use_view = False
242+
can_use_view = False
243243
array = np.ascontiguousarray(array)
244-
if case_use_view:
244+
# JavaScript does not support 64-bit integers
245+
if array.dtype == np.int64:
246+
array = array.astype(np.float32)
247+
elif array.dtype == np.uint64:
248+
array = array.astype(np.float32)
249+
if can_use_view:
245250
image_from_array = itk.image_view_from_array(array)
246251
else:
247252
image_from_array = itk.image_from_array(array)

itkwidgets/widget_viewer.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,6 @@ def __init__(self, **kwargs): # noqa: C901
403403
for dim in range(dimension):
404404
if size[dim] > self.size_limit_3d[dim]:
405405
self._downsampling = True
406-
if self._downsampling and self.image:
407-
self.extractor = itk.ExtractImageFilter.New(self.image)
408-
self.shrinker = itk.BinShrinkImageFilter.New(self.extractor)
409-
if self._downsampling and self.label_image:
410-
self.label_image_extractor = itk.ExtractImageFilter.New(self.label_image)
411-
self.label_image_shrinker = itk.ShrinkImageFilter.New(self.label_image_extractor)
412406
self._update_rendered_image()
413407
if self._downsampling:
414408
self.observe(self._on_roi_changed, ['roi'])
@@ -488,8 +482,12 @@ def f():
488482
self.size_limit_3d, dimension, size)
489483
self._scale_factors = np.array(scale_factors, dtype=np.uint8)
490484
if self.image:
485+
self.extractor = itk.ExtractImageFilter.New(self.image)
486+
self.shrinker = itk.ShrinkImageFilter.New(self.extractor)
491487
self.shrinker.SetShrinkFactors(scale_factors[:dimension])
492488
if self.label_image:
489+
self.label_image_extractor = itk.ExtractImageFilter.New(self.label_image)
490+
self.label_image_shrinker = itk.ShrinkImageFilter.New(self.label_image_extractor)
493491
self.label_image_shrinker.SetShrinkFactors(scale_factors[:dimension])
494492

495493
region = itk.ImageRegion[dimension]()

0 commit comments

Comments
 (0)