Login  Register

[Solved] Help with macro for stack segmentation

Posted by mariob on Nov 01, 2019; 6:38pm
URL: http://imagej.273.s1.nabble.com/Solved-Help-with-macro-for-stack-segmentation-tp5022617.html

Hi, everyone,

I've just started with Fiji. I've written a code for cell segmentation that
works when I run from the "Script editor" window, but not when I run from
the "Process>Batch>Macro" menu, and I was hoping someone could advise me as
to how to solve it.

I have a bunch of stacks (9 slices x 4 channels) with single-cell images,
and I want to segment the cell in each slice to take some measurements with
another portion of the script. In my case, channel 2 is used for
segmentation, and the script is supposed to go through Ch2 in each slice,
segment it and save the mask as a separate image for later use (I also like
to keep the mask as a record in case I have to check anything in the
future).

As I said, the macro bellow works fine in the script editor window (I test
it with one single file at a time) and 9 different masks are generated
corresponding to each slice. But when I run the exact same thing through
Process>Batch>Macro I get 9 identical masks corresponding to the first slice
(i.e., 9 different files with the expected names but with identical images).
If there is more than one file in the parent folder, I get one set of 9
identical masks for each original file.

{
        frames=nSlices;
        title=getTitle();
        ImgName=substring(title, 0, lengthOf(title)-4);
        dir=getDirectory("image");
        dirOut1= dir + File.separator + "mask";
        dirOut2= dir + File.separator + "tiff";
        setMinAndMax(0, 65535);
        File.makeDirectory(dirOut1);
        File.makeDirectory(dirOut2);
        saveAs("TIFF",dirOut2 + File.separator + title);
       
        //to create the masks
        run("Gaussian Blur...", "sigma=1 stack");
        run("Convert to Mask", "method=Mean background=Light calculate");
        run("Fill Holes", "stack");
        run("Open", "stack");
        for (i=0; i<frames; i+=4) {
                setSlice(i+2); //mask is based on 2nd channel
                saveAs("PNG",dirOut1 + File.separator + ImgName + "_" + i+2);
        }
        close();
}

Any input would be much appreciated.

Mario


EDIT: That was rather quick. Adding run("Duplicate...", "title=temp") right after each slice is selected and then saving and closing the duplicate solved the issue.