Glitch with Batch Processing Images

I’m batch processing pictures using a macro to determine percentage of green plant tissue from small plot research trials. Every so often a picture doesn’t appear to process correctly and the coverage estimates are greatly underestimated.

Here is an example of the original image and the subsequent image that doesn’t process correctly after running through the macro.

Original Image

Processed Image

When I manually set the color threshold levels, convert to binary, and run the Analyze Particles function, the vast majority of green tissue is properly selected. But for some reason, the plant tissue in the bottom 2/3rds of the picture isn’t correctly selected when run through the macro. This issue is rather sporadic and doesn’t always occur. I’ve ran entire sets of images (50-100) without this occurring.

Here is the code for the macro I’m using:

run("Set Scale...", "distance=3494.5 known=100 unit=cm");
run("Color Threshold...");
// Color Thresholder 1.49m
// 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]=35;
max[0]=125;
filter[0]="pass";
min[1]=45;
max[1]=255;
filter[1]="pass";
min[2]=0;
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-------------
run("8-bit");
setAutoThreshold("Default");
//run("Threshold...");
setOption("BlackBackground", false);
run("Convert to Mask"); run("Remove Outliers...", "radius=2 threshold=50 which=Dark");

run("Analyze Particles...", "  show=Masks display summarize");

I’ve updated to the most recent version of ImageJ to no avail.

Does anyone know why this is happening and if there is anything I can do to avoid this?

Thanks for advice anyone can share with me.

I’m still learning here, but I wonder if sometimes your computer gets starved for memory causing this intermitant problem. Do you see the problem if you run your macro without showing you the Images as you process? and then making say a montage of the results so you could spot check them? And/Or can you do some test runs without other programs running?

1 Like

Thanks for the thoughts Susan. I’ve wondered about the computer memory issue myself as well. I have increased the memory available to 1250MB by adjusting the settings under Edit > Options > Memory & Threads. I’ve also run the macro with all the other programs on my computer closed and only ImageJ running. Unfortunately, these attempts didn’t resolve the issue.

If you figure it out please post here. I am starting to get into a lot of batch processing and knowing what can go wrong is always a good thing to have a handle on.

Ah… you posted here too.

From the email:

I’ve not been able to replicate this error on my machine using the following macro and your image. At the end, do Window > Tile and they will splay out like this: http://i.imgur.com/QrH3pLh.jpg

I ran this in FIJI (2.0.0-rc-43/1.51b6) and ImageJ (1.50i) Java 1.8.0_91 64 bit Mac OSX 10.11.5.

This took almost 4GB of RAM on my machine. Adjust z loop numbers down if you don’t have at least 6 GB of RAM.

// begin macro
// reset all windows and measurements
run("Options...", "iterations=1 count=1 edm=Overwrite");
run("Line Width...", "line=1");
run("Colors...", "foreground=black background=white selection=yellow");
run("Clear Results");
run("Close All");
print("\\Clear");
run("ROI Manager...");

open(File.openDialog("Find grass file"));

setBatchMode(true);

orig = getTitle();
selectWindow(orig);


for(z=0; z<27; z++) {
  selectWindow(orig);
  run("Duplicate...", "title=Test");
  
  run("Set Scale...", "distance=3494.5 known=100 unit=cm");
  run("Color Threshold...");
  // Color Thresholder 1.49m
  // 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]=35;
  max[0]=125;
  filter[0]="pass";
  min[1]=45;
  max[1]=255;
  filter[1]="pass";
  min[2]=0;
  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-------------
  run("8-bit");
  setAutoThreshold("Default");
  //run("Threshold...");
  setOption("BlackBackground", false);
  
  run("Convert to Mask"); 
  
  run("Remove Outliers...", "radius=2 threshold=50 which=Dark");
  
  run("Analyze Particles...", "  show=Masks display summarize");
  
  selectWindow("Test");
  run("Close");
  
}

setBatchMode("exit and display");

