Skip to content

Commit d332c8f

Browse files
authored
Merge pull request #29 from imcf/labelimage
Merge changes in labelimage branch
2 parents 78791c6 + 331840b commit d332c8f

1 file changed

Lines changed: 74 additions & 6 deletions

File tree

src/imcflibs/imagej/labelimage.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# -*- coding: utf-8 -*-
2+
13
"""Functions to work with ImageJ label images."""
24

3-
from ij import IJ, ImagePlus, Prefs
5+
from ij import IJ, ImagePlus, Prefs, ImageStack
46
from ij.plugin import Duplicator, ImageCalculator
57
from ij.plugin.filter import ThresholdToSelection
68
from ij.process import FloatProcessor, ImageProcessor
@@ -146,39 +148,105 @@ def measure_objects_size_shape_2d(label_image):
146148
def binary_to_label(imp, title, min_thresh=1, min_vol=None, max_vol=None):
147149
"""Segment a binary image to get a label image (2D/3D).
148150
149-
Works on: 2D and 3D binary data.
151+
Works on both 2D and 3D binary data.
150152
151153
Parameters
152154
----------
153-
imp : ImagePlus
155+
imp : ij.ImagePlus
154156
Binary 3D stack or 2D image.
155157
title : str
156158
Title of the new image.
157159
min_thresh : int, optional
158-
Threshold to do segmentation, also allows for label filtering, by
159-
default 1.
160+
Threshold to do segmentation, also allows for label filtering, by default 1.
160161
min_vol : float, optional
161162
Volume under which to exclude objects, by default None.
162163
max_vol : float, optional
163164
Volume above which to exclude objects, by default None.
164165
165166
Returns
166167
-------
167-
ImagePlus
168+
ij.ImagePlus
168169
Segmented labeled ImagePlus.
169170
"""
171+
# Get the calibration of the input ImagePlus
170172
cal = imp.getCalibration()
173+
174+
# Wrap the ImagePlus in an ImageHandler
171175
img = ImageHandler.wrap(imp)
176+
177+
# Threshold the image using the specified threshold value
172178
img = img.threshold(min_thresh, False, False)
173179

180+
# Create an ImageLabeller instance
174181
labeler = ImageLabeller()
182+
183+
# Set the minimum size for labeling if provided
175184
if min_vol:
176185
labeler.setMinSize(min_vol)
186+
187+
# Set the maximum size for labeling if provided
177188
if max_vol:
178189
labeler.setMaxSize(max_vol)
179190

191+
# Get the labeled image
180192
seg = labeler.getLabels(img)
193+
194+
# Set the scale of the labeled image
181195
seg.setScale(cal.pixelWidth, cal.pixelDepth, cal.getUnits())
196+
197+
# Set the title of the labeled image
182198
seg.setTitle(title)
183199

200+
# Return the segmented labeled ImagePlus
184201
return seg.getImagePlus()
202+
203+
204+
def dilate_labels_2d(imp, dilation_radius):
205+
"""Dilate each label in the given ImagePlus using the specified dilation radius.
206+
207+
This method will use a 2D dilation to be applied to each slice of the ImagePlus
208+
and return a new stack.
209+
210+
Parameters
211+
----------
212+
imp : ij.ImagePlus
213+
Input ImagePlus with the labels to dilate
214+
dilation_radius : int
215+
Number of pixels to dilate each label
216+
217+
Returns
218+
-------
219+
ij.ImagePlus
220+
New ImagePlus with the dilated labels
221+
"""
222+
223+
# Create a list of the dilated labels
224+
dilated_labels_list = []
225+
226+
# Iterate over each slice of the input ImagePlus
227+
for i in range(1, imp.getNSlices() + 1):
228+
# Duplicate the current slice
229+
current_imp = Duplicator().run(imp, 1, 1, i, imp.getNSlices(), 1, 1)
230+
231+
# Perform a dilation of the labels in the current slice
232+
IJ.run(
233+
current_imp,
234+
"Label Morphological Filters",
235+
"operation=Dilation radius=" + str(dilation_radius) + " from_any_label",
236+
)
237+
238+
# Get the dilated labels
239+
dilated_labels_imp = IJ.getImage()
240+
241+
# Hide the dilated labels to avoid visual clutter
242+
dilated_labels_imp.hide()
243+
244+
# Append the dilated labels to the list
245+
dilated_labels_list.append(dilated_labels_imp)
246+
247+
# Create a new ImagePlus with the dilated labels
248+
dilated_labels_imp = ImagePlus(
249+
"Dilated labels", ImageStack().create(dilated_labels_list)
250+
)
251+
252+
return dilated_labels_imp

0 commit comments

Comments
 (0)