imcflibs.imagej.roimanager

Functions to work with the RoiManager.

  1"""Functions to work with the RoiManager."""
  2
  3from ij.plugin import RoiEnlarger, RoiScaler  # pylint: disable-msg=import-error
  4from ij.plugin.frame import RoiManager  # pylint: disable-msg=import-error
  5
  6
  7def get_roimanager():
  8    """Instantiate or get the IJ-RoiManager instance.
  9
 10    Use to either get the current instance of the IJ RoiManager or instantiate
 11    it if it does not yet exist.
 12
 13    Returns
 14    -------
 15    ij.plugin.frame.RoiManager
 16        A reference of the IJ-RoiManager.
 17    """
 18    rm = RoiManager.getInstance()
 19    if not rm:
 20        rm = RoiManager()
 21    return rm
 22
 23
 24def clear_ij_roi_manager(rm):
 25    """Delete all ROIs from the RoiManager.
 26
 27    Parameters
 28    ----------
 29    rm : ij.plugin.frame.RoiManager
 30        A reference of the IJ-RoiManager.
 31    """
 32    rm.runCommand("reset")
 33
 34
 35def count_all_rois(rm):
 36    """Count the number of ROIS in the RoiManager.
 37
 38    Parameters
 39    ----------
 40    rm : ij.plugin.frame.RoiManager
 41        A reference of the IJ-RoiManager.
 42
 43    Returns
 44    -------
 45    int
 46        The number of ROIs in the RoiManager.
 47    """
 48    number_of_rois = rm.getCount()
 49
 50    return number_of_rois
 51
 52
 53def save_rois_to_zip(rm, target, selected_rois=None):
 54    """Save selected ROIs in the RoiManager as zip to the target path.
 55
 56    Parameters
 57    ----------
 58    rm : ij.plugin.frame.RoiManager
 59        A reference of the IJ-RoiManager.
 60    target : string
 61        The path to store the ROIs, e.g. /my-images/resulting_rois_subset.zip
 62    selected_rois : list
 63        selected ROIs in the RoiManager to save
 64    """
 65    if selected_rois is not None:
 66        rm.runCommand("Deselect")
 67        rm.setSelectedIndexes(selected_rois)
 68        rm.runCommand("save selected", target)
 69        rm.runCommand("Deselect")
 70    else:
 71        rm.runCommand("Save", target)
 72
 73
 74def show_all_rois_on_image(rm, imp):
 75    """Show all ROIs in the ROiManager on the given ImagePlus.
 76
 77    Parameters
 78    ----------
 79    rm : ij.plugin.frame.RoiManager
 80        A reference of the IJ-RoiManager.
 81    imp : ij.ImagePlus
 82        The imp on which to show the ROIs.
 83    """
 84    rm.runCommand(imp, "Show All")
 85
 86
 87def rename_rois(rm, string):
 88    """Rename all ROIs to include the given string as a prefix.
 89
 90    Parameters
 91    ----------
 92    rm : ij.plugin.frame.RoiManager
 93        A reference of the IJ-RoiManager.
 94    string : str
 95        The string to prefix the ROIs with.
 96    """
 97    number_of_rois = rm.getCount()
 98    for roi in range(number_of_rois):
 99        rm.rename(roi, string + str(roi + 1))
