|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
1 | 3 | """Functions to work with ImageJ label images.""" |
2 | 4 |
|
3 | | -from ij import IJ, ImagePlus, Prefs |
| 5 | +from ij import IJ, ImagePlus, Prefs, ImageStack |
4 | 6 | from ij.plugin import Duplicator, ImageCalculator |
5 | 7 | from ij.plugin.filter import ThresholdToSelection |
6 | 8 | from ij.process import FloatProcessor, ImageProcessor |
@@ -146,39 +148,105 @@ def measure_objects_size_shape_2d(label_image): |
146 | 148 | def binary_to_label(imp, title, min_thresh=1, min_vol=None, max_vol=None): |
147 | 149 | """Segment a binary image to get a label image (2D/3D). |
148 | 150 |
|
149 | | - Works on: 2D and 3D binary data. |
| 151 | + Works on both 2D and 3D binary data. |
150 | 152 |
|
151 | 153 | Parameters |
152 | 154 | ---------- |
153 | | - imp : ImagePlus |
| 155 | + imp : ij.ImagePlus |
154 | 156 | Binary 3D stack or 2D image. |
155 | 157 | title : str |
156 | 158 | Title of the new image. |
157 | 159 | 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. |
160 | 161 | min_vol : float, optional |
161 | 162 | Volume under which to exclude objects, by default None. |
162 | 163 | max_vol : float, optional |
163 | 164 | Volume above which to exclude objects, by default None. |
164 | 165 |
|
165 | 166 | Returns |
166 | 167 | ------- |
167 | | - ImagePlus |
| 168 | + ij.ImagePlus |
168 | 169 | Segmented labeled ImagePlus. |
169 | 170 | """ |
| 171 | + # Get the calibration of the input ImagePlus |
170 | 172 | cal = imp.getCalibration() |
| 173 | + |
| 174 | + # Wrap the ImagePlus in an ImageHandler |
171 | 175 | img = ImageHandler.wrap(imp) |
| 176 | + |
| 177 | + # Threshold the image using the specified threshold value |
172 | 178 | img = img.threshold(min_thresh, False, False) |
173 | 179 |
|
| 180 | + # Create an ImageLabeller instance |
174 | 181 | labeler = ImageLabeller() |
| 182 | + |
| 183 | + # Set the minimum size for labeling if provided |
175 | 184 | if min_vol: |
176 | 185 | labeler.setMinSize(min_vol) |
| 186 | + |
| 187 | + # Set the maximum size for labeling if provided |
177 | 188 | if max_vol: |
178 | 189 | labeler.setMaxSize(max_vol) |
179 | 190 |
|
| 191 | + # Get the labeled image |
180 | 192 | seg = labeler.getLabels(img) |
| 193 | + |
| 194 | + # Set the scale of the labeled image |
181 | 195 | seg.setScale(cal.pixelWidth, cal.pixelDepth, cal.getUnits()) |
| 196 | + |
| 197 | + # Set the title of the labeled image |
182 | 198 | seg.setTitle(title) |
183 | 199 |
|
| 200 | + # Return the segmented labeled ImagePlus |
184 | 201 | 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