Skip to content

Commit 2affd76

Browse files
authored
Merge pull request #11 from imcf/3d-suite
3d suite
2 parents 56c7058 + 120dfed commit 2affd76

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

src/imcflibs/imagej/_loci.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from loci.plugins import BF
2727

28-
from loci.plugins.in import ImporterOptions # pdoc: skip
28+
from loci.plugins.in import ImporterOptions # pdoc: skip
29+
from loci.formats.in import ZeissCZIReader, DefaultMetadataOptions, MetadataLevel, DynamicMetadataOptions, MetadataOptions # pdoc: skip
2930

3031
from loci.formats import ImageReader, Memoizer

src/imcflibs/imagej/labelimage.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ij.process import FloatProcessor, ImageProcessor
77
from inra.ijpb.label import LabelImages as li
88
from inra.ijpb.plugins import AnalyzeRegions
9+
from mcib3d.image3d import ImageHandler, ImageLabeller
910

1011

1112
def label_image_to_roi_list(label_image, low_thresh=None):
@@ -138,3 +139,44 @@ def measure_objects_size_shape_2d(label_image):
138139
"""
139140
regions = AnalyzeRegions()
140141
return regions.process(label_image)
142+
143+
144+
def binary_to_label(imp, title, min_thresh=1, min_vol=None, max_vol=None):
145+
"""Segment a binary image to get a label image (2D/3D).
146+
147+
Works on: 2D and 3D binary data.
148+
149+
Parameters
150+
----------
151+
imp : ImagePlus
152+
Binary 3D stack or 2D image.
153+
title : str
154+
Title of the new image.
155+
min_thresh : int, optional
156+
Threshold to do segmentation, also allows for label filtering, by
157+
default 1.
158+
min_vol : float, optional
159+
Volume under which to exclude objects, by default None.
160+
max_vol : float, optional
161+
Volume above which to exclude objects, by default None.
162+
163+
Returns
164+
-------
165+
ImagePlus
166+
Segmented labeled ImagePlus.
167+
"""
168+
cal = imp.getCalibration()
169+
img = ImageHandler.wrap(imp)
170+
img = img.threshold(min_thresh, False, False)
171+
172+
labeler = ImageLabeller()
173+
if min_vol:
174+
labeler.setMinSize(min_vol)
175+
if max_vol:
176+
labeler.setMaxSize(max_vol)
177+
178+
seg = labeler.getLabels(img)
179+
seg.setScale(cal.pixelWidth, cal.pixelDepth, cal.getUnits())
180+
seg.setTitle(title)
181+
182+
return seg.getImagePlus()

src/imcflibs/imagej/objects3d.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from ij import IJ
2+
from mcib3d.geom import Objects3DPopulation
3+
from mcib3d.image3d import ImageHandler
4+
5+
6+
def population3d_to_imgplus(imp, population):
7+
"""Make an ImagePlus from an Objects3DPopulation (2D/3D).
8+
9+
Works on: 2D and 3D.
10+
11+
Parameters
12+
----------
13+
imp : ij.ImagePlus
14+
Original ImagePlus to derive the size of the resulting ImagePlus.
15+
population : mcib3d.geom.Objects3DPopulation
16+
Population to use to generate the new ImagePlus.
17+
18+
Returns
19+
-------
20+
ImagePlus
21+
Newly created ImagePlus from the population.
22+
"""
23+
dim = imp.getDimensions()
24+
new_imp = IJ.createImage(
25+
"Filtered labeled stack",
26+
"16-bit black",
27+
dim[0],
28+
dim[1],
29+
1,
30+
dim[3],
31+
dim[4],
32+
)
33+
new_imp.setCalibration(imp.getCalibration())
34+
new_img = ImageHandler.wrap(new_imp)
35+
population.drawPopulation(new_img)
36+
37+
return new_img.getImagePlus()
38+
39+
40+
def imgplus_to_population3d(imp):
41+
"""Get an Objects3DPopulation from an ImagePlus (2D/3D).
42+
43+
Works on: 2D and 3D.
44+
45+
Parameters
46+
----------
47+
imp : ij.ImagePlus
48+
Labeled 3D stack or 2D image to use to get population.
49+
50+
Returns
51+
-------
52+
mcib3d.geom.Objects3DPopulation
53+
Population from the image.
54+
"""
55+
img = ImageHandler.wrap(imp)
56+
return Objects3DPopulation(img)

src/imcflibs/strtools.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def sort_alphanumerically(data):
133133
>>> sort_alphanumerically([ "foo-1", "foo-2", "foo-10" ])
134134
["foo-1", "foo-2", "foo-10"]
135135
"""
136-
convert = lambda text: int(text) if text.isdigit() else text.lower()
137-
alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]
136+
137+
def convert(text):
138+
return int(text) if text.isdigit() else text.lower()
139+
140+
def alphanum_key(key):
141+
return [convert(c) for c in re.split("([0-9]+)", key)]
142+
138143
return sorted(data, key=alphanum_key)

0 commit comments

Comments
 (0)