Random errors in analysis of successive pictures using macro

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

Random errors in analysis of successive pictures using macro

Nathalie.Stroeymeyt
Hello,

I have to analyse several thousands of pictures of ant colonies in which individual ants have been painted with paint dots (pink in this example, left panel of the picture):


To do so I have written the following macro (I am new to ImageJ):

inputFolder = getDirectory("Choose the input folder!");
outputFolder = getDirectory("Choose the output folder!");
images = getFileList(inputFolder);
setBatchMode(true);
for (i=0; i<images.length; i++) {
        inputPath = inputFolder + images[i];
        open(inputPath);
        run("Brightness/Contrast...");
        setMinAndMax(14, 182);
        call("ij.ImagePlus.setDefault16bitRange", 0);
        runMacro("Pink_Threshold.txt");
        run("Make Binary");
        run("Fill Holes");
        run("Remove Outliers...", "radius=5 threshold=50 which=Dark");
        run("Dilate");
        run("Fill Holes");
        run("Dilate");
        run("Fill Holes");
        run("Remove Outliers...", "radius=10 threshold=50 which=Dark");
        run("Dilate");
        run("Fill Holes");
        run("Dilate");
        run("Fill Holes");
        run("Set Measurements...", "area centroid center perimeter bounding redirect=None decimal=3");
        run("Analyze Particles...", "size=100-5000 circularity=0.00-1.00 show=[Overlay Outlines] display clear");
        outputPath = outputFolder + "Pink" + images[i] + ".txt";
        saveAs("Results", outputPath);
        outputPath = outputFolder + "Pink" + images[i];
        save(outputPath);
        resetMinAndMax();
        close();
}
setBatchMode(false);

and here is the macro Pink_Threshold:
// Color Thresholder 1.44p
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=213;
max[0]=254;
filter[0]="pass";
min[1]=55;
max[1]=255;
filter[1]="pass";
min[2]=167;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Colour Thresholding-------------


I have been running trials on small substes of pictures (20 at a time). I didn't get any error messages, but when i checked the results, I realised that for some of the pictures, only the top half had been analysed - so the ants in the bottom half had not been detected (see right panel of the example picture).

This seems to happen completely randomly, as when I ran<b> exactly the same program on the faulty pictures a second time, it went well and analysed the full picture.

Do you have an idea what is happening, and how I could fix it? Even if an error does happen, is there any way of getting ImageJ to report that error so I will know which pictures need to be reanalysed?

For information, I am running ImageJ 1.44p on a PC Windows 7, processor  Intel Pentium 4CPU 2.80GHz 2.79 GHz, 2.00GB RAM, 32-bit Operating System.

Many thanks in advance for your help!

Best wishes,
Nathalie