Open an ImageSequence in a Macro File

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

Open an ImageSequence in a Macro File

Xeal
Hi People,

I have the following folder structure:

There is a Folder, called "SourceFolder" which contains subfolders. Each Subfolder is called "2,200mm", "2,220mm", "2,240mm", etc.
In each Subfolder, there are a couple of images (~50).

My macro should do the folloing:
- Chose the Source directory
- Create a List, which contains all subfolders
- Do Image Processing for all images in each subfolder

I have successfully built up a macro, which processes all images in one subfolder, but the macro for all subfolders doesn't work.

The code looks like this so far:

SourceDir = getDirectory("Choose source directory, which contains the folders with measurements");
FolderList = getFileList(SourceDir);
File.makeDirectory(SourceDir+"withoutStaticBackground");
     
for (k=0; k<FolderList.length; k++) {
       
 if (endsWith(FolderList[k], "/")) {
     File.makeDirectory(SourceDir+"withoutStaticBackground"+File.separator+FolderList[k]);
        }

        print(SourceDir+FolderList[k]); //The name of the folder is printed
        run("Image Sequence...", "open=[SourceDir+FolderList[k]] sort"); //but the images will not be opend

     }


I have inserted the "Print-Command", to check if the file path is correct. If I run this macro, I don't get any error-message. But no image is opend.

Can someone help me ?

Best Regards
Holger


Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

dscho
Hi Xeal,

On Tue, 11 Oct 2011, Xeal wrote:

> My macro should do the folloing:
> - Chose the Source directory
> - Create a List, which contains all subfolders
> - Do Image Processing for all images in each subfolder

There is a tutorial on the Fiji Wiki to do almost exactly that (it works
on files, not subdirectories):

http://fiji.sc/How_to_apply_a_common_operation_to_a_complete_directory

>         run("Image Sequence...", "open=[SourceDir+FolderList[k]] sort");

Here, you specified the _verbatim_ string "open=[SourceDir+FolderList[k]]
sort". So ImageJ will look for a folder called "SourceDir+FolderList[k]"
which does not exist.

Use this instead:

        "open["+SourceDir+FolderList[k]+"] sort"

i.e. concatenate a string, two variables and another string.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

Xeal
Johannes Schindelin wrote
>         run("Image Sequence...", "open=[SourceDir+FolderList[k]] sort");

Here, you specified the _verbatim_ string "open=[SourceDir+FolderList[k]]
sort". So ImageJ will look for a folder called "SourceDir+FolderList[k]"
which does not exist.

Use this instead:

        "open["+SourceDir+FolderList[k]+"] sort"

i.e. concatenate a string, two variables and another string.

Thanks for your quick answer.
If I do it like that, I have to chose the different folders manually. So my analysis won't run absoluteley automatic.

Is there a way to avoid this ?
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

dpoburko
In reply to this post by Xeal
Hi Holger,

On 10/11/2011 9:38 AM, Xeal wrote:

> Hi People,
>
> I have the following folder structure:
>
> There is a Folder, called "SourceFolder" which contains subfolders. Each
> Subfolder is called "2,200mm", "2,220mm", "2,240mm", etc.
> In each Subfolder, there are a couple of images (~50).
>
> My macro should do the folloing:
> - Chose the Source directory
> - Create a List, which contains all subfolders
> - Do Image Processing for all images in each subfolder
>
> I have successfully built up a macro, which processes all images in one
> subfolder, but the macro for all subfolders doesn't work.
>
> The code looks like this so far:
>
> SourceDir = getDirectory("Choose source directory, which contains the
> folders with measurements");
> FolderList = getFileList(SourceDir);
> File.makeDirectory(SourceDir+"withoutStaticBackground");
>
> for (k=0; k<FolderList.length; k++) {
>
>   if (endsWith(FolderList[k], "/")) {
>
> File.makeDirectory(SourceDir+"withoutStaticBackground"+File.separator+FolderList[k]);
>          }


>          print(SourceDir+FolderList[k]); //The name of the folder is printed
>          run("Image Sequence...", "open=[SourceDir+FolderList[k]] sort");
> //but the images will not be opend
It also looks like the two lines above should be included in your "if"
braces, whereas they may now falsely execute on items of the folderList
that are not folders.

>       }
>
>
> I have inserted the "Print-Command", to check if the file path is correct.
> If I run this macro, I don't get any error-message. But no image is opend.
>
> Can someone help me ?
>
> Best Regards
> Holger
>
>
>
>
> --
> View this message in context: http://imagej.588099.n2.nabble.com/Open-an-ImageSequence-in-a-Macro-File-tp6881682p6881682.html
> Sent from the ImageJ mailing list archive at Nabble.com.
Good Luck,
Damon

--
Post Doctoral Fellow
Tsien Lab, Molecular&  Cellular Physiology
Stanford University
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

Xeal
dpoburko wrote
> It also looks like the two lines above should be included in your "if"
> braces, whereas they may now falsely execute on items of the folderList
> that are not folders.
Thanks :)

