Help with Java Code

Hi All,

I recorded a java code to perform colony counting using imagej. Now, I am interested in making this code more generic such that i don’t have to rerun the steps on imagej but just put in this code for direct assessment. I need help with two things:

  1. Making ROI more flexible such that if there is a rectangular image then rectangular Roi covering the 95% of the image is selected. If there is circular image as in the case of agar plate, then Oval ROI covering 95% of the image can be selected.
  2. Making redirect option more generic such that the grayscale image opened on imagej is selected in the redirect option.
    Here is the java code for reference:
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;

public class My_Plugin implements PlugIn {

	public void run(String arg) {
		ImagePlus imp = IJ.getImage();
		imp2 = imp.duplicate();
		//IJ.setTool("oval");
		imp.setRoi(new OvalRoi(203,34,172,377));
		imp.setRoi(new OvalRoi(40,34,335,377));
		imp.setRoi(new OvalRoi(40,34,374,377));
		imp.setRoi(new OvalRoi(40,34,374,389));
		IJ.setAutoThreshold(imp, "Default");
		Prefs.blackBackground = false;
		IJ.run(imp, "Convert to Mask", "");
		IJ.run("Close");
		IJ.run("Set Measurements...", "area mean integrated add redirect=colony.jpg decimal=3");
		IJ.run(imp, "Analyze Particles...", "  show=Outlines display exclude summarize");
	}

}

Thanks,
Rohitesh

I would recommend you to change the macro Recorder to “Macro”, instead of “Java”, and record your analysis pipeline again.
A little suggestion from my side, try to find someone at your local department or institute to help you with the whole image processing part of your project. If you can not afford the time and effort to learn from the basics of image processing and programming, there’s no point of doing it yourself. Otherwise, let’s just say a false or biased conclusion is so so so much worse than no conclusion.

If you will definitely need to do more or deeper image processing work yourself in the future, then please first do go through the following two links:
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html

With that and your recorded Macro codes, you should be able to understand each line of the recorded codes, and know where to make change to make your workflow more flexible.

If you had tried that, then I can propose some possible solution for your problem as follows:

  • 1st question
    If you ever had looked at the codes you recorded (even those ones in Java), you should already saw the four lines of codes starts like imp.setRoi(new OvalRoi(…,));
    You don’t need any programming knowledge to know these lines are possibly having something to do with your hand-draw ROIs.
    If you record a macro and draw a rectangle ROI, the recorder will record the following command:
    makeRectangle(203, 34, 172, 377);
    This mean you drew a rectangle shape with (203, 34) to be the top left corner coordinate, and with width 172 pixel and height 377 pixel;
    A circular ROI will be generated with similar command as:
    makeOval(203, 34, 172, 377);
    The 3rd and 4th parameter should be both the diameter of your desired circular ROI (Note in your recorded example, you never had a true circular ROI but always elliptical ROIs, as the width and height are never the same).
    You can also easily get the width and height of your current image with macro command: getWidth(), and getHeight();
    Therefore you are now equipped with all the parameters you can play with to fulfill what you want.
    Although to calculate the area of a Oval might not be so simple, I’m sure you can figure that all out by yourself now.

  • 2nd question
    in the macro recorder you should have the following line of code:
    run(“Set Measurements…”, “area mean integrated add redirect=colony.jpg decimal=3”);
    You see your redirected image name there as “colony.jpg” (Avoid jpeg images all the time with scientific image processing, will you). Therefore, you just need to assign the name of you new image here like following:
    redirectImage = “anotherColony.tif”; (change the name here to match the image you actually work with)
    run(“Set Measurements…”, “area mean integrated add redirect=[”+redirectImage+"] decimal=3");

Good luck