strange overlay and/or image calculator problem with batch macro

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
mjt
Reply | Threaded
Open this post in threaded view
|

strange overlay and/or image calculator problem with batch macro

mjt
Hi all,
I've written a batch macro to 'count' rice grains with cracks in them, and produce a labelled image of the original containing overlays of grains with cracks (orange), grains with no cracks (cyan) and the identified cracks themselves (red). The first image is fine, and the overlayed image saves to the output directory, but then the images after that seems to be overlayed as well with the original 'overlay' form the first image! I cant seem to figure out whats going on. I've dissected the macro and think that its with overlaying the coloured grains. I am pretty much a 'newbie' when it comes to macros like this, so any help would be very much appreciated! I've posted the code below and a couple of example images (final settings for processing haven't been finalised yet, so 'crack' finding is not excellent).
Cheers, Mark

.....start of macro
input = getDirectory("Choose Source Directory ");
output = getDirectory("Choose Destination Directory ");

setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
        action(input, output, list[i]);
setBatchMode(false);

function action(input, output, filename) {
    showProgress(i+1, list.length);
    open(input + filename);
    original=getTitle();
        dotIndex = indexOf(original, ".");
        title = substring(original, 0, dotIndex);
        run("Set Measurements...", "  redirect=None decimal=2");
        run("Colors...", "foreground=black background=white selection=green");
        run("8-bit");
        //crop original image
        run("Duplicate...", "title=crop");
        selectWindow("crop");
        run("Auto Threshold", "method=Triangle");
        run("Invert");
        run("Analyze Particles...", "size=1000-Infinity show=Masks exclude in_situ");
        run("Create Selection");
        run("Make Inverse");
        run("To Bounding Box");
        run("Scale... ", "x=1.08 y=1.08 centered");
        selectWindow(original); //when using a variable as title (original), don't use quotes around title
        run("Restore Selection");
        run("Crop");
        close("crop");
        selectWindow(original);
        run("Select None");
        //create mask of seeds
        run("Duplicate...", "title=plate");
        run("Duplicate...", "title=cracks");
        run("Duplicate...", "title=grains");
        run("Duplicate...", "title=labels");
        selectWindow("grains");
        run("Auto Threshold", "method=Minimum");
        selectWindow("plate");
        run("Auto Threshold", "method=Triangle");
        run("Invert");
        run("Analyze Particles...", "size=1000-Infinity show=Masks exclude in_situ");
        run("Create Selection");
        run("Make Inverse");
        run("Scale... ", "x=0.79 y=0.79 centered");
        selectWindow("grains");
        run("Restore Selection");
        run("Make Inverse");
        run("Fill", "slice");
        close("plate");
        //filter to remove grains touching edge and brokens
        run("Make Inverse");
        run("Adjustable Watershed", "tolerance=8");
        run("Analyze Particles...", "size=1000-Infinity circularity=0.5-1.00 show=Masks exclude in_situ");
        run("Select None");
        run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing add");
        //fill in cracks on edge of grains
        run("Colors...", "foreground=white background=black selection=green");
        setForegroundColor(255, 255, 255);
        for (i=0 ; i<roiManager("count"); i++) {
            roiManager("select", i);
            run("Enlarge...", "enlarge=20");
            run("Enlarge...", "enlarge=-20");
            run("Fill", "slice");
                roiManager("Update");
        }
        run("Colors...", "foreground=black background=white selection=green");
        run("Adjustable Watershed", "tolerance=8");
        //Produce 'cracks' image
        selectWindow("cracks");
        run("Median...", "radius=6");
        imageCalculator("Subtract create", original,"cracks");
        close("cracks");
        selectWindow("Result of " +original);
        rename("cracks");
        run("Enhance Contrast...", "saturated=4 normalize");
        run("Despeckle");
        run("Auto Threshold", "method=Moments white");
        selectWindow(original);
        selectWindow("grains");
        run("Create Selection");
        run("Make Inverse");
        selectWindow("cracks");
        run("Restore Selection");
        run("Enlarge...", "enlarge=-6");
        run("Make Inverse");
        run("Colors...", "foreground=black background=black selection=green");
        setForegroundColor(0, 0, 0);
        run("Fill", "slice");
        run("Select None");
        run("Analyze Particles...", "size=30-Infinity circularity=0.00-0.5 show=Masks in_situ");
        //Select grains with cracks
        run("BinaryReconstruct ", "mask=grains seed=cracks create white");
        selectWindow("Reconstructed");
        rename("grains with cracks");
        selectWindow("grains");
        imageCalculator("XOR create", "grains with cracks","grains");
        selectWindow("Result of grains with cracks");
        rename("grains no cracks");
        selectWindow("grains no cracks");
        run("Analyze Particles...", "  show=Nothing summarize");
        selectWindow("grains with cracks");
        run("Analyze Particles...", "summarize");
        selectWindow("Summary");
  saveAs("Measurements", output + title + "_summary.xls");
  if (isOpen(title + "_summary.xls")) {
       selectWindow(title + "_summary.xls");
       run("Close");
    }
        //Label original with colours and cracks
        selectWindow("grains with cracks");
        run("Create Selection");
        run("Make Inverse");
        run("Colors...", "foreground=orange background=white selection=green");
        run("RGB Color");
        setForegroundColor(255, 200, 0);
        run("Fill", "slice");
        run("Select None");
        selectWindow("grains no cracks");
        run("Create Selection");
        run("Make Inverse");
        run("Colors...", "foreground=cyan background=white selection=green");
        run("RGB Color");
        run("Fill", "slice");
        run("Select None");
        selectWindow("grains with cracks");
        imageCalculator("Add", "grains with cracks","grains no cracks");
        rename("grain-label");
        selectWindow("cracks");
        run("Create Selection");
        run("Make Inverse");
        run("Colors...", "foreground=red background=white selection=green");
        run("RGB Color");
        run("Fill", "slice");
        run("Select None");
        selectWindow("labels");
        run("Subtract Background...", "rolling=50 light sliding");
        run("Enhance Contrast...", "saturated=0.2 normalize");
        run("Add Image...", "image=grain-label x=0 y=0 opacity=30 zero");
        run("Add Image...", "image=cracks x=0 y=0 opacity=80 zero");
    run("Flatten");
    //save labelled original
    selectWindow("labels-1");
    outputfile = title + "_label";
    fullpath = output + outputfile;
    saveAs("Jpeg", fullpath);
    run("Close All");
        }
.......end of macro

<nabble_a href="test3.jpg">test3.jpgtest4.jpg
mjt
Reply | Threaded
Open this post in threaded view
|

Re: strange overlay and/or image calculator problem SOLVED

mjt
Hi all, it seems simple to me now, it was just that the ROI manager wasn't reset before starting the next image in the folder, of course!
Cheers,
Mark