Complex (for me!) macro - advice needed

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

Complex (for me!) macro - advice needed

ross oshea
Hi. I am trying to put together a macro to do batch processing of images in order to count cells, but am not very experienced in this process and can't get it to do all I want. The threshold / erode / watershed steps seem to work well for isolating individual cells, so I would like to be able to put together the steps listed below. I have added some bits from the "Record Macro" function to the BatchMeasure macro but I'm sure this is NOT the most elegant solution!

1) ask user to enter the folder containing the images and, for each image:
2) convert to 8-bit
3) ask user to enter threshold values
4) erode image
5) watershed image
6) ask user to set particle size * this is the one I can't get, so it's set at 165 - 2000 in my macro!

The following macro works except for point 6 - can anyone please let me know how to include parameters for particle size? Any improvements in the macro would also be most useful.

My pathetic Batch Measure Macro:

    dir = getDirectory("Choose a Directory ");
    list = getFileList(dir);
    start = getTime();

  width=512; height=512;
  Dialog.create ("Threshold");
  Dialog.addNumber("Lower_Threshold:", 0);
  Dialog.addNumber("Upper_Threshold:", 255);
  Dialog.show();
  Lower_Threshold = Dialog.getNumber();
  Upper_Threshold = Dialog.getNumber();

    setBatchMode(true);
    for (i=0; i<list.length; i++) {
        path = dir+list[i];
        showProgress(i, list.length);
        if (!endsWith(path,"/")) open(path);
        if (nImages>=1) {
          run("8-bit");
          setAutoThreshold();
          //run("Threshold...");
          setThreshold(Lower_Threshold, Upper_Threshold);
          run("Convert to Mask");
          run("Erode");
          run("Watershed");
          run("Analyze Particles...", "size=165-2000 circularity=0.00-1.00 show=Masks summarize");
        }
    }
Reply | Threaded
Open this post in threaded view
|

Re: Complex (for me!) macro - advice needed

Justin McGrath
You should be able to use something similar to the dialog you used
ealier in the macro.


Insert this in place of your "Analyze Particles" statement.
---Start macro bit-------------
Dialog.create("Threshold values");
Dialog.addMessage("Enter the lower and upper threshold limits");
Dialog.addNumber("Lower threshold", 165);
Dialog.addNumber("Upper threshold", 2000);
Dialog.show();

lower = Dialog.getNumber;
upper = Dialog.getNumber;

run("Analyze Particles...", "size="+lower+"-"+upper+"
circularity=0.00-1.00 show=Masks summarize");
---End macro bit ---

I don't think you need the statement setAutoThreshold() since you set
it yourself later.

If you can eliminate the user input, then this could be fully
automated.  Is there any reason that autothreshold won't work for you?
 If it doesn't, there may be some other thresholding algorithm that
does.  Also, will the particles be of very different sizes from one
image to the next?  If they're the same size, then I would skip the
user input and just use constants.

Justin


On 9/12/07, ross oshea <[hidden email]> wrote:

> Hi. I am trying to put together a macro to do batch processing of images in
> order to count cells, but am not very experienced in this process and can't
> get it to do all I want. The threshold / erode / watershed steps seem to
> work well for isolating individual cells, so I would like to be able to put
> together the steps listed below. I have added some bits from the "Record
> Macro" function to the BatchMeasure macro but I'm sure this is NOT the most
> elegant solution!
>
> 1) ask user to enter the folder containing the images and, for each image:
> 2) convert to 8-bit
> 3) ask user to enter threshold values
> 4) erode image
> 5) watershed image
> 6) ask user to set particle size * this is the one I can't get, so it's set
> at 165 - 2000 in my macro!
>
> The following macro works except for point 6 - can anyone please let me know
> how to include parameters for particle size? Any improvements in the macro
> would also be most useful.
>
> My pathetic Batch Measure Macro:
>
>     dir = getDirectory("Choose a Directory ");
>     list = getFileList(dir);
>     start = getTime();
>
>   width=512; height=512;
>   Dialog.create ("Threshold");
>   Dialog.addNumber("Lower_Threshold:", 0);
>   Dialog.addNumber("Upper_Threshold:", 255);
>   Dialog.show();
>   Lower_Threshold = Dialog.getNumber();
>   Upper_Threshold = Dialog.getNumber();
>
>     setBatchMode(true);
>     for (i=0; i<list.length; i++) {
>         path = dir+list[i];
>         showProgress(i, list.length);
>         if (!endsWith(path,"/")) open(path);
>         if (nImages>=1) {
>           run("8-bit");
>           setAutoThreshold();
>           //run("Threshold...");
>           setThreshold(Lower_Threshold, Upper_Threshold);
>           run("Convert to Mask");
>           run("Erode");
>           run("Watershed");
>           run("Analyze Particles...", "size=165-2000 circularity=0.00-1.00
> show=Masks summarize");
>         }
>     }
> --
> View this message in context: http://www.nabble.com/Complex-%28for-me%21%29-macro---advice-needed-tf4427248.html#a12629425
> Sent from the ImageJ mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Complex (for me!) macro - advice needed

ross oshea
Thanks for the reply Justin - your additions sorted the problem out, and it now works as I would like!

Just to clarify my question, I was not trying to remove user input, but to add it to the particle size argument, which you addressed for me. Basically, I want to be able to set the threshold and size limits at the start of each batch of images (since differences between sample groups can require this change, e.g. particle size would need to be larger for cells photgraphed at higher magnification).

Thanks again,
Ross