Clear Outside, "this command requires a selection" even though using rm.select(). Issue with rm.select()?

Hello,

I have a script where I am trying to get an ROI of an image using an autothreshold and then trying to clear everything outside of that ROI. However, the issue is that when I create the roi, add it to the roi manager and then try and select it and use the “clear outside” command, it does not work and I get a prompt saying “this command requires a selection”. The portion of the script pertaining to this follows below. I took it from the macro recorder. Any help would be appreciated!

IJ.setAutoThreshold(imp, “Intermodes dark no-reset”)
IJ.run(imp, “Convert to Mask”, “”)
IJ.run(imp, “Analyze Particles…”, “size=100000-Infinity add”)
rm = RoiManager()
rm.select(imp, 0)
IJ.setBackgroundColor(0, 0, 0)
IJ.run(imp, “Clear Outside”, “”)

I’ve tried using imp= getCurrentImage() and then putting that in rm.select(), I’ve tried not putting any image input in rm.select(), but it essentially doesn’t select the ROI so that clear outside can clear everything outside the ROI.

Thanks,
Harris

The code you posted here could not be the code you have tested. Some elements are not allowed

Anyway, here is a complete plugin version which fixes your problem:

import ij.*;
import ij.plugin.frame.RoiManager;
import ij.plugin.*;

public class My_Plugin implements PlugIn {
	public void run(String arg) {
		ImagePlus imp = IJ.getImage();

		RoiManager rm = RoiManager.getInstance();
		if (rm != null)
		rm.reset();

		IJ.run(imp, "Analyze Particles...", "size=100-Infinity add");
		rm = RoiManager.getInstance();
		if (rm != null){
			rm.select(imp, 0);
			IJ.setBackgroundColor(0, 0, 0);
			IJ.run(imp, "Clear Outside", "");
		}
	}
}

Hi Phaub,

Thanks for the reply, the code does work, those elements are just an error in copy pasting the code from pycharm to the forum here. I’m writing the code in Jython. Either way, thank you for that, was just missing the getInstance() part of the code to point back to the RoiManager for the selection and now it works perfectly! Appreciate the response!

Thanks,
Harris