Inverted image, problems with Batch Processing / Macro

Hello, I have some difficulties when executing “Batch Processing”, using a “Macro” created by me.
I am calculating the leaf area, from many points.

The problem is that some images after (8-Bit and Make Binary) become inverted (Image1inverted). this way the program calculates the spaces between the sheets, and this is bad. LMAO.

Question 1, is there any way to include the command (edit> Invert) inside the macro.

Question 2, there is another way to solve this problem.

Ps: This problem does not occur with all images.(Image2normal)

Edit: Sorry, I forgot to add the photos :smiley:

Image1Inverted.tif (12.4 MB)

Image2normal.tif (690.2 KB)

Can you send us the part of your macro that would be responsible for this behavior?
With one or two images

Two preferably, one of either kind, so we can see the behaviour in one but not in the other one. Mandatory code snippet:

run("Select All");
1 Like

The code is simple, but the problem is how the images become.

I thought about using that code. But it doesn’t happen with all images. Check the update of the post, which I did.

We would need the source images (originals).

Image1inverted. Original.

Image2normal. Normal.

Can I afford advice:

  • separate your objects well (they touch in one of the images)
  • try to arrange them in column and row.
    Otherwise the background color is fine.

Try this:

// Step 1 processing: thresholding
//setThreshold(0, 150);
run("Convert to Mask");

Can you tell me about your results, if you don’t mind.

1 Like

I have run into similar issues before when running macros. In one case the most common color is green, while in the other the most common color is white. Due to the differing proportions, Make Binary will pick different foreground and background colors (in the first, green is clearly the background color, and in the second, white is the background color, since those are the most common colors in each image - from the code’s point of view).

Mathew’s code avoids using Make Binary and thus avoids this problem.



In ImageJ guide:
If a threshold has not been set, Make Binary↑ will analyze the histogram of the current selection, or of the entire image if no selection is present, and set an automatic threshold level to create the binary image ( ‘Auto-thresholding ’ is displayed in the Status bar, cf. Threshold… [T]↑).

Without going into tech, do you know how the choice is made?
What are the selection criteria? Probably a threshold if> 50% is pixel 0 or pixel 255!

Thank you in advance.

1 Like

As far as I can tell, and I haven’t dug into the code, but it seems to behave like a K(=2) means clustering. And the most common “color” becomes the background, and the least common “color” the foreground. So it has nothing to do with brightness at all.

That leads to problems when whatever you are looking for can be either more or less than 50% of the image. Setting a fixed threshold where over and under (or vice versa) are locked to the foreground and background is the best way around it. At least that I have found.


//This explains these choices below:

// or
run(“Make Binary”);

1 Like

And in case I am wrong about that, perhaps @etadobson knows better, or the right person to poke.

1 Like

@Wayne is the obvious person to best help in this particular issue. :slight_smile:


Thank you very much, it worked well. Thankful.