Background Subtraction With Rolling Ball

The rolling ball algorithm removes uneven background from microscopy images by conceptually rolling a sphere underneath the image surface and subtracting it. It was described by Alan Day in a 1993 paper at the University of Dundee, and it ended up becoming the default background removal method in ImageJ and Fiji. The idea is straightforward, but getting it right requires understanding what the radius actually does and when the method falls apart. Imagine the image as a terrain map where pixel brightness is height. A ball of a certain radius rolls along the bottom of that terrain. The path traced by the bottom of the ball becomes the estimated background. Subtract that from the original image and you get the foreground removed from the background. The radius parameter is everything. Set it too low and fine structures get eaten into the background estimate. Set it too high and large dim features disappear entirely. A good starting point is roughly 1.5 times the diameter of the objects you care about. If your cells are about 20 pixels across, try a radius around 30 to 40. From there, adjust until the background looks flat and the objects remain intact.

I ran into a situation last year where I was working with immunofluorescence images of neuronal cultures. The background had a slow undulating gradient from the coverslip thickness variation, but the dendrites were thin and faint. I started with a rolling ball radius of 50 and every dendrite segment was getting subtracted. Dropped it to 20, and the background still looked okay. The trick was realizing that the effective subtraction also depends on the ball traversing the image diagonally — larger diagonal paths make the algorithm approximate the background more aggressively. Switching to a non-recursive method with a smaller radius fixed it.

Running Rolling Ball in Fiji

If you are using Fiji, this is already built in. Open your image, go to Process > Subtract Background, and you will see the rolling ball dialog. Set the radius in pixels. Choose whether the preview should show the subtracted result or the estimated background. There is also a checkbox for stack processing if you are working with z-stacks or time series. Check it if you need the background calculated per-slice rather than across the whole stack at once. The non-recursive option is usually the right choice for most fluorescence images. The recursive variant can produce slightly different results on very noisy data but it tends to over-subtract. One thing people consistently miss is that the rolling ball operates on each channel independently in multichannel images. If you have a merge of three fluorescence channels, you need to run the subtraction on each channel separately or use the stack option after splitting. Running it on a merged RGB image will treat all channels as a single intensity field and you will get garbage results.

Get the Full Details

Rolling Ball Game Scratch at Gerald Anderson blog
Rolling Ball Game Scratch at Gerald Anderson blog

When rolling ball fails

It does not work well when the foreground fills a significant portion of the image. If more than about half the pixels are signal, the ball cannot distinguish between background and foreground reliably. The algorithm will start subtracting parts of your actual objects. In those cases, consider using a top-hat transform or fitting a polynomial surface to the background manually. Another failure mode is sharp edges and hard borders in the background. The rolling ball assumes the background varies smoothly relative to the ball size. If you have a hard rectangular region of high background, like a well edge in a plate reader image or a mask boundary, the ball will get stuck on that edge and produce a distorted subtraction near it. I dealt with this once on a plate-based assay image where the well boundary created a bright ring that the ball kept trying to follow. I cropped out the well borders before running the subtraction and then pasted the results back. That solved it without needing a more complex method. Highly sparse images with isolated bright spots against an otherwise flat background also do not benefit much. The rolling ball has nothing to roll on except the noise floor, and the result is often no different from a simple Gaussian blur subtraction at the same scale. Use that instead and save yourself the parameter tuning.

Practical workflow tips

Always work on 8-bit or 16-bit grayscale images. Convert first if needed. The rolling ball method does not handle floating point or 32-bit images directly in Fiji without conversion. Running it on 32-bit data without converting can silently produce incorrect values or just crash depending on your build. Preview before committing. The rolling ball subtraction is destructive unless you check the do not replace box or use Process > Duplicate first. I typically duplicate the image, run the subtraction on the copy, and compare side by side. The visual difference is immediate and saves you from having to redo the whole pipeline if the radius is wrong. For batch processing, wrap the command in a macro. One line in FIJI macro language sets the radius and runs it across a folder of images. It cuts a manual operation that would take maybe twenty minutes for ten images down to under a minute. Something like:

run("Subtract Background...", "rolling=30 stack"); Place that inside a loop over file names and you are done. The stack flag handles z-stacks in the same command.

Rolling Ball - Play Rolling Ball On Slope
Rolling Ball - Play Rolling Ball On Slope

Getting it

You do not need to download anything specific for the Rolling Ball algorithm. It comes standard with Fiji, which you can get from fiji.sc. Install Fiji, open any image, and the option is already there under Process > Subtract Background. No plugin installation required. If you are working in Python, the scikit-image library has a similar function called white_tophat, and pyimagej provides a bridge to run the actual Fiji implementation directly from a Jupyter notebook if you want the exact same behavior. The method is not perfect and it will not fix every background problem you encounter. But for standard fluorescence microscopy with moderate signal density and smooth background variation, it is usually the fastest thing that works. Set the radius based on object size, avoid the edge cases I mentioned, and you should get clean results without spending hours on it.