exit("Finished!");	
//end macro
1 Like

Do you mean that it doesn’t always occur for the same image? Or just that it only occurs for some images?

Is the image above one for which the problem occurs? Or does not occur? Or sometimes occurs?

As a general rule, debugging intermittent issues—which occur only sometimes for the exact same series of steps on the same inputs—is much more difficult than debugging when the error is reproducible for a particular image.

While anything is possible, I would be shocked if a lack of sufficient memory made ImageJ silently give wrong results. When your computer memory is insufficient, you should certainly be seeing an error message.

I did initially notice some issues when I was using an i loop with internal i loops. They didn’t look like the example OP showed, but there was definitely one that was inverted and another that was lighter (perhaps repeatedly filtered?). The loop did not stop either (50 images before I stopped it).

I was very confused reading your replies, until I noticed the mailing list thread. It is always a good idea to cross-link when discussions are fragmented across multiple sources. For others, the relevant thread on the mailing list is:

http://imagej.1557.x6.nabble.com/Glitch-with-Batch-Processing-tt5016511.html

Certainly, you cannot reuse the same loop variable in an inner loop, without hosing everything. You need separate variables. E.g.:

for (i=0; i<m; i++) {
    for (j=0; j<n; j++) {
        ...
    }
}

If you use i for both, then when n < m your loop will never end.

2 Likes

I thought I’d mention it in case OP was making that error. I believe this code is only partial to their entire work flow. What say you @majones?

1 Like

From the listserv, the following seems logical.

Hi Marcus,

I ran your code on my machine and everything worked as expected. (I did interpret the position of some comment delimiters in a specific manner that might be different than actually in your code.)

Your example images suggest to me some kind of hiccup occurs after the first run(“Convert to Mask”) triad. Many years ago I started adding wait statements after long-processing-time commands to prevent one command from starting before another begins. I don’t know if that problem of not synchronizing properly has been fixed or not in recent time. Your image looks like the top 1/3 was processed one way and then the bottom 2/3 another.

Did the macro execute to completion when it gave the output image? It does not appear to have been processed with your run('Remove Outliers…") command.

If it is a synchronization problem adding strategic wait statements or putting your macro into setBatchMode(true) mode might help.

Best regards,

Dan

2 Likes

To better clarify - the issue appears to be sporadic in that it only occurs for some images. The image that I posted has been a problem image. I did greatly reduce the file size of the image in order to upload it to this site. However, the settings on the digital camera have been adjusted to reduce the file size of the raw images.

In this specific example, the problem photo was only 1 of 4 images collected to be batched. I don’t receive any error messages during the process and only discover the problem when browsing through the processed photos.

Hi, I think I might be having a similar issue with that a “wait” command might solve, Do you have a set number of milliseconds you recommend? Or do you define the wait based on a command in your code?

I run as much stuff as I can in batch mode. It is simply way faster to do so because it’s not drawing the images. I believe that’s what Dan McDonald is referring to in his email post on the listserv. Every once in awhile an image will take a bit longer to print to the screen. If it starts working on the next step before that’s done it will operate on a partial image, which is weird, but I have seen it before, especially on my mac when out of batch mode before there were fixes in place to improve speed of the drawing on Macs last year.

As far as what would work… I really don’t know. Best to try lower numbers first (10-100 ms) and then go up if you still see them. If you have a lot to process those ms will add up quickly.

I am guessing the issue is with your “Remove Outliers…” command. That command is majorly reducing the mask pixels here!

BEFORE

AFTER

I know you want to eliminate the tiny specks, but you might be better off doing that via the Analyze Particles... command without nuking the real structures of interest.

If it is a full memory or exhausted resource problem, you could try inserting run(“Collect Garbage”); in the loop. In Fiji, it is in the utilities plugin menu

Has this ever actually made a non-working macro start working for you? In theory, it should not be necessary…

Also, I would be really surprised if this problem is memory-related. It seems like a problem with the analysis to me.