100
101    rm.runCommand("UseNames", "true")
102
103
104def rename_rois_by_number(rm):
105    """Rename all ROIs in the RoiManager according to their index number.
106
107    Parameters
108    ----------
109    rm : ij.plugin.frame.RoiManager
110        A reference of the IJ-RoiManager.
111    """
112    number_of_rois = rm.getCount()
113    for roi in range(number_of_rois):
114        rm.rename(roi, str(roi + 1))
115
116
117def change_roi_color(rm, color, selected_rois=None):
118    """Change the color of selected / all ROIs in the RoiManager.
119
120    Parameters
121    ----------
122    rm : ij.plugin.frame.RoiManager
123        A reference of the IJ-RoiManager.
124    color : string
125        The desired color. e.g. "green", "red", "yellow", "magenta" ...
126    selected_rois : list, optional
127        ROIs in the RoiManager that should be changed. By default None which
128        will result in all ROIs to be changed.
129    """
130    if selected_rois is not None:
131        rm.runCommand("Deselect")
132        rm.setSelectedIndexes(selected_rois)
133        rm.runCommand("Set Color", color)
134        rm.runCommand("Deselect")
135    else:
136        number_of_rois = rm.getCount()
137        for roi in range(number_of_rois):
138            rm.select(roi)
139            rm.runCommand("Set Color", color)
140
141
142def measure_in_all_rois(imp, channel, rm):
143    """Perform all configured measurements in one channel of the given image.
144
145    The choice of measured parameters is done through ImageJ's "Set
146    Measurements" command.
147
148    Parameters
149    ----------
150    imp : ij.ImagePlus
151        The imp to measure on.
152    channel : integer
153        The channel to measure in (starting at 1).
154    rm : ij.plugin.frame.RoiManager
155        A reference of the IJ-RoiManager.
156    """
157    imp.setC(channel)
158    rm.runCommand(imp, "Deselect")
159    rm.runCommand(imp, "Measure")
160
161
162def load_rois_from_zip(rm, path):
163    """Load ROIs from the given zip file and add them to the RoiManager.
164
165    Parameters
166    ----------
167    rm : ij.plugin.frame.RoiManager
168        A reference of the IJ-RoiManager.
169    path : string
170        Path to the ROI zip file.
171    """
172    rm.runCommand("Open", path)
173
174
175def enlarge_all_rois(amount_in_um, rm, pixel_size_in_um):
176    """Enlarge all ROIs in the RoiManager by x scaled units.
177
178    Parameters
179    ----------
180    amount_in_um : float
181        The value by which to enlarge in scaled units, e.g 3.5.
182    rm : ij.plugin.frame.RoiManager
183        A reference of the IJ-RoiManager.
184    pixel_size_in_um : float
185        The pixel size, e.g. 0.65 px/um.
186    """
187    amount_px = amount_in_um / pixel_size_in_um
188    all_rois = rm.getRoisAsArray()
189    rm.reset()
190    for roi in all_rois:
191        enlarged_roi = RoiEnlarger.enlarge(roi, amount_px)
192        rm.addRoi(enlarged_roi)
193
194
195def scale_all_rois(rm, scaling_factor):
196    """Inflate or shrink all ROIs in the RoiManager.
197
198    Parameters
199    ----------
200    rm : ij.plugin.frame.RoiManager
201        A reference of the IJ-RoiManager.
202    scaling_factor : float
203        The scaling factor by which to inflate (if > 1) or shrink (if < 1 ).
204    """
205    all_rois = rm.getRoisAsArray()
206    rm.reset()
207    for roi in all_rois:
208        scaled_roi = RoiScaler.scale(roi, scaling_factor, scaling_factor, True)
209        rm.addRoi(scaled_roi)
210
211
212def select_rois_above_min_intensity(imp, channel, rm, min_intensity):
213    """Select ROIs based on their intensity in a given channel of the image.
214
215    See https://imagej.nih.gov/ij/developer/api/ij/process/ImageStatistics.html
216
217    Parameters
218    ----------
219    imp : ij.ImagePlus
220        The imp on which to measure.
221    channel : integer
222        The channel to measure in (starting at 1).
223    rm : ij.plugin.frame.RoiManager
224        A reference of the IJ-RoiManager.
225    min_intensity : integer
226        The selection criterion (lower intensity threshold).
227
228    Returns
229    -------
230    list(int)
231        A list of ROI index numbers fulfilling the selection criterion
232        (intensity is above the threshold).
233    """
234    imp.setC(channel)
235    all_rois = rm.getRoisAsArray()
236    selected_rois = []
237    for i, roi in enumerate(all_rois):
238        imp.setRoi(roi)
239        stats = imp.getStatistics()
240        if stats.max > min_intensity:
241            selected_rois.append(i)
242
243    return selected_rois
244
245
246def extract_color_of_all_rois(rm):
247    """Get the color names of the ROIs in the RoiManager.
248
249    Iterates over all ROIs and gets either their "Stroke Color" (if present) or
250    their "Color" property.
251
252    Parameters
253    ----------
254    rm : ij.plugin.frame.RoiManager
255        A reference of the IJ-RoiManager.
256
257    Returns
258    -------
259    list
260        A list containing the corresponding color name strings for each ROI.
261    """
262    rgb_color_lookup = {
263        -65536: "red",
264        -65281: "magenta",
265        -16711936: "green",
266        -256: "yellow",
267        -1: "white",
268        -16776961: "blue",
269        -16777216: "black",
270        -14336: "orange",
271        -16711681: "cyan",
272    }
273
274    all_rois = rm.getRoisAsArray()
275    roi_colors = []
276    for roi in all_rois:
277        stroke_color = roi.getStrokeColor()
278        if stroke_color:
279            roi_colors.append(rgb_color_lookup[stroke_color.getRGB()])
280        else:
281            roi_colors.append(rgb_color_lookup[roi.getColor().getRGB()])
282
283    return roi_colors
284
285
286def add_rois_to_roimanager(
287    roi_array, roi_manager, keep_rois_name, prefix, bbox=None, z_slice=None, group=None
288):
289    """Add all ROIs from a list to the RoiManager.
290
291    Parameters
292    ----------
293    roi_array : list(ij.gui.Roi)
294        List of ROIs to put in RM.
295    roi_manager : ij.plugin.frame.RoiManager
296        ROIManager in which to put the ROIs.
297    keep_rois_name : bool
298        If true, will keep the name of the ROI. Otherwise the ROI will be
299        renamed using its index number.
300    prefix : str
301        String to prefix the name of the ROI with.
302    bbox : java.awt.Rectangle, optional
303        Use this bounding box to shift the ROI list, by default None.
304    z_slice : int, optional
305        Shift the ROI also in Z, by default None (=no shifting).
306    group : int, optional
307        Put the ROI into the given ROI group, by default None.
308    """
309    # roi_manager.reset()
310    for index, roi in enumerate(roi_array):
311        if not keep_rois_name:
312            roi.setName(prefix + "-" + str(index))
313        else:
314            roi.setName(prefix + "-" + roi.getName())
315        if bbox is not None:
316            roi = shift_roi_by_bounding_box(roi, bbox, z_slice)
317        if group is not None:
318            roi.setGroup(group)
319        roi_manager.addRoi(roi)
320
321
322def shift_roi_by_bounding_box(roi, bbox, z_slice=None):
323    """Move a ROI based on a bounding box.
324
325    Translate one ROI based on another ROI's bounding box.
326
327    Parameters
328    ----------
329    roi : ij.gui.Roi
330        The ROI to be moved.
331    bbox : java.awt.Rectangle
332        The bounding box by which the ROI should be shifted, e.g. retrieved by
333        calling `OtherRoi.getBounds()` on a ROI object.
334    z_slice : int, optional
335        Shift the ROI also in Z, by default None (=no shifting).
336    """
337    # roi_manager.reset()
338    roi.setLocation(bbox.x + roi.getBounds().x, bbox.y + roi.getBounds().y)
339    if z_slice is not None:
340        roi.setPosition(roi.getPosition() + z_slice)
341    return roi
def get_roimanager():
 8def get_roimanager():
 9    """Instantiate or get the IJ-RoiManager instance.
10
11    Use to either get the current instance of the IJ RoiManager or instantiate
12    it if it does not yet exist.
13
14    Returns
15    -------
16    ij.plugin.frame.RoiManager
17        A reference of the IJ-RoiManager.
18    """
19    rm = RoiManager.getInstance()
20    if not rm:
21        rm = RoiManager()
22    return rm

