Weird bug in a macro involving ROIs, cropping, analyze particles

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Weird bug in a macro involving ROIs, cropping, analyze particles

JDing
I made the following macro to measure the grayscale color of seeds in a 48-well plate in a particular order. It essentially takes a picture of the seeds in the plate, crops out each seed one at a time, measures it, and puts the measurement on a results table.

The problem is that some of the measurements are incorrect. This occurs when the measurements for a seed simply repeat the measurements for the previous seed. For example, when I execute the macro, the first 4 entries on the results table looks like this:



Clearly, the second row is wrong, as many of the measurements are the same as for the previous seed. The weird part about this is that when I close everything (except for ImageJ itself) and rerun the macro under the exact same conditions, the results are different (and still incorrect). Here is the results table after the second try:



After running it a third time, the results are back to what they were the first time:



The results seem to alternate between these two cases each time I run the macro. The bug doesn't occur if I execute all the commands of the macro manually.

Here is the script:

title = getTitle()
open("C:\\Users\\GrandMaster\\Pictures\\ControlCenter3\\blank.jpg");
imageCalculator("Subtract", title,"blank.jpg");
selectWindow(title);
run("8-bit");
run("Set Scale...", "distance=3229 known=27.3388666667 pixel=1 unit=cm global");
roiManager("Open", "C:\\Users\\GrandMaster\\Pictures\\ControlCenter3\\RoiSet.zip");
run("Clear Results");
for (i=0; i<48; i++) {
        roiManager("Select", i);
        run("Duplicate...", "title=duplicate-1.jpg");
        selectWindow(title);
        run("Duplicate...", "title=duplicate-2.jpg");
        selectWindow("duplicate-2.jpg");
        setAutoThreshold("Default dark");
        //run("Threshold...");
        setThreshold(10, 255);
        run("Convert to Mask");
        roiManager("Show All with labels");
        roiManager("Show All");
        run("Analyze Particles...", "size=0.10-1.00 circularity=0.00-1.00 show=Nothing add");
        selectWindow("duplicate-1.jpg");
        roiManager("Select", 48);
        roiManager("Measure");
        roiManager("Select", 48);
        roiManager("Delete");
        selectWindow("duplicate-2.jpg");
        close();
        selectWindow("duplicate-1.jpg");
        close();
}


The bolded section (the for loop) is where I think something goes wrong. Essentially what I'm doing in the loop is taking the first well and using the first duplicate ("duplicate-1") to find out where the seed is within the well, and establishing that as ROI 48. I then use the second duplicate to use ROI 48 to measure the grayscale, then repeat this process another 47 times for the other 47 wells.

I'm not sure what's going on here. I've looked at my script for a long time and can't figure out where I coded incorrectly. Perhaps ImageJ is accidentally using the ROI for the previous well for the current one? Any help would be greatly appreciated.

JDing