Clear Outside, "this command requires a selection" even though using Issue with


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”), “Convert to Mask”, “”), “Analyze Particles…”, “size=100000-Infinity add”)
rm = RoiManager(), 0)
IJ.setBackgroundColor(0, 0, 0), “Clear Outside”, “”)

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


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();, "Analyze Particles...", "size=100-Infinity add");
		rm = RoiManager.getInstance();
		if (rm != null){, 0);
			IJ.setBackgroundColor(0, 0, 0);, "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!