Instantiate or get the IJ-RoiManager instance.

Use to either get the current instance of the IJ RoiManager or instantiate it if it does not yet exist.

Returns
  • ij.plugin.frame.RoiManager: A reference of the IJ-RoiManager.
def clear_ij_roi_manager(rm):
25def clear_ij_roi_manager(rm):
26    """Delete all ROIs from the RoiManager.
27
28    Parameters
29    ----------
30    rm : ij.plugin.frame.RoiManager
31        A reference of the IJ-RoiManager.
32    """
33    rm.runCommand("reset")

Delete all ROIs from the RoiManager.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
def count_all_rois(rm):
36def count_all_rois(rm):
37    """Count the number of ROIS in the RoiManager.
38
39    Parameters
40    ----------
41    rm : ij.plugin.frame.RoiManager
42        A reference of the IJ-RoiManager.
43
44    Returns
45    -------
46    int
47        The number of ROIs in the RoiManager.
48    """
49    number_of_rois = rm.getCount()
50
51    return number_of_rois

Count the number of ROIS in the RoiManager.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
Returns
  • int: The number of ROIs in the RoiManager.
def save_rois_to_zip(rm, target, selected_rois=None):
54def save_rois_to_zip(rm, target, selected_rois=None):
55    """Save selected ROIs in the RoiManager as zip to the target path.
56
57    Parameters
58    ----------
59    rm : ij.plugin.frame.RoiManager
60        A reference of the IJ-RoiManager.
61    target : string
62        The path to store the ROIs, e.g. /my-images/resulting_rois_subset.zip
63    selected_rois : list
64        selected ROIs in the RoiManager to save
65    """
66    if selected_rois is not None:
67        rm.runCommand("Deselect")
68        rm.setSelectedIndexes(selected_rois)
69        rm.runCommand("save selected", target)
70        rm.runCommand("Deselect")
71    else:
72        rm.runCommand("Save", target)

