MJLe
MJLe

Reputation: 1

Is there a way to append information to a results table from multiple images in ImageJ using a macro?

I am trying to write a macro to assist in the the processing of multiple images of animals in ImageJ. Briefly, I have multiple folders with photos of animals which need to have points set on certain features, and I want a single results table that I can save as a csv file with the coordinates from all of the images in one folder on it.

The information that I would like to have in the final results table is:

  1. X and Y coordinates for (let's say 6) locations on each image made with the multi-point tool, where each one is associated with the image's filename. (I think I've figured out this part)

  2. Because there are multiple images in each folder that I am running through this macro, I would like the coordinates for each new image to be appended to the results table output instead of being cleared with each new iteration of the for loop.

  3. Because the multi-point tool output is not sequential, ideally those points will be numbered in a separate column the final table (I have not attempted this yet, but it will be my next hurdle).

My results table would ideally look something like this (albeit with the coordinates filled in for X and Y):

|  X  |  Y  |  Filename  |  CoordNumber  |
|     |     |   AA.tif   |       1       |
|     |     |   AA.tif   |       2       |
|     |     |   AA.tif   |       3       |
|     |     |   AA.tif   |       4       |
|     |     |   AA.tif   |       5       |
|     |     |   AA.tif   |       6       |
|     |     |   BB.tif   |       1       |
|     |     |   BB.tif   |       2       |
|     |     |   BB.tif   |       3       |
|     |     |   BB.tif   |       4       |
|     |     |   BB.tif   |       5       |
|     |     |   BB.tif   |       6       |
|     |     |   CC.tif   |       1       |
|     |     |   CC.tif   |       2       |
|     |     |   CC.tif   |       3       |
|     |     |   CC.tif   |       4       |
|     |     |   CC.tif   |       5       |
|     |     |   CC.tif   |       6       |

I have written a macro which has a user go through the images one by one, and records the coordinates of multi-point tool selections. Unfortunately, the for loop that I have written does not append the coordinates from the next image to the results table, instead it clears the results from the previous image and starts fresh. While I can save each one seperately and do this by hand, it would be much quicker to just have ImageJ create a continuous table for me (as seen in the imaginary table above), if that is possible.

My attempt at the macro is below:

//---------------------------------------------
macro "Ventral Measurements Test ."
{
//---------------------------------------------
imageDirectory=getDirectory("Open Folder");
print("InitialFolder is=", imageDirectory);
list=getFileList(imageDirectory);
n=lengthOf(list);
print("The number of images is: ="+n);
//---------------------------------------------
// Start macro processing
//---------------------------------------------
//Using a for loop
for(i=0; i<n;i++) {
open(imageDirectory+list[i]);
//---------------------------------------------
//setTool("multipoint");
waitForUser("If you haven't already, use the multi-point tool to mark the image as described in your instructions.\n Pay close attention to the orientation of the specimen. (is it in the VENTRAL orientation?)\n When you are finished, double-check to ensure that your selections are correctly placed and in order.\n If you notice anything odd about the specimen, make a note of its filename and what you think is odd,\n and Email your comment along with the data.\n If everything looks good, click OK to go to the next image.");
getSelectionCoordinates( x, y );
for(j=0; j<lengthOf(x); j++) {
Table.set("X", j, x[j]);
Table.set("Y", j, y[j]);
Filename=getTitle();
Table.set("Filename",j,Filename);
Table.update();
}
//---------------------------------------------
close(list[i]);
}
//---------------------------------------------
close("*");
// End of macro processing
//---------------------------------------------
exit("Finished! Please review to make sure everything is displayed correctly,\n and then save the document if it looks correct.\n Don't forget to email your data and comments.");
}
//---------------------------------------------

Upvotes: 0

Views: 618

Answers (1)

You could do something like this:

imageDirectory=getDirectory("Open Folder");
print("InitialFolder is=", imageDirectory);
list=getFileList(imageDirectory);
n = lengthOf(list);
print("The number of images is: ="+n);
row_to_write = 0;

for(i = 0; i < n; i++){
    open(imageDirectory+list[i]);
    close("\\Others");
    run("ROI Manager...");
    setTool("multipoint");
    waitForUser("Select points to measure");
    n_rois = roiManager("count");
    coordNumber = 1;
    for(j = 0; j < n_rois; j++){
         roiManager("select", j);
         getSelectionCoordinates(x, y);
         setResult("X", row_to_write, x[0]);
         setResult("Y", row_to_write, y[0]);
         setResult("Filename", row_to_write, list[i]);
         setResult("CoordNumber", row_to_write, coordNumber);
         row_to_write += 1;
         coordNumber += 1;
    }
    roiManager("reset");
}
saveAs("results");
close("*");
  1. You can open a new roiManager for each image where you add all points. Once all points are added you can click ok the waitForUser() and it will loop through each point getting the coordinates and writing to a results table (setResult() function).
  2. Use a variable to keep track of the row to write the results.
  3. To get what you call coordNumber, you can reset that variable whenever you open a new image. That also allows you to output the filename in another column.

Hope that's helpful!

Upvotes: 0

Related Questions