Macro for beginners

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

Macro for beginners

Toufic El Arnaout
Hello everyone,I am new to ImageJ and Fiji. I know well now how to use basic macro for batch mode for all basic analysis functions, but cases with variables to read and save. Please help me correct the macro below in red (sorry for the long email).. it will be needed for now and future similar ones.
a- I have images in let's say folder A in C:.b- I want those images to have their background removed using image "background.tif".c- Then have the output image for each saved in folder B in C:.d- I might be wrong in some cases, but the setBatchMode was probably added below in a wrong way because I found it in tutorials. I may also have the input and output entries wrongly placed. In addition, I may not need the images to be all opened after processing...e- The sixth line entry, called action is wrong as the software says (I also found it here: http://fiji.sc/How_to_apply_a_common_operation_to_a_complete_directory)
Furthermore, let me know how to call the files in this macro. I think this macro assumes that the file name is 0, and next one is 1, 2, 3 etc? What if the file name in stupid cases for example is 1_2014-10-31-153821-0200? and the next one is *-0201, etc (because otherwise, simply renaming 1000 files in Windows by selecting all will make them called 1, 1 (2), 1 (3), etc.... and I assume will be complicated for me to understand for i, i + 1, i + 2 rule...toufic


input = "C:\\A\\";output = "C:\\B\\";
setBatchMode(true);list = getFileList(input);for (i = 0; i < list.length; i++) action(input, output, list[i]);
open(input + filename);open("C:\\background.tif");selectWindow(filename);run("Subtract Background...", "rolling=40 light");imageCalculator("Subtract create", "background.tif", input);selectWindow("Result of background.tif");saveAs("jpeg", output);
setBatchMode(false);
         
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Macro for beginners

ERIC
Hi Toufic,
Here is what I propose. It may be clear enough to answer your questions.
I added few remarks to clarify.
Hope it helps !!!

Eric


// Gets the name of directories with file separator "\"

input=getDirectory("Choose an Input Folder");
output=getDirectory("Choose an Output folder");
Bckgrd=getDirectory("Choose a Background folder");

// goes faster without dispalying images
setBatchMode(true);


// Open the Bcgrd Image
open(Bckgrd + "background.tif");

// array of names of files in alphabetical order (no needs for specific
names)
list = getFileList(input);

//needs open and close brackets to define the input folder loop
for (i = 0; i < list.length; i++) {
open(input + list[i]);
title1=getTitle();
imageCalculator("Subtract create",list[i] , "background.tif");
title2=getTitle();

// Save the file with the name "Bckg_"+ original title
saveAs("jpeg", output+"Bckg_"+title2);

// close the images
close();
selectWindow(title1);
close();
} // end of  loop

selectWindow( "background.tif");
close();

Le 05/11/2014 20:36, Toufic El Arnaout a écrit :

