Roi -> Enlarge function

Hi there,

is there a way of controlling the output of the function Enlarge when it does not return a roi?
I’m using it for roi reduction (Enlarge, value = -9), but I need to know if the roi colapses.

Thank you
Mafalda

You can use the Java API of RoiEnlarger (the class behind the Edit > Selection > Enlarge… command) to achieve this, e.g. in this Groovy script (runnable from the script editor after choosing Language > Groovy):

#@ ImagePlus imp

import ij.plugin.RoiEnlarger

roi=imp.getRoi()
newRoi = RoiEnlarger.enlarge(roi, -9)
if (newRoi.equals(roi))
	println "The ROI is too small to shrink further"
imp.setRoi(newRoi)

Thank you, Jan.
It works, but I need to put -9 as a variable.
I will use it this code, calling from ImageJ1 macro, but I don’t know how can I pass the value -9.
If I define a function like this:

def enlarge_check(int value){
	roi=imp.getRoi()

	newRoi = RoiEnlarger.enlarge(roi, -value)

	if (newRoi.equals(roi)){
		println "The ROI is too small to shrink further"
		return 0
	}
	else{
		return 1
	}
	
}

How do I run from imageJ, do you know?
Something like run(“enlarge_ckeck, value = -9”);

1 Like

Defining a function will only help when you call it from a full-fledged scripting language, but not from macros. Macros don’t support calling functions defined in other languages, and more importantly, the run macro function doesn’t support the idea of a return value.

To make this runnable from macro, you’ll have to:

  • add a script parameter to the above script,
  • decide for a way how the macro should test the output, e.g. by removing the ROI so you can test if selectionType == -1.

This will be the updated script:

#@ ImagePlus imp
#@ Double value

import ij.plugin.RoiEnlarger

roi=imp.getRoi()
newRoi = RoiEnlarger.enlarge(roi, -value)
if (newRoi.equals(roi))
	imp.setRoi(null)
else
	imp.setRoi(newRoi)

Then:

  • save the script into e.g. Fiji.app/scripts/Plugins/Shrink_ROI.groovy to make it recordable
  • restart Fiji
  • start the macro recorder (Plugins > Macros > Record…)
  • open an image and create an ROI
  • run your new script from the menu (Plugins > Shrink ROI)
  • add the recorded command to your macro, e.g.
run("Shrink ROI", "value=9")
if (selectionType == -1) {
  // the last ROI was too small
} else {
  // some processing with the smaller ROI
}

Hope that helps!

1 Like