Save selected ROIs in the RoiManager as zip to the target path.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • target (string): The path to store the ROIs, e.g. /my-images/resulting_rois_subset.zip
  • selected_rois (list): selected ROIs in the RoiManager to save
def show_all_rois_on_image(rm, imp):
75def show_all_rois_on_image(rm, imp):
76    """Show all ROIs in the ROiManager on the given ImagePlus.
77
78    Parameters
79    ----------
80    rm : ij.plugin.frame.RoiManager
81        A reference of the IJ-RoiManager.
82    imp : ij.ImagePlus
83        The imp on which to show the ROIs.
84    """
85    rm.runCommand(imp, "Show All")

Show all ROIs in the ROiManager on the given ImagePlus.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • imp (ij.ImagePlus): The imp on which to show the ROIs.
def rename_rois(rm, string):
 88def rename_rois(rm, string):
 89    """Rename all ROIs to include the given string as a prefix.
 90
 91    Parameters
 92    ----------
 93    rm : ij.plugin.frame.RoiManager
 94        A reference of the IJ-RoiManager.
 95    string : str
 96        The string to prefix the ROIs with.
 97    """
 98    number_of_rois = rm.getCount()
 99    for roi in range(number_of_rois):
100        rm.rename(roi, string + str(roi + 1))
101
102    rm.runCommand("UseNames", "true")

Rename all ROIs to include the given string as a prefix.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • string (str): The string to prefix the ROIs with.
def rename_rois_by_number(rm):
105def rename_rois_by_number(rm):
106    """Rename all ROIs in the RoiManager according to their index number.
107
108    Parameters
109    ----------
110    rm : ij.plugin.frame.RoiManager
111        A reference of the IJ-RoiManager.
112    """
113    number_of_rois = rm.getCount()
114    for roi in range(number_of_rois):
115        rm.rename(roi, str(roi + 1))

Rename all ROIs in the RoiManager according to their index number.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
def change_roi_color(rm, color, selected_rois=None):
118def change_roi_color(rm, color, selected_rois=None):
119    """Change the color of selected / all ROIs in the RoiManager.
120
121    Parameters
122    ----------
123    rm : ij.plugin.frame.RoiManager
124        A reference of the IJ-RoiManager.
125    color : string
126        The desired color. e.g. "green", "red", "yellow", "magenta" ...
127    selected_rois : list, optional
128        ROIs in the RoiManager that should be changed. By default None which
129        will result in all ROIs to be changed.
130    """
131    if selected_rois is not None:
132        rm.runCommand("Deselect")
133        rm.setSelectedIndexes(selected_rois)
134        rm.runCommand("Set Color", color)
135        rm.runCommand("Deselect")
136    else:
137        number_of_rois = rm.getCount()
138        for roi in range(number_of_rois):
139            rm.select(roi)
140            rm.runCommand("Set Color", color)

Change the color of selected / all ROIs in the RoiManager.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • color (string): The desired color. e.g. "green", "red", "yellow", "magenta" ...
  • selected_rois (list, optional): ROIs in the RoiManager that should be changed. By default None which will result in all ROIs to be changed.
def measure_in_all_rois(imp, channel, rm):
143def measure_in_all_rois(imp, channel, rm):
144    """Perform all configured measurements in one channel of the given image.
145
146    The choice of measured parameters is done through ImageJ's "Set
147    Measurements" command.
148
149    Parameters
150    ----------
151    imp : ij.ImagePlus
152        The imp to measure on.
153    channel : integer
154        The channel to measure in (starting at 1).
155    rm : ij.plugin.frame.RoiManager
156        A reference of the IJ-RoiManager.
157    """
158    imp.setC(channel)
159    rm.runCommand(imp, "Deselect")
160    rm.runCommand(imp, "Measure")

Perform all configured measurements in one channel of the given image.

The choice of measured parameters is done through ImageJ's "Set Measurements" command.