> Hello everyone,I am new to ImageJ and Fiji. I know well now how to use basic macro for batch mode for all basic analysis functions, but cases with variables to read and save. Please help me correct the macro below in red (sorry for the long email).. it will be needed for now and future similar ones.
> a- I have images in let's say folder A in C:.b- I want those images to have their background removed using image "background.tif".c- Then have the output image for each saved in folder B in C:.d- I might be wrong in some cases, but the setBatchMode was probably added below in a wrong way because I found it in tutorials. I may also have the input and output entries wrongly placed. In addition, I may not need the images to be all opened after processing...e- The sixth line entry, called action is wrong as the software says (I also found it here: http://fiji.sc/How_to_apply_a_common_operation_to_a_complete_directory)
> Furthermore, let me know how to call the files in this macro. I think this macro assumes that the file name is 0, and next one is 1, 2, 3 etc? What if the file name in stupid cases for example is 1_2014-10-31-153821-0200? and the next one is *-0201, etc (because otherwise, simply renaming 1000 files in Windows by selecting all will make them called 1, 1 (2), 1 (3), etc.... and I assume will be complicated for me to understand for i, i + 1, i + 2 rule...toufic
>
>
> input = "C:\\A\\";output = "C:\\B\\";
> setBatchMode(true);list = getFileList(input);for (i = 0; i < list.length; i++) action(input, output, list[i]);
> open(input + filename);open("C:\\background.tif");selectWindow(filename);run("Subtract Background...", "rolling=40 light");imageCalculator("Subtract create", "background.tif", input);selectWindow("Result of background.tif");saveAs("jpeg", output);
> setBatchMode(false);
>        
> --
> 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: Macro for beginners

Kenton Arkill
I'd add one small thing.. often computers add annoying little files like
thumbs.db and such, so add:
if(endswith(".tif"){   // if they are tifs
open(input + list[i]);
etcetc
}

On 5 November 2014 21:23, Eric Denarier <[hidden email]>
wrote:

> Hi Toufic,
> Here is what I propose. It may be clear enough to answer your questions. I
> added few remarks to clarify.
> Hope it helps !!!
>
> Eric
>
>
> // Gets the name of directories with file separator "\"
>
> input=getDirectory("Choose an Input Folder");
> output=getDirectory("Choose an Output folder");
> Bckgrd=getDirectory("Choose a Background folder");
>
> // goes faster without dispalying images
> setBatchMode(true);
>
>
> // Open the Bcgrd Image
> open(Bckgrd + "background.tif");
>
> // array of names of files in alphabetical order (no needs for specific
> names)
> list = getFileList(input);
>
> //needs open and close brackets to define the input folder loop
> for (i = 0; i < list.length; i++) {
> open(input + list[i]);
> title1=getTitle();
> imageCalculator("Subtract create",list[i] , "background.tif");
> title2=getTitle();
>
> // Save the file with the name "Bckg_"+ original title
> saveAs("jpeg", output+"Bckg_"+title2);
>
> // close the images
> close();
> selectWindow(title1);
> close();
> } // end of  loop
>
> selectWindow( "background.tif");
> close();
>
> Le 05/11/2014 20:36, Toufic El Arnaout a écrit :
>
>  Hello everyone,I am new to ImageJ and Fiji. I know well now how to use
>> basic macro for batch mode for all basic analysis functions, but cases with
>> variables to read and save. Please help me correct the macro below in red
>> (sorry for the long email).. it will be needed for now and future similar
>> ones.
>> a- I have images in let's say folder A in C:.b- I want those images to
>> have their background removed using image "background.tif".c- Then have the
>> output image for each saved in folder B in C:.d- I might be wrong in some
>> cases, but the setBatchMode was probably added below in a wrong way because
>> I found it in tutorials. I may also have the input and output entries
>> wrongly placed. In addition, I may not need the images to be all opened
>> after processing...e- The sixth line entry, called action is wrong as the
>> software says (I also found it here: http://fiji.sc/How_to_apply_a_
>> common_operation_to_a_complete_directory)
>> Furthermore, let me know how to call the files in this macro. I think
>> this macro assumes that the file name is 0, and next one is 1, 2, 3 etc?
>> What if the file name in stupid cases for example is
>> 1_2014-10-31-153821-0200? and the next one is *-0201, etc (because
>> otherwise, simply renaming 1000 files in Windows by selecting all will make
>> them called 1, 1 (2), 1 (3), etc.... and I assume will be complicated for
>> me to understand for i, i + 1, i + 2 rule...toufic
>>
>>
>> input = "C:\\A\\";output = "C:\\B\\";
>> setBatchMode(true);list = getFileList(input);for (i = 0; i < list.length;
>> i++) action(input, output, list[i]);
>> open(input + filename);open("C:\\background.tif");selectWindow(filename);run("Subtract
>> Background...", "rolling=40 light");imageCalculator("Subtract create",
>> "background.tif", input);selectWindow("Result of
>> background.tif");saveAs("jpeg", output);
>> setBatchMode(false);
>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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