Login  Register

Re: YUV thresholding in a macro

Posted by Stefan Helfrich-2 on Mar 11, 2016; 11:08am
URL: http://imagej.273.s1.nabble.com/YUV-thresholding-in-a-macro-tp5015832p5015850.html

Hi Steven,

are your "100 photos to treat per day" of the same specimen over time?
If so, you should open them in a stack (via File > Import > Image
Sequence) and take care that the properties (Image > Properties...) are
set correctly (slice=1, frames > 1). This is a requirement for the macro
that I am proposing at the end of the mail.

The macro applies the auto-generated part from the Color Thresholder to
each slice of a stack and applies the Analyze Particles command that you
had provided in your mail. You can change the first line of the macro to
adapt the U threshold that is applied to the images.

All the best,
Stefan

uThreshold = 100;

// Hides intermediate images
setBatchMode(true);

// Color Thresholder 1.49v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);

Stack.getDimensions(width, height, channels, slices, frames);
originalTitle = getTitle();
originalId = getImageID();

// Apply the auto-generated macro to each slice of an image
for (j=1; j<=frames; j++) {
      selectImage(originalId);

      setSlice(j);
      run("Duplicate...", " ");
      duplicateId = getImageID();

      a=getTitle();
      call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
      run("RGB Stack");
      run("Convert Stack to Images");
      selectWindow("Red");
      rename("0");
      selectWindow("Green");
      rename("1");
      selectWindow("Blue");
      rename("2");
      min[0]=0;
      max[0]=255;
      filter[0]="pass";
      min[1]=0;
      max[1]=uThreshold;
      filter[1]="pass";
      min[2]=0;
      max[2]=255;
      filter[2]="pass";
      for (i=0;i<3;i++){
        selectWindow(""+i);
        setThreshold(min[i], max[i]);
        run("Convert to Mask");
        if (filter[i]=="stop")  run("Invert");
      }
      imageCalculator("AND create", "0","1");
      imageCalculator("AND create", "Result of 0","2");
      for (i=0;i<3;i++){
        selectWindow(""+i);
        close();
      }
      selectWindow("Result of 0");
      close();
      selectWindow("Result of Result of 0");
      rename("thresholded-"+j);
      // Colour Thresholding-------------

      run("Analyze Particles...", "size=500-Infinity display summarize");
}

run("Images to Stack", "name="+originalTitle+"-thresholded
title=thresholded use");
selectWindow(originalTitle+"-thresholded");

setBatchMode(false);

On 3/9/16 1:40 PM, asuspeg wrote:

> Hello everyone,
>
> I'm using imageJ to determine the %area of cells (microalgae) on a substrate
> (platic microplates) to follow the developement of the microalgae day after
> day.
>
> So, I take photos of my cultures day after day and use the "Color Threshold"
> tool for thresholding my pictures in terms of colour saturation (my cells
> are yellow and the background is black). For that, I use the YUV color space
> and after selecting "dark background", I adjust the "U" value at 100. After
> that, I simply use the "Analyze particles" tool to extract the %area value
> of the cells.
>
> It works very well but I have something like 100 photos to treat per day,
> and it will increase in the next weeks... That's why I would like to
> automatize the treatment by using a Macro.
>
> So, I use the tool "Record..." and do my manipulations (I click on "Macro"
> button in the "Color Threshold" window after seting up the parameters cited
> above), save the macro and install it.
>
> Unfortunately, the macro doesn't work, even with just one picture: it blocks
> at the Color Threshold step by doing a HSB threshold with default
> parameters, and few seconds after, closes the picture with the message
> "There are no image open".
>
> I don't know what's the problem... but I think it's the same problem than
> described in a previous post:
> http://imagej.1557.x6.nabble.com/unconsistency-while-using-YUV-color-threshold-in-macro-td5003443.html
>
> Thank you for your help !
>
> Here is an example of 2 pictures I use:
> https://www.dropbox.com/sh/qiueaj5n7wk3xkm/AACslOuyjMZddDfRDorgEcUla?dl=0
>
> Here is my macro:
>
> run("Color Threshold...");
> // Color Thresholder 1.49v
> // Autogenerated macro, single images only!
> min=newArray(3);
> max=newArray(3);
> filter=newArray(3);
> a=getTitle();
> call("ij.plugin.frame.ColorThresholder.RGBtoYUV");
> run("RGB Stack");
> run("Convert Stack to Images");
> selectWindow("Red");
> rename("0");
> selectWindow("Green");
> rename("1");
> selectWindow("Blue");
> rename("2");
> min[0]=0;
> max[0]=255;
> filter[0]="pass";
> min[1]=0;
> max[1]=100;
> filter[1]="pass";
> min[2]=0;
> max[2]=255;
> filter[2]="pass";
> for (i=0;i<3;i++){
>    selectWindow(""+i);
>    setThreshold(min[i], max[i]);
>    run("Convert to Mask");
>    if (filter[i]=="stop")  run("Invert");
> }
> imageCalculator("AND create", "0","1");
> imageCalculator("AND create", "Result of 0","2");
> for (i=0;i<3;i++){
>    selectWindow(""+i);
>    close();
> }
> selectWindow("Result of 0");
> close();
> selectWindow("Result of Result of 0");
> rename(a);
> // Colour Thresholding-------------
> run("Analyze Particles...", "size=500-Infinity display summarize");
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/YUV-thresholding-in-a-macro-tp5015832.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
Stefan Helfrich, M.Sc.
Bioimaging Center (L931)
University of Konstanz

PO Box 604
78457 Konstanz
Germany

Tel +49-7531-884666
Fax +49-7531-884005

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