how to manipulate directory names

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

how to manipulate directory names

Mohamed Tleis
Dear All,

The following macro code process all my images and store them into one
folder, but what I want is to keep sub-folders(i.e. images in subfolder of
dir, should be in subfolder of dir2), how can this be accomplished?

[code]
dir = getDirectory("Choose a Directory of Images ");
dir2 = getDirectory("Choose a Directory to Save images To");
listFiles(dir);

function listFiles(dir) {
     list = getFileList(dir);
     for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/"))
       {
    listFiles(""+dir+list[i]);
}
        else {
if (endsWith(list[i], ".tif" ) )
{
open(dir+list[i]);
run("Sigma Filter Plus", "radius=2 use=7 minimum=0.9 outlier");
setThreshold(5, 255);
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
saveAs("tiff", dir2+"BWS_"+list[i]);
close();
}
        }

     }
  }
[/code]
Reply | Threaded
Open this post in threaded view
|

Re: how to manipulate directory names

Krs5
Dear Mohammed,

You could try something like:

[code]
dir1 = getDirectory("Choose a Directory of Images ");
list = getFileList(dir1);
dir2 = getDirectory("Choose a Directory to Save images To");
setBatchMode(true);
for (i=0; i<list.length; i++) {
        if (File.isDirectory(dir1+list[i])){
                dir3 = dir1+list[i];
                list3 = getFileList(dir3);
                dir4 = dir2+list[i];
                File.makeDirectory(dir4);
                for (j=0; j<list3.length; j++)
                        analyse(dir3, dir4, list3, j);
        }else analyse(dir1, dir2, list, i);
}
       
               
                       
function analyse(dir, dir2, list, i) {
        open(dir+list[i]);
        run("Sigma Filter Plus", "radius=2 use=7 minimum=0.9 outlier");
        setThreshold(5, 255);
        run("Convert to Mask");
        run("Fill Holes");
        run("Watershed");
        saveAs("tiff", dir2+"BWS_"+list[i]);
        close();
}
       
[code]

To speed up the process I added setBatchMode(true); at line 4.

Did not fully tested it but hope it works

Kees


-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Mohammed Tlais
Sent: 23 November 2011 12:36
To: [hidden email]
Subject: how to manipulate directory names

Dear All,

The following macro code process all my images and store them into one
folder, but what I want is to keep sub-folders(i.e. images in subfolder of
dir, should be in subfolder of dir2), how can this be accomplished?

[code]
dir = getDirectory("Choose a Directory of Images ");
dir2 = getDirectory("Choose a Directory to Save images To");
listFiles(dir);

function listFiles(dir) {
     list = getFileList(dir);
     for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/"))
       {
    listFiles(""+dir+list[i]);
}
        else {
if (endsWith(list[i], ".tif" ) )
{
open(dir+list[i]);
run("Sigma Filter Plus", "radius=2 use=7 minimum=0.9 outlier");
setThreshold(5, 255);
run("Convert to Mask");
run("Fill Holes");
run("Watershed");
saveAs("tiff", dir2+"BWS_"+list[i]);
close();
}
        }

     }
  }
[/code]