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

imagej

#1

Dear all,

I’m just facing a bug (?): In a binary image, the black (0) background becomes selected when I use the “Create selection” menu.

black_background_becomes_selection

I’ve seen this happening during quite some ImageJ courses on some random computers but we never really figured out why it happens here, but not there. I have it now the first time on my computer and still don’t know what’s going on.

The background is 0, objects are 255. The lookup table is not inverted. Everything appears fine…

Any ideas?

Thanks!

Cheers,
Robert


#2

‘Create selection’ works on the selected thresold range. If you do not set a threshold you are letting IJ to use whatever value was set before. Set a threshold before calling the Create selection command. You might also want to check if the Binary Options “black background” option is set.


#3

This code leads me to believe it will always select the 0 pixels if the LUT isn’t inverted:

Note also the reference to ‘white background’ in the message above.

But I always set a threshold in any case, so at least I can be confident what looked ‘red’ before will be inside the selection (assuming red is what is showing the thresholded region).


#4

Ahh, so in my case that may happen because I used the image calculators AND to create the binary image…

Thanks for the hints!


#5

Yes, if you set a thresold range, it will use that.


#6

Sorry to intrude here, but the OP wrote that he is dealing with a binary image. Therefore, I think, a threshold is no longer required, or did I misunderstand something?

Best

Herbie


#7

Yeah right. Thanks guys!

So the issue was that the binary image was created from the Image Calculator (AND). If you run “Create Selection” from macro you need to add a line to the macro just before to fix the issue:

setThreshold(127, 255);
run("Create Selection");

Thanks everyone! :slight_smile:

Nevertheless, @Wayne might that be a bug?


#8

Yes, in the case of binary images, setting threshold is like setting the image polarity: which is background and which is foreground without affecting the binary options.


#9

Sorry but for me it is sufficient to use:

setOption("BlackBackground", true); // should be used at the start of any macro
run("Make Binary");
run("Create Selection");

Nevertheless, @Wayne might that be a bug?

I don’t judge this being a bug (see my PS).

Regards

Herbie

PS:
The ImageJ User Guide tells us:

27.12.11 Create Selection

Creates a selection from a thresholded image or a binary mask [15].


#10

Herbie,

have you tried with a binary image that results from the Image Calculator?

newImage("image1", "8-bit ramp", 100, 100, 1);
run("Duplicate...", " ");
run("Rotate 90 Degrees Left");
selectWindow("image1");
setAutoThreshold("Otsu dark");
run("Convert to Mask");
selectWindow("image1-1");
setAutoThreshold("Otsu dark");
run("Convert to Mask");
imageCalculator("OR create", "image1","image1-1");
selectWindow("Result of image1");
resetThreshold();

//setThreshold(127, 255);

run("Colors...", "foreground=white background=black selection=green");
run("Create Selection");

Try it with and without the commented line.

Robert


#11

I shall try in some minutes …

For the time being here is a demo macro:

setOption("BlackBackground", true);
newImage("image_0-255", "8-bit black", 256, 256, 1);
makeRectangle(100, 120, 115, 60);
run("Set...", "value=255");
run("Select None");
run("Create Selection");
roiManager("Add");
roiManager("Select", 0);
roiManager("translate", 30, 30);
roiManager("Select", 0);
run("Add Selection...");
roiManager("delete");
newImage("binarized-image_0-255", "8-bit black", 256, 256, 1);
makeRectangle(100, 120, 115, 60);
run("Set...", "value=255");
run("Select None");
run("Make Binary"); // please note that this line makes the difference
run("Create Selection");
roiManager("Add");
roiManager("Select", 0);
roiManager("translate", 30, 30);
roiManager("Select", 0);
run("Add Selection...");
close("ROI Manager");
exit();

Have fun

Herbie


#12

This method appears to work because “Make Binary” secretly sets the min & max thresholds to be 255, and then these are used.

If the threshold is reset after “Make Binary” is called, then “Create Selection” will instead default to taking the 0 pixels.

The important thing seems to be the thresholds set in the ImageProcessor, which might not be evident if they aren’t reflected in the LUT.

See for yourself with

setOption("BlackBackground", true); // should be used at the start of any macro
run("Make Binary");
resetThreshold()
run("Create Selection");

and optionally check values with

getThreshold(lower, upper)
print(lower + ", " + upper)

(@haesleinhuepf hmmm, those macro lines were easier to find than I remember… didn’t even have to resort to the web browser :wink: )


#13

Agreed,

your example behaves differently from the one I’ve posted and I’m not yet perfectly sure why. But a hint appears being:

[…] which might not be evident if they aren’t reflected in the LUT.

In general I’ve difficulties with understanding the whole threshold logic of ImageJ and I suspect that certain complications are due to backwards compatibility.

Perhaps thresholding needs to be redesigned and implemented using a different name.

There were recent discussions regarding the “Don’t reset feature”, the deficiencies regarding the thresholding of 16bit and 32bit images, and the exclusion of zero and max values from the auto threshold computations.

Regards

Herbie


#14

For my part finally I should like to point out my impression that ImageJ makes some implicite assumptions about what is foreground and what is background.

If I run my above macro I get:
image_0-255
binarized-image_0-255

If I run my above macro with the white area defined by

makeRectangle(20, 20, 200, 200);

I get:
image_0-255
binarized-image_0-255

Regards

Herbie


#15

Glad I can help make hints appear :slight_smile:

You get differences in each case because “Make Binary” is calling this method with the ISODATA2 method:

You can see towards the end that a high or low threshold is chosen based on where the histogram mode falls.

The message for me remains the same: never use “Make Binary” or “Create Selection” without explicitly setting a threshold beforehand, so that automatically determining a threshold is not necessary.


#16

Thanks for having a look at the source.

So

This method appears to work because “Make Binary” secretly sets the min & max thresholds to be 255, and then these are used.

is not perfectly true?

Best

Herbie


#17

Intriguing question. I wouldn’t personally consider ‘completeness’ to be a necessary prerequisite for something to be ‘perfectly’ true, but I guess some might disagree. Although this is all veering a bit closer to a discussion on the ontological argument than I was expecting at image.sc.

It remains the case that, under some circumstances, “Make Binary” will set min & max thresholds to 255 in a way that is not obvious to the user. When the applying “Create Selection”, the selection that is generated differs depending upon whether or not these thresholds are retained or reset. In the original example, where min/max thresholds are not set then the black region is detected… otherwise the white region is.

But it does appear that “Make Binary” will sometimes set different thresholds, depending on the image content, as your macro demonstrates.


#18

But it does appear that “Make Binary” will sometimes set different thresholds, depending on the image content, as your macro demonstrates.

No reason to assume metaphysics in action but that’s exactly what I meant by stating:

that ImageJ makes some implicite assumptions about what is foreground and what is background.

I ran into similar situation with one of my own plugins and the reason was that I wanted to make certain actions automatic, so the user needn’t care about what is object and background. However, I realized that the problem is finding a criterion that reliably works in all situations. I didn’t find one hence I’ve implemented the option to let the user decide if she isn’t happy with the automatic result.

I conjecture that a similar automatic decision is the reason for the observed behaviour and that, for backwards compatibility, it isn’t possible to change it.

Thanks to all and let’s see how things develop.

Regards

Herbie


#19

To avoid ambiguity, always set the threshold before using “Create Selection”.


#20

Dear @Wayne ,

would it be possible to run that automatically in case the Image Calculator calculates a binary image? Or may there be side effects I’m not thinking of?

Thanks!

Best,
Robert