make selection, run macro, and open next file in directory

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

make selection, run macro, and open next file in directory

Andrew Sanchez
This is what I want my macro to do

1) prompt me to select a directory
2) open the first file in that directory
3) wait for me to draw a circle
4) run the macro
5) open the next file in the selected directory
6) repeat 3) - 5)

Currently I need to manually open images one at a time, run the macro on them individually, then open another file and repeat.  I thought the "Open Next" command (Ctrl + Shift + O) would do the trick, but after running this macro, I get a message saying “Directory information for ‘file_name.JPG’ not found.”

Here is the macro I am currently working with:


run("Set Measurements...", "area display redirect=None decimal=2");
run("Measure");
run("Crop");
setBackgroundColor(255, 255, 255);
run("Clear Outside");


// Color Thresholder 1.48v
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=38;
max[0]=127;
filter[0]="pass";
min[1]=27;
max[1]=190;
filter[1]="pass";
min[2]=18;
max[2]=254;
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=5-Infinity summarize");
Reply | Threaded
Open this post in threaded view
|

Re: make selection, run macro, and open next file in directory

fabrice senger-2
Hi there,

you could use a loop and at the point you have to make a drawing or
selection or whatever use the "wait for user" command.

For the loop you can have a look at example macros and the "wait for user"
is described in macro functions.

Cheers,

Fabrice.

2015-06-25 22:22 GMT+02:00 Andrew Sanchez <[hidden email]>:

> This is what I want my macro to do
>
> 1) prompt me to select a directory
> 2) open the first file in that directory
> 3) wait for me to draw a circle
> 4) run the macro
> 5) open the next file in the selected directory
> 6) repeat 3) - 5)
>
> Currently I need to manually open images one at a time, run the macro on
> them individually, then open another file and repeat.  I thought the "Open
> Next" command (Ctrl + Shift + O) would do the trick, but after running this
> macro, I get a message saying “Directory information for ‘file_name.JPG’
> not
> found.”
>
> Here is the macro I am currently working with:
>
>
> run("Set Measurements...", "area display redirect=None decimal=2");
> run("Measure");
> run("Crop");
> setBackgroundColor(255, 255, 255);
> run("Clear Outside");
>
>
> // Color Thresholder 1.48v
> // Autogenerated macro, single images only!
> min=newArray(3);
> max=newArray(3);
> filter=newArray(3);
> a=getTitle();
> run("HSB Stack");
> run("Convert Stack to Images");
> selectWindow("Hue");
> rename("0");
> selectWindow("Saturation");
> rename("1");
> selectWindow("Brightness");
> rename("2");
> min[0]=38;
> max[0]=127;
> filter[0]="pass";
> min[1]=27;
> max[1]=190;
> filter[1]="pass";
> min[2]=18;
> max[2]=254;
> 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=5-Infinity summarize");
>
>
>
>
> --
> View this message in context:
> http://imagej.1557.x6.nabble.com/make-selection-run-macro-and-open-next-file-in-directory-tp5013306.html
> Sent from the ImageJ mailing list archive at 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: make selection, run macro, and open next file in directory

Anderson, Charles (DNR)
In reply to this post by Andrew Sanchez
Andrew,

1) I placed your color thresholding macro inside the loop of the macro I had previously sent you, then removed the previous thresholding code. I think it does what you want (except that you draw the circle AFTER thresholding; see point #2).

2) You want % green calculated as the green area inside the circle divided by the total area inside your circle, not divided by the total area of the image, thus your macro was not giving you the correct answer.  If you draw the circle (and clear outside) after thresholding, then the % green is based on the area of the active ROI circle.

3) If you must see the color image to place the circle/ellipse with sufficient accuracy, you will have record the circle ROI you identify on the color image, then apply it later to the thresholded image before analyzing particles. There have been recent threads on applying ROI to another image, but I have not learned the details.

4) "Open Next" did not work as you expected after you ran your macro because the run("HSB Stack") command closes the original and replaces it with a stack given the same name.  When the original is closed, the connection to what you consider previous or next is lost.  But if you begin your macro with a line to 'run("Duplicate...", "");' the original will remain open, the rest of the macro will work on the copy, and the "Open Next" will work.

Charles

// CircleVeg2.ijm
run("Set Measurements...", "area redirect=None decimal=2");
setBackgroundColor(255, 255, 255);
dir1 = getDirectory("Choose Source Directory ");
list = getFileList(dir1);
//setBatchMode(true);
for (k = 0; k<list.length; k++) {
        showProgress(k+1, list.length);
        open(dir1+list[k]);

        // Color Thresholder 1.48v    // Your macro here
        // Autogenerated macro, single images only!
        min=newArray(3);
        max=newArray(3);
        filter=newArray(3);
        a=getTitle();
        run("HSB Stack");
        run("Convert Stack to Images");
        selectWindow("Hue");
        rename("0");
        selectWindow("Saturation");
        rename("1");
        selectWindow("Brightness");
        rename("2");
        min[0]=38;
        max[0]=127;
        filter[0]="pass";
        min[1]=27;
        max[1]=190;
        filter[1]="pass";
        min[2]=18;
        max[2]=254;
        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-------------

        setTool("oval");
        waitForUser("Select areas","Hold shift to draw elipse, then click 'Ok'");
        // Draw ellipse using oval tool holding shift down
        run("Clear Outside");
  run("Analyze Particles...", "size=5-Infinity summarize");
        selectWindow(list[k]);
        close();
}

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Andrew Sanchez
Sent: Thursday, June 25, 2015 3:23 PM
To: [hidden email]
Subject: make selection, run macro, and open next file in directory

This is what I want my macro to do

1) prompt me to select a directory
2) open the first file in that directory
3) wait for me to draw a circle
4) run the macro
5) open the next file in the selected directory
6) repeat 3) - 5)

Currently I need to manually open images one at a time, run the macro on them individually, then open another file and repeat.  I thought the "Open Next" command (Ctrl + Shift + O) would do the trick, but after running this macro, I get a message saying “Directory information for ‘file_name.JPG’ not found.”

Here is the macro I am currently working with:

...

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