Writing maxima positions in a file

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

Writing maxima positions in a file

foster12
Hello !
 
I would like to find the maxima of an array of images.(any image is black with two spots bright. All in gray scale).

So I've compiled this Macro***. In 2 words, you select a directory in which there are about 300 TIFF files and the macro gives me the maxima positions.

Now I would like that for ANY image, the position of the two maxima were saved in a ONE only text file. For example in a .xls . How could I do?

Is there a method for calculate (ONLY if the maxima are TWO) directly the distance between this two points and finally get ONE file text with all the distances?

Best Regards

Enrico Punzo


***
     dir = getDirectory("Choose a Directory ");
   setBatchMode(true);
   count = 0;
   countFiles(dir);
   n = 0;
   processFiles(dir);
   //print(count+" files processed");
   
   function countFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              countFiles(""+dir+list[i]);
          else
              count++;
      }
  }

   function processFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              processFiles(""+dir+list[i]);
          else {
             showProgress(n++, count);
             path = dir+list[i];
             processFile(path);
          }
      }
  }

  function processFile(path) {
       if (endsWith(path, ".tif")) {
           open(path);
           File.open(result.txt)
           run("Find Maxima...", "noise=25 output=[List] ");
           File.append(row, getDirectory("home")+"results.xls");
           save(path);
           close();
      }
  }
Reply | Threaded
Open this post in threaded view
|

Re: Writing maxima positions in a file

Michael Schmid
Hi Enrico,

the command you are missing:
http://rsb.info.nih.gov/ij/developer/macro/functions.html#getResult

e.g. roughly like this:

if (nResults() == 2) {
   x0=getResult("X", 0);
   y0=getResult("Y", 0);
   x1=getResult("X", 1);
   y1=getResult("Y", 1);
   dist = sqrt((x0-x1)*(x0-x1) - (y0-y1)*(y0-y1));
   File.append(path+","+dist, getDirectory("home")+"allDistances.csv");
}

I see no sense in opening 'results.txt' in your macro:
You don't assign it to a variable to use it later 'print' and 'close'  
statements, and you don't close it, which you should always do with  
open files.
See the demo macro for how to use it:
   http://rsb.info.nih.gov/ij/macros/SaveTextFileDemo.txt


Michael
________________________________________________________________

On 13 Dec 2011, at 18:14, Enrico Punzo wrote:

> Hello !
>
> I would like to find the maxima of an array of images.(any image is  
> black with two spots bright. All in gray scale).
>
> So I've compiled this Macro***. In 2 words, you select a directory  
> in which there are about 300 TIFF files and the macro gives me the  
> maxima positions.
>
> Now I would like that for ANY image, the position of the two maxima  
> were saved in a ONE only text file. For example in a .xls . How  
> could I do?
>
> Is there a method for calculate (ONLY if the maxima are TWO)  
> directly the distance between this two points and finally get ONE  
> file text with all the distances?
>
> Best Regards
>
> Enrico Punzo
>
>
> ***
>      dir = getDirectory("Choose a Directory ");
>    setBatchMode(true);
>    count = 0;
>    countFiles(dir);
>    n = 0;
>    processFiles(dir);
>    //print(count+" files processed");
>
>    function countFiles(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>           if (endsWith(list[i], "/"))
>               countFiles(""+dir+list[i]);
>           else
>               count++;
>       }
>   }
>
>    function processFiles(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>           if (endsWith(list[i], "/"))
>               processFiles(""+dir+list[i]);
>           else {
>              showProgress(n++, count);
>              path = dir+list[i];
>              processFile(path);
>           }
>       }
>   }
>
>   function processFile(path) {
>        if (endsWith(path, ".tif")) {
>            open(path);
>            File.open(result.txt)
>            run("Find Maxima...", "noise=25 output=[List] ");
>            File.append(row, getDirectory("home")+"results.xls");
>            save(path);
>            close();
>       }
>   }
Reply | Threaded
Open this post in threaded view
|

Re: Writing maxima positions in a file

foster12
Hi Michael,

I have appreciated very much your issue.

Your solution work perfectly, I've only changed

dist = sqrt((x0-x1)*(x0-x1) - (y0-y1)*(y0-y1));

with

dist = sqrt((x0-x1)*(x0-x1) + (y0-y1)*(y0-y1));  

Best Regards

Enrico


Reply | Threaded
Open this post in threaded view
|

Re: Writing maxima positions in a file

sunwukong
Im trying to do something like your post. (find maxima in multiple files)
could you post your final code so I can figure out the file recursion and results write portion?


The below macro seems to be working
It does not write the results to a file but shows them in a clip board / dialogue box (which is fine)
however it shows one more line of data (I'm running it against 4 .jpg files and its showing 5

e.g.,
*********** error message
FreeMemory() 9555K of 640MB (1%)
nImages() 1
getTitle() "ScanImage001.jpg"
dir "d:\aifs\"
count 4
n 1
dir "d:\aifs\"
list array[4]
i 0
path "d:\aifs\ScanImage001.jpg"
path "d:\aifs\ScanImage001.jpg"
********** and the results are **********
1 317 242
2 364 268
3 334 235
4 316 225
5 256 428
************  
which looks like it found 5 maxima points in the first file and did not check the next 3 files
The error window shows that it correctly found 4 files in the target directory "D:\aifs"

************* macro ***********
 dir = getDirectory("Choose a Directory ");
 setBatchMode(true);
 count = 0;
 countFiles(dir);
 n = 0;
 processFiles(dir);
 //print(count+" files processed");

 function countFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
countFiles(""+dir+list[i]);
else
count++;
}
}

 function processFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "/"))
processFiles(""+dir+list[i]);
else {
 showProgress(n++, count);
 path = dir+list[i];
 processFile(path);
}
}
}

function processFile(path) {
 if (endsWith(path, ".jpg")) {
 open(path);
// File.open(result.txt)
 run("Find Maxima...", "noise=25 output=[List] ");
//for (i=0; i<=2*PI; i+=0.01)
//print(f,list);
     print(f, [List]);
 File.append(row, getDirectory("home")+"results.xls");
 save(path);
 close();
}
}