To apply ROIs from the Manager on another image with 'IJ.run("From ROI Manager", "")'

imagej
#1

I am going to create a batch process to apply ROIs from the Manager on another image with IJ.run(“From ROI Manager”, “”).
The idea was verified when I run the process within ImageJ Macro.

open("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTempC.jpg");
setAutoThreshold("Default");
//run("Threshold...");
setThreshold(80, 255);
//setThreshold(80, 255);
setOption("BlackBackground", false);
run("Convert to Mask");
run("Make Binary");
run("Fill Holes");
run("Convert to Mask");
run("Analyze Particles...", "size=30-3000 show=Masks display exclude clear add in_situ");
roiManager("Set Fill Color", "green");
open("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTemp.jpg");
run("From ROI Manager");
saveAs("Jpeg", "D:/testimageJ14 -autoanalyze/TestedNG/testNG-result20190326.jpg");
close();
selectWindow("fusedTempC.jpg");
close();

But it did not work when I combined the process in Java by the Java reference code which was generated by ImageJ. Here it is.

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class automark implements PlugIn {

	public void run(String arg) {
		RoiManager rm = RoiManager.getInstance();
		if (rm==null) rm = new RoiManager();
		ImagePlus imp = IJ.openImage("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTempC.jpg");
		IJ.setAutoThreshold(imp, "Default");
		IJ.setRawThreshold(imp, 80, 255, null);
		imp2 = imp.duplicate();
		Prefs.blackBackground = false;
		IJ.run(imp, "Make Binary", "");
		IJ.run(imp, "Fill Holes", "");
		imp2 = imp.duplicate();
		IJ.run(imp, "Analyze Particles...", "size=30-3000 show=Masks display exclude clear add in_situ");
		imp2 = imp.duplicate();
		rm.runCommand("Set Fill Color", "green");
		imp = IJ.openImage("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTemp.jpg");
		IJ.run("From ROI Manager", "");
		IJ.saveAs(imp, "Jpeg", "D:/testimageJ14 -autoanalyze/TestedNG/testNG-result20190326-2.jpg");
		imp.close();
		imp.close();
	}

}

It seems that the function of IJ.run("From ROI Manager", ""); did not work. I am new to Java and ImageJ and still perusing the User Guides of imageJ and developing topics.
But I really appreciate you for any support and advice here.
Thanks!

Best Regards,
DQ

0 Likes

#2

Hello DQ -

Your java plugin code works* for me.

*) As it stands, it doesn’t compile. You need to import
RoiManager, and declare the variable imp2 as ImagePlus.
(Two other oddities: You call imp.close() twice, and it
looks like you never do anything with imp2.)

When I make those changes, load a sample image instead of
your images, and save the processed image to a locally valid
path, I can compile and run it.

Here is the resulting image:

analyzed_blobs

If you’re still having problems, could you tell us more specifically
what isn’t working? What do you expect, and what unexpected
results or error messages are you getting? If it’s something specific
to the image you are processing, could you post the image?

(I compiled against and ran with a recently auto-updated Fiji /
“ImageJ 2.0.0-rc-69/1.52n”.)

For completeness, here is the (tweaked) code that I ran:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.RoiManager;

// public class automark implements PlugIn {
public class My_Plugin implements PlugIn {

	public void run(String arg) {
		RoiManager rm = RoiManager.getInstance();
		if (rm==null) rm = new RoiManager();
		// ImagePlus imp = IJ.openImage("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTempC.jpg");
		IJ.run ("Blobs (25K)");
		ImagePlus imp = IJ.getImage();
		IJ.setAutoThreshold(imp, "Default");
		IJ.setRawThreshold(imp, 80, 255, null);
		ImagePlus imp2 = imp.duplicate();
		Prefs.blackBackground = false;
		IJ.run(imp, "Make Binary", "");
		IJ.run(imp, "Fill Holes", "");
		imp2 = imp.duplicate();
		IJ.run(imp, "Analyze Particles...", "size=30-3000 show=Masks display exclude clear add in_situ");
		imp2 = imp.duplicate();
		rm.runCommand("Set Fill Color", "green");
		// imp = IJ.openImage("D:/testimageJ14 -autoanalyze/fusedtemp/fusedTemp.jpg");
		IJ.run ("Blobs (25K)");
		imp = IJ.getImage();
		IJ.run("From ROI Manager", "");
		// IJ.saveAs(imp, "Jpeg", "D:/testimageJ14 -autoanalyze/TestedNG/testNG-result20190326-2.jpg");
		IJ.saveAs (imp, "Jpeg", "/home/user/analyzed_blobs.jpg");
		imp.close();
		imp.close();
	}

}

Thanks, mm

0 Likes

#3

@mountain_man,

Thank you very much for your support.

I am trying to use ImageJ to build a test tool to catch and mask the white areas in the tile images from an industrial camera. The work piece is bad if there is more than one white area in its tile images.

here is the example image, fyi.

test%20tile%20example%202

test%20tile%20example%202-1

ImageJ ( and Fiji of course) is a great and powerful software. I think ImageJ could also help industrial engineers to build testing tools freely and quickly , e.g. offline industrial product surface quality inspection, image fusing, analysis and process etc.

0 Likes