ImageJ: Black (0) background becomes selection - a bug?

imagej

#21

I think it’s better to set the threshold so that it is obvious what is foreground and what is background.


#22

I’m not sure if I understand correctly. Isn’t the threshold obvious, when the Image Calculator has a binary image as result, e.g. after executing the AND operation?


#23

It is not obvious what values foreground pixels have. Is it 0 or is it 255?


#24

I thought, that’s defined by the black background checkbox…

image

Btw. how can the Image Calculator calculate a logical conjunction like AND, without knowing what values correspond to foreground?


#25

This bug is fixed in the latest ImageJ daily build (1.52k7). The “Create Selection” command was ignoring the “Black background” setting.


#26

All it does is use the Java “&” (bitwise AND) operator.


#27

I just tested the new version and it works great! From bug report to bug fix in 24 hours. It’s a pleasure working with you on ImageJ.

Thanks a lot!

Cheers,
Robert


#28

Dear Wayne (@Wayne),

I’m very happy with the bug-fix but I still don’t understand why

run("Make Binary");

doesn’t do what the name “Make Binary” implies. With all of my image processing software (e.g. Photoshop) it defaults to a threshold at 127 for 8bit images.

Here is a demo macro:

setOption("BlackBackground", true);
newImage("Small-Rect_0-255", "8-bit black", 256, 256, 1);
makeRectangle(100, 120, 115, 60);
demo();
roiManager("delete");
newImage("Big-Rect_0-255", "8-bit black", 256, 256, 1);
makeRectangle(20, 20, 200, 200);
demo();
close("ROI Manager");
exit();
//
function demo() {
   run("Set...", "value=255");
   run("Select None");
   //run("Make Binary"); // this makes the difference
   run("Create Selection");
   roiManager("Add");
   roiManager("Select", 0);
   roiManager("translate", 30, 30);
   roiManager("Select", 0);
   run("Add Selection...");
}

Results without “run(“Make Binary”);” (ImageJ 1.52k7):
binarized-image_0-255
Big-Rect_0-255

Results with “run(“Make Binary”);” (ImageJ 1.52k7):
binarized-image_0-255
binarized-image_0-255

For me the latter result is counter-intuitive.

Best

Herbie


#29

There is nothing counterintuitive. The background is set by default to be light. If you used the Thresholder with the setting Dark background checked , Create selection would’ve worked as expected. In a binary image (make binary) the background is set to dark (0) and that is why it works. Running Make binary on an image with applied threshold simply sets the background to dark.
In order to ensure correct behavior in a macro code you just need to add
after the binarization procedure

setAutoThreshold (“dark”);
Run(“Create selection”);

and it will behave.


#30

The background is set by default to be light.

setOption("BlackBackground", true);

I doubt the (expected) behaviour of

run("Make Binary");

which appears to act signal dependent.

Hope this makes things a bit clearer

Herbie


#31

anyway isn’t it possible to set background black and equals to 0 forever and for whatever ImageJ version and applications and plugins?
I know it will a pain for retrocompatibility but it will be very clear for the future and also very intuitive and same as all other image processing tools in the world.

this background (together with the inverted LUT…) it’s the main reason of macros and plugins failures in my Institute…


#32

Yes, it is possible.
Just record setting it in the binary options and add that line to the StartupMacros.txt file.

Mine includes:
run("Options...", "iterations=1 black pad edm=Overwrite count=1");

so I have black background by default.
That is a very convenient way of setting a number of options to override the defaults.
Just remember to distribute your StartupMacros.txt file.


#33

Thanks for chiming in Gabriel,

your suggestion doesn’t resolve the problem I have with the situation displayed under

Results with “run(“Make Binary”); (ImageJ 1.52k7)

above.

Do you have any opinion about why

run("Make Binary");

doesn’t perform according to its name and why it appears to act signal dependent?

Best

Herbie


#34

Hm… Maybe (not completely sure, though) the problem is that Create Selection is taking a default value of the thresholder?

If you just add

setThreshold(255,255);

before the Make Binary command of your macro, then the result is as expected.
Maybe there is some inconsistency here about what to consider background colour for thresholding and what to set the thresholded phase to: e.g. if I want to detect a dark object, should I paint it black or white?

Also I just noted that if one repeatedly presses ‘Auto’ and then ‘Apply’, it behaves differently when have ‘Dark background’ selected (it alternated background and foreground results).

If one is explicit about the threshold (as I suggested above), then Make Binary uses that value and behaves as expected.


#35

Yes Gabriel,

actually we have been there
https://forum.image.sc/t/imagej-black-0-background-becomes-selection-a-bug/21927/19?u=herbie
however, the problem I’m pointing to is that if someone uses

run("Make Binary");

she can expect a threshold action and the applied threshold should default to 127 for 8bit images.

ImageJ however soemtimes does something completely different when using

run("Make Binary");

and that’s what bothers me and, as it appears nobodyelse.

Best

Herbie


#36

Maybe I have been misinterpreting all this. I understand “Make binary” as a command that generates a two valued image based on a threshold range. The “foreground/background” issue (what is white, what is black) is set with the Binary option “Black background”, but you still need to explain which range of greyscales are to be converted to foreground or background when calling “Make binary”.
Again, (to me) the example you posted is missing the threshold setting. I guess that calling “Make binary” on its own defaults to use some default threshold value that (I guess) is in the image processor of the image. Note that each new image one creates has a default threshold range 0…128 regardless of the Dark_background option.


#37

Thanks Gabriel!

I must admit that I don’t understand everything you’ve stated above but it seems that my problems are about terms (only).

I understand “Make binary” as a command that generates a two valued image based on a threshold range.

So this is what corresponds to clicking “Apply” in the “Threshold”-dialog.
In Photoshop if I convert an 8bit image to Bitmap the default is 50% threshold and it is set. I always though that

run("Make Binary");

does just this.

Again, (to me) the example you posted is missing the threshold setting.

Yes and this was intended, because the images are already binary and applying

run("Make Binary");

should do nothing!

I guess that calling “Make binary” on its own defaults to use some default threshold value that (I guess) is in the image processor of the image.

Granted for the time being, but why then does

run("Make Binary");

change foreground and background even if

setOption("BlackBackground", true);

is set.

run("Options...", "iterations=1 black pad edm=Overwrite count=1");

doesn’t make a difference.

My impression is, that

run("Make Binary");

makes assumptions about foreground and background and if I find the time I shall have a closer lokk at the source.

Thanks

Herbie


#38

This bug is fixed in the latest ImageJ daily build (1.52k10).


#39

Dear Wayne,

thank you very much for correcting this additional problem.

I shall also like to thank all of those who participated in this discussion. Your patience is much appreaciated!

Regards

Herbie