Batch macro does not replicate manual results for Analyze Particles

//Macro code I am trying to execute on many many files within a folder

//Gets the file name
fileName = getTitle();
originalName = replace( fileName, "_probs.tif", "");
 
//Separates the stack
run("Stack to Images");
close();
close();

//Select Yeast
selectWindow("Yeast");

//Threshold
setAutoThreshold("Default dark");
//run("Threshold...");
run("NaN Background");
setOption("BlackBackground", false);

//Convert to mask
run("Convert to Mask");

//Count
run("Analyze Particles...", "size=40-1000 pixels circularity=0.30-1.00 show=[Overlay Masks] display exclude summarize in_situ");

//Save counts
run("Read and Write Excel","file=[PATH/Counts.xlsx]","stack_results");

//Save masks
saveAs("Tiff",originalName + "_countmask.tif");
close();

Link to an example of an image stack I am trying to process: https://umich.box.com/s/moqt367pmclvpvufmqyh1fgq74u3hrpc

Hello! I am new to this kind of image analysis and would be grateful for your help in identifying what has gone wrong. I’ve included my code and a link to the image for reproducibility. Hoping that any also insight will benefit other newbies trying to execute similar pipelines.

The immediate challenge is that I’m trying to use a batch process macro in Fiji to replicate the results I get manually using Analyze Particles to count objects in an image. This seems like a trivial thing, but I cannot get a valid Analyze Particles count inside the macro even though i. the protocol works when I manually execute it, and ii. recording a macro from my manual actions produces code that is identical to what I’m trying to execute.

The larger image analysis goal is to segment and count cells in images of stained yeast cells that that may be touching, overlapping, or partially engulfed by a macrophage and therefore only partially stained. My current approach to this task uses Weka classification to export probability maps for what is a yeast cell vs macrophage vs background. Here I am trying to take those probability maps, split stacks to images, select the yeast image, threshold it appropriately setting the background to NaN with no black background, convert the image to a binary mask, and then count the resulting objects.

Again, running this process manually gives me useful counts of yeast cells. Inside a batch process macro, I get NaN for the counts. I thought that something about my file processing inside the macro might be off so I’ve tried executing the macro and saving a tif of the binary mask just before the Analyze Particles step. Manually running Analyze Particles on this binary mask file works just fine, but the batch process macro still produces NaN counts. :man_facepalming: Can you help? What am I missing?

I have searched this forum and others for relevant issues with batch process macros and analyze particles (for example: ImageJ Batch Processing for Analyze Particles Function or Make Binary Foreground/Background Issues or Inverted image, problems with Batch Processing / Macro), but I seem to be having a slightly different set of issues than what I can find. Links to more appropriate resources or even better search terms would be very welcome. If relevant, I am working with the 1.53c installation.

After solving this issue, I suspect I will have more troubleshooting to get the counts exported appropriately as a xls or csv file. I’m currently working with this package: Read and Write Excel - ImageJ, but other recommendations also welcome.

Thanks in advance!!

Andrea

Have you tried saving the results file as part of the macro?
saveAs("Results", "C:\wherever\data.csv");

Obviously in batch mode you would need to have a variable in the name so that it did not keep overwriting itself, but that would be a good first check for your macro. Then you could work on the Excel issue.

You do mention manually and batch process macros, but not what happens when you run the macro on a single image.

Also, it was not clear to me if this is the base macro that you are adjusting to run in batch mode, or if you are using this macro through the Process->Batch menu.

I can’t access your images, but maybe someone else can take a look if there is something specifically going on there.

Hi,

Thanks for your quick reply!

I am running Process-> Batch. When I just select Run->Macro on an open image, I get the same issue I see in the batch process case.

I’m realizing that I should correct what I said above to clarify that I get 0s and NaNs in the Summary table from Analyze Particles when using either the Batch or single Macro functions. So the issue may be that Fiji is not recognizing any objects at all?

Summary.csv (90 Bytes)

Now that I can upload files, here is an example of the converted binary mask after all upstream processing that generates non-zero counts manually but zero counts in the macro.

Phago_CP2_well1_A1_R2_cfw_contomask.tif (4.0 MB)

1 Like

Wondering about this. Do you really not have a black background? That might explain NaNs since you set the background to NaNs then measured it?

Hmmmmm. Interesting idea.

Looks like setting this BlackBackground to true still produces zero counts. I can look at the upstream steps, but I can’t figure out what could account for the difference in running AnalyzeParticles manually and via the macro.

Ah, yes, I was able to get that image, and it is correctly producing 0 counts for the image based on your settings.

Inverted LUT stuff

You are measuring all of the white space in the background which does not fit your 40-1000 pixel size. I suspect the confusion might be that you inverted the lookup table at some point. Maybe this was due to the blackbackground false step.

image
What some of the image looks like on my end.
image

Anyway, that is not the main problem. It looks like you are doing the macro in inches instead of pixels, as indicated at the top of your image in the top image I posted.

Your macro on the bottom, the corrected syntax on the top.
image
It looks like you must have manually typed in pixels or added an s inadvertently, so even when you target the small objects, they are way too small in square inches to show up within your 40-1000 range.

1 Like

Oh my goodness. What a tiny, annoying error! Thanks for helping to identify where the issue was! You’ve saved me from tearing out the rest of my hair over one s.

A

1 Like