This Problem is still not solved
Xeal wrote
>If I do it like that, I have to chose the different folders manually. So my analysis won't run absoluteley >  automatic.

Is there a way to avoid this ?
I mean, it doesn't make sense that I construct a list, which contains all subfolders, but sill, I have to chose an image from each subfolder..
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

albertoduina@virgilio.it
In reply to this post by Xeal
Hi to everybody.
I have tried to solve the Xeal problem:


// ----------------------------
// Main macro body
//-----------------------------



        SourceDir = getDirectory("Choose source directory, which contains the  
folders with measurements");
        FolderList = getFileList(SourceDir);
        File.makeDirectory(SourceDir+"withoutStaticBackground");
       
        for (k=0; k<FolderList.length; k++) {
                 if (endsWith(FolderList[k], "/")) {
      File.makeDirectory(SourceDir+"withoutStaticBackground"+File.separator+FolderList[k]);
        }
        }
        processFolder(SourceDir);





// --------------------------------------------
// copied from BatchProcessFolders
// --------------------------------------------
function processFolder(dir) {
        list = getFileList(dir);
        n=0;
        for (i=0; i<list.length; i++) {
                if (endsWith(list[i], "/"))
                        processFolder(""+dir+list[i]);
                else {
                       
                        n++;
                        path = dir+list[i];
                         if (n==1) {
                                print("sequence "+path);
                                run ("Image Sequence...", "open=[path] sort");
                        }
  }
        }
  }

//---- END ------


Ciao
Alberto Duina
Reply | Threaded
Open this post in threaded view
|

Re: Open an ImageSequence in a Macro File

Xeal
Thank you ver much Alberto, this helps a lot.
Your solution works, but there is one little problem. Here is your modified code:


macro "Remove static background Action Tool - C000T2f20S" {

SourceDir = getDirectory("Choose source directory, which contains the folders with measurements in different cell depths");
FolderList = getFileList(SourceDir); //erstellt die Liste "list", die alle Dateinamen enth?lt. Die Elemente der Liste stehen an stelle i=0 bis i = list.length
File.makeDirectory(SourceDir+"withoutStaticBackground");

setBatchMode(true);

for (k=0; k<FolderList.length; k++) {

        if (endsWith(FolderList[k], "/")) {
        File.makeDirectory(SourceDir+"withoutStaticBackground"+File.separator+FolderList[k]);
        }

  }

  processFolder(SourceDir);



// --------------------------------------------
// Process folder to remove static background
// --------------------------------------------
function processFolder(dir) {
        list = getFileList(dir);
        n=0;
        for (i=0; i<list.length; i++) {
                if (endsWith(list[i], "/"))
                        processFolder(""+dir+list[i]);
                else {
                       
                        n++;
                        path = dir+list[i]; // in dieser Schleife sind list[i] die Dateinamen der Bilder in einem Ordner
                       
                         if (n==1) {
                          print("sequence "+path);
                                run ("Image Sequence...", "open=[path] sort");

                          rename("Stack");
                                run("Z Project...", "start=1 stop=100 projection=[Min Intensity]"); //Generate a new image with the static background
                                idz = getImageID();
                                imageCalculator("Subtract stack", "Stack","MIN_Stack"); // Remove static background from all images in the stack
                                selectImage(idz); // close image of static background
                                close(); // close image of static background

                                run("Stack to Images"); //disassemble the stack to get single images with reduced static background

                                while(nImages>0) {

                                // get image IDs of all open images
                                ids=newArray(nImages); //new Array, containing the ids of all opened images
                                for (l=0;l<nImages;l++) {
                                selectImage(l+1);
                                rename(l+1);
                                ids[l]=getImageID;
                                }

                                // process all open images
                                for (l=0;l<ids.length;l++) {
                                showProgress(l+1, ids.length);

                                ////////////////////////////////
  //save images start
  ////////////////////////////////
                                selectImage(ids[l]);
                                if (l<10)
                                j="0"+l;
                                else j=l;
                //saveAs("Tiff", dir+"withoutStaticBackground"+File.separator++j+"_processed");
                close();
                ////////////////////////////////
                                //save images end
                                ////////////////////////////////


       
                        }
               
                }
       

                               
                               

                               
       }
  }
        }
  }

//---- END ------
   
       

       


setBatchMode(false);
print("Static background has been removed and images have been saved.");
}





The Problem is related to the saving process. Before your solution I used this code to save the images:
saveAs("Tiff", SourceDir+"withoutStaticBackground"+File.separator+FolderList[k]+j+"_processed");

Due to the use of the function "processFolder(dir);" I should replace "SourceDir" by "dir".
In the function I can't use FolderList[k] any more, to define the path to save my images, because it isn't defined in the function any more. I also can't use "list[i], because this array contains the filenames, not the foldernames.

What would be the best way to get around this problem ? Should I call "processFolder(SourceDir);" within the first if-statement in the main macro and define the processFolder-function with a second argument (FolderList) ? I tried this, but it didn't work.

Thanks a lot
Holger Schweinfurth