Feret'Dimeter and loops for otoliths?

Hi Everybody !

I am starting to whrite macro with “fiji” to extract the feret’diameter of otoliths pictures (max length).
I manage to extract this informations for one picture of an otolith but i fail to make a loop on a folder that contain all the over pictures.

I think the probleme is in my loop and when i save the working directory … Do you have any idear ?
Here is the code :

open("C:\\Users\\Xavier\\Desktop\\xavier\\image0001.bmp"); 
list = getFileList(getDirectory("image")); 
for (i = 0; i < list.length; i++)
{
open(list[i]);
name = getTitle();
  index = lastIndexOf(name, ".");
  if (index!=-1) name = substring(name, 0, index);
run("8-bit");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(0, 124);
setOption("BlackBackground", false);
run("Convert to Mask");
//run("Close");
run("Set Measurements...", "area centroid fit feret's display redirect=None decimal=3");

run("Analyze Particles...", "display");

a = getWidth();
b = getHeight();

saveAs("Results", getDirectory("image") + a + "x" + b + "_" + name +".xls");
run("Close");
}

What does getDirectory("image") print for you? Does it have a trailing slash character? Try using print statements to figure out what is happening. E.g., instead of:

saveAs("Results", getDirectory("image") + a + "x" + b + "_" + name +".xls");

Try:

dir = getDirectory("image");
name = a + "x" + b + "_" + name +".xls";
path = dir + name;
print("Saving results to:");
print("\tdir = " + dir);
print("\tname = " + name);
print("\tpath = " + path);
saveAs("Results", path);

This is a nice and simple way of debugging.

Thank you for your help ctrueden :wink: !!!

When i print(getDirectory(“image”)), the log window show the good path :
C:\Users\Xavier\Desktop\xavier\

When i run the loop, it stop end write it :
“Undefin variable in line 2 for (i = 0; i < list.length; i++)”

I would like to verify every line of code since the start, but i dont exactly how to do. (its like that i do with R)
For exemple, how can i see if the “getFileList()” does work ? to print it or with an over possibility ?
How it’s possible to create “text” with variable like a and b ? and be sure that is considered as text.

An over little question, is there a fuction to call that del all what appened before like deletting variables created for strating of zero.

It’s true i ask too many questions … :wink:

Thank you !

I am not sure I understand the question. But if you want to verify the value of list, you can do so:

list = getFileList(getDirectory("image"));
print(list.length);

Note that because list is an array, you cannot write print(list); (the macro interpreter will give an error), but you can print list.length to get the number of elements.

One way to make a variable be a string is to append to the empty string. Here is an example macro:

num = 5;
word = "" + num;
if (num < 10) print("num < 10");
if (word < 10) print("word < 10");

It should print num < 10 but not word < 10 when executed.

Not to my knowledge. At least, the built-in macro functions do not list anything. But the macro should start clear every time you run it, unpolluted by previous executions.

You could also look into other supported scripting languages, which might (I’m not sure) have a function like that.

Thanks you Curtis :wink: it help me a lot !
Now i just have to make the loop work, i am tryng !
Here is the new code :

open("C:\\Users\\Morgane\\Desktop\\xavier\\image0001.bmp"); // opening the first picture
dir=getDirectory("image") // take location of the first picture
list = getFileList(getDirectory("image")); // make a list of pictures
print(list.length); // check the list
name = getTitle(); //taking the name of the picture open
  index = lastIndexOf(name, "."); // Modify the name
  if (index!=-1) name = substring(name, 0, index); // Modify the name
a = getWidth(); //get the width
b = getHeight(); //get the height just for to know what scale i will use
name1 = name + "_" + a + "x" + b; // create the name for the ".xls" file
print(name1); // verify the name IT works

///////////////////////////////////////////////////////////////////
//// START of the loop
///////////////////////////////////////////////////////////////////

//for (i = 0; i < list.length; i++)
//{
//open(list[i]);

run("8-bit");
setAutoThreshold("Default");
setOption("BlackBackground", false);
run("Convert to Mask");
run("Set Measurements...", "area centroid fit feret's display redirect=None decimal=3");
run("Analyze Particles...", "display");
path = dir + name;
saveAs("Results", path + ".xls");
//saveAs("Results", name);
run("Close");
//}

Hi Curtis !!!

Now i manage to do a loop on all the pictures, to create an xls files for eatch pictures with all the informations i need, so do you know a way to save all informations in only one file (xls or csv) ?

open("C:\\Users\\Morgane\\Desktop\\xavier\\image0001.bmp"); // opening the first picture
dir=getDirectory("image") // take location of the first picture
list = getFileList(getDirectory("image")); // make a list of pictures
print(list.length); // check the list
name = getTitle(); //taking the name of the picture open
  index = lastIndexOf(name, "."); // Modify the name
  if (index!=-1) name = substring(name, 0, index); // Modify the name
a = getWidth(); //get the width
b = getHeight(); //get the height just for to know what scale i will use
name1 = name + "_" + a + "x" + b; // create the name for the ".xls" file
print(name1); // verify the name IT works
run("Close");

///////////////////////////////////////////////////////////////////
//// START of the loop
///////////////////////////////////////////////////////////////////

for (i = 0; i < list.length; i++)
{
open(dir + list[i]);
name = getTitle(); //taking the name of the picture open
  index = lastIndexOf(name, "."); // Modify the name
  if (index!=-1) name = substring(name, 0, index); // Modify the name
a = getWidth(); //get the width
b = getHeight(); //get the height just for to know what scale i will use
name1 = name + "_" + a + "x" + b; // create the name for the ".xls" file
print(name1); // verify the name IT works
run("8-bit");
setAutoThreshold("Default");
setOption("BlackBackground", false);
run("Convert to Mask");
run("Set Measurements...", "area centroid fit feret's display redirect=None decimal=3");
run("Analyze Particles...", "display");
path = dir + name;
saveAs("Results", path + ".xls");
//saveAs("Results", name);
run("Close");
}

macro "Close All Windows" { 
      while (nImages>0) { 
          selectImage(nImages); 
          close(); 
      } 
  } 

/////////////////////////////////////////////////////////////////////////////////////

One way could be to save each batch of results to a temp file, read it back in using File.openAsRawString(tmpFile, 10000000), then append the string to your master file using File.append.

Hi Curtis !

I found a way to save all files “.csv” created in one file, using R. I do not found an easiest way in imageJ script but It works. Here is the code :wink:

setwd("/Users/otolitho/Desktop/GOU_OG") # set the working directory
names<-list.files() # make a list of files
names # check names
namescsv<-names[grep(".csv",names)] # selecting ".csv" files
namescsv # check
L=list() # creating a new list without informations
#### Loop for opening all data.frame and put them into a place of the list
for (i in 1:length(namescsv)){
L[[i]] <- read.csv(namescsv[i],sep=",")
}
L # check the data.frame of the list
Synthese<-do.call("rbind",L) # joining al data.frame made with the same number of columns
Synthese # check the results