Parameters
  • imp (ij.ImagePlus): The imp to measure on.
  • channel (integer): The channel to measure in (starting at 1).
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
def load_rois_from_zip(rm, path):
163def load_rois_from_zip(rm, path):
164    """Load ROIs from the given zip file and add them to the RoiManager.
165
166    Parameters
167    ----------
168    rm : ij.plugin.frame.RoiManager
169        A reference of the IJ-RoiManager.
170    path : string
171        Path to the ROI zip file.
172    """
173    rm.runCommand("Open", path)

Load ROIs from the given zip file and add them to the RoiManager.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • path (string): Path to the ROI zip file.
def enlarge_all_rois(amount_in_um, rm, pixel_size_in_um):
176def enlarge_all_rois(amount_in_um, rm, pixel_size_in_um):
177    """Enlarge all ROIs in the RoiManager by x scaled units.
178
179    Parameters
180    ----------
181    amount_in_um : float
182        The value by which to enlarge in scaled units, e.g 3.5.
183    rm : ij.plugin.frame.RoiManager
184        A reference of the IJ-RoiManager.
185    pixel_size_in_um : float
186        The pixel size, e.g. 0.65 px/um.
187    """
188    amount_px = amount_in_um / pixel_size_in_um
189    all_rois = rm.getRoisAsArray()
190    rm.reset()
191    for roi in all_rois:
192        enlarged_roi = RoiEnlarger.enlarge(roi, amount_px)
193        rm.addRoi(enlarged_roi)

Enlarge all ROIs in the RoiManager by x scaled units.

Parameters
  • amount_in_um (float): The value by which to enlarge in scaled units, e.g 3.5.
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • pixel_size_in_um (float): The pixel size, e.g. 0.65 px/um.
def scale_all_rois(rm, scaling_factor):
196def scale_all_rois(rm, scaling_factor):
197    """Inflate or shrink all ROIs in the RoiManager.
198
199    Parameters
200    ----------
201    rm : ij.plugin.frame.RoiManager
202        A reference of the IJ-RoiManager.
203    scaling_factor : float
204        The scaling factor by which to inflate (if > 1) or shrink (if < 1 ).
205    """
206    all_rois = rm.getRoisAsArray()
207    rm.reset()
208    for roi in all_rois:
209        scaled_roi = RoiScaler.scale(roi, scaling_factor, scaling_factor, True)
210        rm.addRoi(scaled_roi)

Inflate or shrink all ROIs in the RoiManager.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • scaling_factor (float): The scaling factor by which to inflate (if > 1) or shrink (if < 1 ).
def select_rois_above_min_intensity(imp, channel, rm, min_intensity):
213def select_rois_above_min_intensity(imp, channel, rm, min_intensity):
214    """Select ROIs based on their intensity in a given channel of the image.
215
216    See https://imagej.nih.gov/ij/developer/api/ij/process/ImageStatistics.html
217
218    Parameters
219    ----------
220    imp : ij.ImagePlus
221        The imp on which to measure.
222    channel : integer
223        The channel to measure in (starting at 1).
224    rm : ij.plugin.frame.RoiManager
225        A reference of the IJ-RoiManager.
226    min_intensity : integer
227        The selection criterion (lower intensity threshold).
228
229    Returns
230    -------
231    list(int)
232        A list of ROI index numbers fulfilling the selection criterion
233        (intensity is above the threshold).
234    """
235    imp.setC(channel)
236    all_rois = rm.getRoisAsArray()
237    selected_rois = []
238    for i, roi in enumerate(all_rois):
239        imp.setRoi(roi)
240        stats = imp.getStatistics()
241        if stats.max > min_intensity:
242            selected_rois.append(i)
243
244    return selected_rois

Select ROIs based on their intensity in a given channel of the image.

See https://imagej.nih.gov/ij/developer/api/ij/process/ImageStatistics.html

Parameters
  • imp (ij.ImagePlus): The imp on which to measure.
  • channel (integer): The channel to measure in (starting at 1).
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
  • min_intensity (integer): The selection criterion (lower intensity threshold).
Returns
  • list(int): A list of ROI index numbers fulfilling the selection criterion (intensity is above the threshold).
