|
1 | 1 | from ij import IJ |
2 | 2 | from mcib3d.geom import Objects3DPopulation |
3 | | -from mcib3d.image3d import ImageHandler |
| 3 | +from mcib3d.image3d import ImageHandler, ImageLabeller |
4 | 4 |
|
5 | 5 |
|
6 | 6 | def population3d_to_imgplus(imp, population): |
@@ -54,3 +54,45 @@ def imgplus_to_population3d(imp): |
54 | 54 | """ |
55 | 55 | img = ImageHandler.wrap(imp) |
56 | 56 | return Objects3DPopulation(img) |
| 57 | + |
| 58 | + |
| 59 | +def segment_3d_image(imp, title=None, min_thresh=1, min_vol=None, max_vol=None): |
| 60 | + """Segment a 3D binary image to get a labelled stack. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + imp : ImagePlus |
| 65 | + Binary 3D stack. |
| 66 | + title : str, optional |
| 67 | + Title of the new image. |
| 68 | + min_thresh : int, optional |
| 69 | + Threshold to do segmentation, also allows for label filtering, by default 1. |
| 70 | + min_vol : int, optional |
| 71 | + Volume (voxels) under which to filter objects, by default None. |
| 72 | + max_vol : int, optional |
| 73 | + Volume above which to filter objects, by default None. |
| 74 | +
|
| 75 | + Returns |
| 76 | + ------- |
| 77 | + ImagePlus |
| 78 | + Segmented 3D labelled ImagePlus. |
| 79 | + """ |
| 80 | + cal = imp.getCalibration() |
| 81 | + img = ImageHandler.wrap(imp) |
| 82 | + img = img.threshold(min_thresh, False, False) |
| 83 | + |
| 84 | + labeler = ImageLabeller() |
| 85 | + if min_vol: |
| 86 | + labeler.setMinSizeCalibrated(min_vol, img) |
| 87 | + if max_vol: |
| 88 | + labeler.setMaxSizeCalibrated(max_vol, img) # Issue with typo an |
| 89 | + # TODO: this method might not work on older Fiji deployments, needs testing |
| 90 | + # on newer versions (deprecated .setMaxsize, it's .setMaxSizeCalibrated) |
| 91 | + # TODO Keep it in mind for next Fiji deployment |
| 92 | + # labeler.setMaxsize(max_vol) |
| 93 | + |
| 94 | + seg = labeler.getLabels(img) |
| 95 | + seg.setScale(cal.pixelWidth, cal.pixelDepth, cal.getUnits()) |
| 96 | + seg.setTitle(title) |
| 97 | + |
| 98 | + return seg.getImagePlus() |
0 commit comments