Automatically select "white" images (microscopic images showing only background) from stack and delete



Hi, I am analysing microscopic images. Since the structures I am analysing are very big, the microscope automatically makes multiple adjoining images. Thereby also a lot of “white” images are generated, which only show the background, not the structure I am interested in. Is it somehow possible to automatically select these images in order to delete them afterwards? At the moment I am selecting the “white images” by hand, which is very time consuming.

Do you have any ideas?
Thanks a lot


Depending on the type of image you are dealing with, you could attempt to find if there is any tissue in the image by computing a threshold for the background (e.g. via the saturation in HSV space) and comparing the area of the thresholded phase to the area of the image. If it is close to the image size, then assume that it is mostly background.


My problem is that I want to automatically select and remove specific images / slices from a stack or batch of images.

These images / slices I want to automatically exclude are all white. At the moment I have to look at every image / slice of my stack and remove the white ones by hand.

A function solving my problem could for example be to create a sub stack, which only contains pictures with a certain amount of pixels that are different from white or only such images / slices containing contrasts within certain thresholds. A idea is also to select one of the white images by hand and let imageJ or Fiji find and select all of those images / slices which are identical or similar and remove them all at ones.

But I have no idea how I could do this with imageJ or Fiji

Thanks for suggestions and help!


Can you upload a sample image or stack with examples of what you are after?


Good day Jessica,

it appears as if the slices in question are not really “white”, i.e. they may contain pixels with values ≠ 255.

So please post at least two slices of a stack:

  1. A slice with a typical structure
  2. A slice that you judge being “white”

Please post the slice images in their original RAW- or TIF-file format.
No JPG-format though, because JPG introduces artifacts!
(Converting a JPG-compressed image to TIFF- or PNG-format doesn’t make sense.)
You may also post images as ZIP-archives or make them accessible via a dropbox-like service.

The task to eliminate the slices in question from stacks in an automatic fashion can be solved if we know exactly how the “white” slices really look like and how they differ from the slices with structured contents.

To separate both types of slices can be rather easy or may need a more adavnaced analysis concerning the degree of structure. Without seeing typical slices we can’t telll you what to do.




Dear Herbie,
thanks a lot for your suggestioins, you are absolutelly right!

Here are two example pictures. One lacks structures I am interested in, the other one contains structures. Normally I adjust brightness and contrast when I analyse them, then they appear white, but these are the original ones.

When taking these pictures I select a certain area on a microscopic slide and then I let the machine take the Images with a certain magnification. This is something you would also do as a first step, if you want to do a Multiple Image Alignment. Therefore I get a lot of These “empty” Images which I would like to exclude without looking at every picture in order to remove it by hand.



Thanks for posting the images.

The “white” image shows a noisy gray value of Mean ≈ 125 and StdDev ≈ 17.7;

My first attempt would be to eliminate images showing typical statistical features.
With an ImageJ-macro this should be possible.

Loop through the slices of the stack and for every slice:

  1. Get the statistical features
  2. Compare the features to those of a “white” prototype
  3. Decide whether to keep or eliminate

One may start with simple statistical features such as the StdDev, Skewness, and Kurtosis.

I shall provide you with a macro-template that does this.

Please stay tuned



Hello Herbie,

thank you so much for your effort, I really appreciate that!

Best regards



here is a sample stack with 12 slices as a ZIP-archive (13.0 MB)
that (after un-zipping it) you may use to test the following ImageJ-macro:

// imagej-macro "stackClean" (Herbie G., 06. Dec. 2018)
requires( "1.52i" );
run("Duplicate...", "title=temp duplicate");
do { 
   if (sd<sdevThr && krt>kurtThr ) run("Delete Slice"); else i++;
} while (i<=nSlices);
// imagej-macro "stackClean" (Herbie G., 06. Dec. 2018)

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it with the sample stack open in ImageJ.

The resulting stack has 8 slices, i.e. 4 “white” slices have been eliminated from the original sample stack.

Please note that the macro checks for two statistical values, the Standard Deviation and the Kurtosis. The values are checked versus the respective threshold values defined at the beginning of the macro. For the sample stack a “white” slice is detected and deleted
if its sd < 5 and if its krt > 30;

You may choose the threshold values to fit your needs.

Just measure “white” and structured slices of your stack with:

The threshold values should be between the values you get for the “white” and a typical structured slice.





This is great, thanks a lot for your help!
I will try out the macro with a couple of my stacks and tell you how it works.




in any case please try the macro with the provided sample stack first and report back if it works as expected!




Yes, it worked excellent. Therefore I will try it with some of my “real” stacks :smiley:


Just a hint:

Perhaps youu try with the Standard Deviationa alone:

Replace these two code lines

   if (sd<sdevThr && krt>kurtThr ) run("Delete Slice"); else i++;

by this line

   if (sd<sdevThr) run("Delete Slice"); else i++;




Another approach with the Background subtraction, which works with your stack. Adjust the mean threshold (I used 5 here) to suit your needs.

title1 = getTitle();
run("Duplicate...", "title=temp duplicate");
run("Subtract Background...", "rolling=50 stack");
n = nSlices;
for(i=n; i>0; i--) {
	if(avg < 5) {
		run("Delete Slice");



Hi Herbie,

I tried the macro version with the sd only and it works perfectly. Thanks a lot, again!
You saved me so much time!

@vedsharma: Also many thanks to you for the other Version! If I come into trouble with Herbies macro, I will test yours.

Best regards