def extract_color_of_all_rois(rm):
247def extract_color_of_all_rois(rm):
248    """Get the color names of the ROIs in the RoiManager.
249
250    Iterates over all ROIs and gets either their "Stroke Color" (if present) or
251    their "Color" property.
252
253    Parameters
254    ----------
255    rm : ij.plugin.frame.RoiManager
256        A reference of the IJ-RoiManager.
257
258    Returns
259    -------
260    list
261        A list containing the corresponding color name strings for each ROI.
262    """
263    rgb_color_lookup = {
264        -65536: "red",
265        -65281: "magenta",
266        -16711936: "green",
267        -256: "yellow",
268        -1: "white",
269        -16776961: "blue",
270        -16777216: "black",
271        -14336: "orange",
272        -16711681: "cyan",
273    }
274
275    all_rois = rm.getRoisAsArray()
276    roi_colors = []
277    for roi in all_rois:
278        stroke_color = roi.getStrokeColor()
279        if stroke_color:
280            roi_colors.append(rgb_color_lookup[stroke_color.getRGB()])
281        else:
282            roi_colors.append(rgb_color_lookup[roi.getColor().getRGB()])
283
284    return roi_colors

Get the color names of the ROIs in the RoiManager.

Iterates over all ROIs and gets either their "Stroke Color" (if present) or their "Color" property.

Parameters
  • rm (ij.plugin.frame.RoiManager): A reference of the IJ-RoiManager.
Returns
  • list: A list containing the corresponding color name strings for each ROI.
def add_rois_to_roimanager( roi_array, roi_manager, keep_rois_name, prefix, bbox=None, z_slice=None, group=None):
287def add_rois_to_roimanager(
288    roi_array, roi_manager, keep_rois_name, prefix, bbox=None, z_slice=None, group=None
289):
290    """Add all ROIs from a list to the RoiManager.
291
292    Parameters
293    ----------
294    roi_array : list(ij.gui.Roi)
295        List of ROIs to put in RM.
296    roi_manager : ij.plugin.frame.RoiManager
297        ROIManager in which to put the ROIs.
298    keep_rois_name : bool
299        If true, will keep the name of the ROI. Otherwise the ROI will be
300        renamed using its index number.
301    prefix : str
302        String to prefix the name of the ROI with.
303    bbox : java.awt.Rectangle, optional
304        Use this bounding box to shift the ROI list, by default None.
305    z_slice : int, optional
306        Shift the ROI also in Z, by default None (=no shifting).
307    group : int, optional
308        Put the ROI into the given ROI group, by default None.
309    """
310    # roi_manager.reset()
311    for index, roi in enumerate(roi_array):
312        if not keep_rois_name:
313            roi.setName(prefix + "-" + str(index))
314        else:
315            roi.setName(prefix + "-" + roi.getName())
316        if bbox is not None:
317            roi = shift_roi_by_bounding_box(roi, bbox, z_slice)
318        if group is not None:
319            roi.setGroup(group)
320        roi_manager.addRoi(roi)

Add all ROIs from a list to the RoiManager.

Parameters
  • roi_array (list(ij.gui.Roi)): List of ROIs to put in RM.
  • roi_manager (ij.plugin.frame.RoiManager): ROIManager in which to put the ROIs.
  • keep_rois_name (bool): If true, will keep the name of the ROI. Otherwise the ROI will be renamed using its index number.
  • prefix (str): String to prefix the name of the ROI with.
  • bbox (java.awt.Rectangle, optional): Use this bounding box to shift the ROI list, by default None.
  • z_slice (int, optional): Shift the ROI also in Z, by default None (=no shifting).
  • group (int, optional): Put the ROI into the given ROI group, by default None.
def shift_roi_by_bounding_box(roi, bbox, z_slice=None):
323def shift_roi_by_bounding_box(roi, bbox, z_slice=None):
324    """Move a ROI based on a bounding box.
325
326    Translate one ROI based on another ROI's bounding box.
327
328    Parameters
329    ----------
330    roi : ij.gui.Roi
331        The ROI to be moved.
332    bbox : java.awt.Rectangle
333        The bounding box by which the ROI should be shifted, e.g. retrieved by
334        calling `OtherRoi.getBounds()` on a ROI object.
335    z_slice : int, optional
336        Shift the ROI also in Z, by default None (=no shifting).
337    """
338    # roi_manager.reset()
339    roi.setLocation(bbox.x + roi.getBounds().x, bbox.y + roi.getBounds().y)
340    if z_slice is not None:
341        roi.setPosition(roi.getPosition() + z_slice)
342    return roi

Move a ROI based on a bounding box.

Translate one ROI based on another ROI's bounding box.

Parameters
  • roi (ij.gui.Roi): The ROI to be moved.
  • bbox (java.awt.Rectangle): The bounding box by which the ROI should be shifted, e.g. retrieved by calling OtherRoi.getBounds() on a ROI object.
  • z_slice (int, optional): Shift the ROI also in Z, by default None (=no shifting).