How to detect black image in imagej macro?

Is there any function that can find the black image in a group of images? Images are grey scale (8 bit)

You can use the getStatistics(area, mean, min, max, std, histogram) macro function (https://imagej.nih.gov/ij/developer/macro/functions.html#getStatistics) and test if the mean parameter is 0, or less than some value, if the images are not completely black.

4 Likes

I used this command but for an image which is not black, it returns zero mean. The image is 32 bit and it is masked. I added an image as an example which returns zero.
image

@Bio7
@haesleinhuepf
@etadobson
@Wilson_Adams
You are really helpful that’s why I am sharing this question with you.
Thank you all.

I think there’s a few ways you can do it with getStatistics() with some conditional formatting. This might not work all the time, but might give you something to test.

  1. min == max
  2. mean == 0 & std == 0
  3. max == mean == 0

Just a few ideas to get you going. hopefully that’s helpful.

-Wilson

1 Like

@Wilson_Adams I tried that and the problem is I get zero for non-black image.

Hi, the image you show has A LOT more black pixels than white, so the mean would still be 0. Try other parameters as Wilson suggested, perhaps max > 0, but for that condition the “black” image should really be all 0 pixel values.

1 Like

@AlesK @Wilson_Adams
Thanks for your helps.
image
At least those rarely non-black pixels should be shown in the histogram. Am I right? You see the histogram is all zero.

they should be shown in the histogram. I’m confused why they arent. Those white pixels should pop up as 255, shouldnt they?

1 Like

That’s weird, min and max are both 0, but mode is not zero. In 32 but image the pixel values are not integer, but they are floating point values. Can you describe the steps you do to generate the images? It looks like you do a threshold and generate a mask. The problem with mask images is that they use a lookup table for display, so the pixel values might be different from what you see.

2 Likes

if this a 32 bit image, your bins might be too wide to separate 1 and 0.

1 Like

@Wilson_Adams I increased the bin numbers, again the same.
image

That seems right. Can you instead use 8 bit images as the output of segmentation?

2 Likes

@AlesK Do you mean changing 32 bit to 8 bit?

1 Like

when your bin width is 1700, and the differences in your signal and background is <1700, those pixels are going to be jammed into one histogram bin.

1 Like

You can try it, but if you get all zero pixels, then try to change one of the previous steps, so that you don’t get the 32 bit image

2 Likes

yeah if you can avoid the 32-bit image, you might be able to get around this problem.

1 Like

@Wilson_Adams @AlesK
If the problem is masking and lookup table, why changing from 32 bit to other type would solve the problem? I changed the image that I have (already masked) to 16 bit, did not change.

I think the getStatistics function uses 256 bins, so if your values are 0 and 1, they would end up in the same bin for 32 and 16 bit images.

1 Like

Update: I realized even with 32 bit image it returns non zero value. look at here:
image
The problem arises from masking.
I need to have the histogram after masking. Any new suggestion regarding this update?