[Solved] Help with macro for stack segmentation

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Solved] Help with macro for stack segmentation

mariob
This post was updated on .
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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with macro for stack segmentation

Peter Haub
Hi Mario,

I have created a stack
     newImage("Untitled", "32-bit black", 200, 200, 12);
     run("Invert", "stack");

and draw some black objects on slice 2, 6 and 10.

I copied your macro code into the Process>Batch>Macro window 1:1
and run it ('Input path' = location of the created stack).

Here ('old' ImageJ 1.49u) it works as expected for one or more images in
the input folder.
For each image the different mask images (3 in my case) are stored in
the subfolder 'mask'.

Maybe you can run this as a test.

Regards,
Peter

On 01.11.2019 19:38, mariob wrote:

> 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
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Help with macro for stack segmentation

mariob
Hi, Peter,

Thanks for the response, but I managed to circumvent the issue by including
a Duplicate function before saving each mask.

Mario



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html