11from ij import IJ
22from mcib3d .geom import Objects3DPopulation
3- from mcib3d .image3d import ImageHandler
3+ from mcib3d .image3d import ImageHandler , ImageLabeller
44
55
66def population3d_to_imgplus (imp , population ):
@@ -54,3 +54,45 @@ def imgplus_to_population3d(imp):
5454 """
5555 img = ImageHandler .wrap (imp )
5656 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 on newer versions
90+ # (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