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)
Thankful.

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

Image1inverted
Image1Inverted.tif (12.4 MB)

Image2normal
Image2normal.tif (690.2 KB)

Hi
@FERRETTO
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");
run("Invert");
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.

@FERRETTO
We would need the source images (originals).

OK
Image1inverted. Original.

Image2normal. Normal.

Hi
@FERRETTO
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
run("8-bit");
setAutoThreshold("Triangle");
//run("Threshold...");
//setThreshold(0, 150);
run("Convert to Mask");
//-----------------------------

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

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.

2 Likes

Hi
@Research_Associate

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.

2 Likes

//This explains these choices below:

run(“8-bit”);
setBackgroundColor(0,0,0);
//---------------
setOption(“BlackBackground”,false);
//---------------
// or
//---------------
setOption(“BlackBackground”,true);
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:

2 Likes

Thank you very much, it worked well. Thankful.
Working