Scikit Image ? Rolling-ball Algorithm (2025)

Previous

Quiz AI Version

Next

The rolling-ball algorithm is a powerful tool for estimating the background intensity of a grayscale image with uneven exposure. This algorithm was originally proposed by Stanley R. Sternberg in 1983, and this algorithm is frequently used in biomedical image processing.

This algorithm works like a filter and is quite intuitive. It treats the image as if it were a surface comprised of unit-sized blocks stacked on top of each other instead of individual pixels. The number of these blocks, and thus the height of the surface is determined by the intensity of the pixel.

To determine the background intensity at a specific pixel position, a ball is submerged under the surface at that position. The apex of the ball, once completely covered by blocks, provides the background intensity at that location. By rolling this ball beneath the surface, background values can be obtained for the entire image.

Scikit-Image Implementation

Scikit-image library implements a general version of this rolling-ball algorithm. Unlike the traditional approach using only balls, this implementation allows the use of arbitrary shapes as kernels. Additionally, it is designed to operate on n-dimensional ndimages, which means it can be applied to a wide range of image types, including RGB images. Furthermore, it provides the flexibility to filter image stacks along any or all spatial dimensions.

Using the skimage.restoration.rolling_ball() function

To estimate the background intensity of an image with uneven exposure, you can utilize the skimage.restoration.rolling_ball() function. This function applies a rolling or translational operation to a kernel, calculating the background intensity for an ndimage.

Syntax

Here's the syntax −

skimage.restoration.rolling_ball(image, *, radius=100, kernel=None, nansafe=False, num_threads=None)

Parameters

Following is the explanation of the parameters of this function −

  • image (ndarray): The input image to be filtered.
  • radius (int, optional): The radius of a ball-shaped kernel that is rolled or translated in the image. If the kernel parameter is provided, this radius parameter is not used.
  • kernel (ndarray, optional): An optional kernel can be provided instead of using the default ball-shaped kernel. This kernel should have the same number of dimensions as the input image. The kernel is filled with the intensity of the kernel at that position.
  • nansafe (bool, optional): By default, this parameter is set to False. If set to False, the function assumes that none of the values in the input image are np.nan. Using nansafe=False can result in a faster implementation.
  • num_threads (int, optional): This parameter specifies the maximum number of threads to use for computation. If None, the function will use the OpenMP default value, typically equal to the maximum number of virtual cores. note: It's an upper limit to the number of threads, and the exact number is determined by the system's OpenMP library.

Return value

The function returns a ndarray background. The estimated background of the input image. This output will be an array of the same shape as the input image.

Example

The following example demonstrates how to use the restoration.rolling_ball function to estimate the background of a dark background image −

import matplotlib.pyplot as pltfrom skimage import io, restoration# Load an imageimage = io.imread('Images/Cloud.jpg')# Use the rolling-ball algorithm to estimate the backgroundbackground = restoration.rolling_ball(image)# Create a figure with three subplots for visualizationfig, ax = plt.subplots(1, 3, figsize=(12, 4))# Plot the original image ax[0].imshow(image, cmap='gray')ax[0].set_title('Original Image')ax[0].axis('off')# Plot the estimated backgroundax[1].imshow(background, cmap='gray')ax[1].set_title('Estimated Background')ax[1].axis('off')# Calculate and plot the resulting image (original - background)result = image - backgroundax[2].imshow(result, cmap='gray')ax[2].set_title('Result (Original - Background)')ax[2].axis('off')plt.tight_layout()plt.show()

Output

Scikit Image ? Rolling-ball Algorithm (1)

Example

This example demonstrates how the rolling-ball algorithm can be applied to images with bright backgrounds −

import matplotlib.pyplot as pltfrom skimage import io, restoration, util# Load an image image = io.imread('Images/hand writting.jpg')image_inverted = util.invert(image)# Estimate the background of the inverted image using the rolling-ball algorithmbackground_inverted = restoration.rolling_ball(image_inverted, radius=45)# Calculate the filtered image by subtracting the estimated background from the inverted imagefiltered_image_inverted = image_inverted - background_inverted# Invert both the filtered image and the estimated background to restore their original orientationfiltered_image = util.invert(filtered_image_inverted)background = util.invert(background_inverted)# Create a figure with three subplots for visualizationfig, ax = plt.subplots(1, 3, figsize=(12, 4))# Plot the original imageax[0].imshow(image, cmap='gray')ax[0].set_title('Original Image')ax[0].axis('off')# Plot the estimated background ax[1].imshow(background, cmap='gray')ax[1].set_title('Estimated Background')ax[1].axis('off')# Plot the resulting filtered image ax[2].imshow(filtered_image, cmap='gray')ax[2].set_title('Result (Filtered Image)')ax[2].axis('off')plt.tight_layout()plt.show()

Output

Scikit Image ? Rolling-ball Algorithm (2)

Specifying a Custom Kernel for the Rolling-Ball Algorithm

The rolling_ball() function in Scikit-Image typically uses a ball-shaped kernel as the default choice. However, in certain scenarios, such as when the intensity dimension varies significantly from the spatial dimensions or when the image dimensions may have different meanings(e.g., one dimension represents a stack counter in an image stack), using a ball-shaped kernel may be too restrictive.

To address such situations, the rolling_ball function includes a kernel argument, which allows you to specify a custom kernel. This kernel should have the same dimensionality as the image but can have a different shape. It's important to note that the dimensionality should match, even if the shape varies.

Scikit-Image provides two default kernels to help with kernel creation −

1. ball_kernel: Specifies a ball-shaped kernel, which is also used as the default kernel.

Its syntax is as follows −

skimage.restoration.ball_kernel(radius, ndim)
  • radius (int): The radius of the ball.
  • ndim (int): The number of dimensions of the ball. It should match the dimensionality of the image.

2. ellipsoid_kernel: Specifies an ellipsoid-shaped kernel.

Its syntax is as follows −

skimage.restoration.ellipsoid_kernel(shape, intensity)
  • shape (array-like): Determines the length of the principal axis of the ellipsoid, excluding the intensity axis. It specifies the dimensions of the ellipsoid kernel.
  • intensity (int): Determines the length of the intensity axis of the ellipsoid.

Example

This example applies the rolling-ball algorithm to the image using the custom ellipsoid-shaped kernel to estimate the background −

import matplotlib.pyplot as pltfrom skimage import io, restoration# Load an imageimage = io.imread('Images/Cloud.jpg')# Create an ellipsoid-shaped kernel with specified dimensionskernel_size = (70.5 * 2, 70.5 * 2)kernel_radius = 70.5 * 2kernel = restoration.ellipsoid_kernel(kernel_size, kernel_radius)# Use the rolling-ball algorithm to estimate the backgroundbackground = restoration.rolling_ball(image, kernel=kernel)# Create a figure with three subplots for visualizationfig, ax = plt.subplots(1, 3, figsize=(12, 4))# Plot the original image ax[0].imshow(image, cmap='gray')ax[0].set_title('Original Image')ax[0].axis('off')# Plot the estimated backgroundax[1].imshow(background, cmap='gray')ax[1].set_title('Background')ax[1].axis('off')# Calculate and plot the resulting image (original - background)result = image - backgroundax[2].imshow(result, cmap='gray')ax[2].set_title('Result (Original - Background)')ax[2].axis('off')plt.tight_layout()plt.show()

Output

Scikit Image ? Rolling-ball Algorithm (3)

Print Page

Previous

Next

Advertisements

Scikit Image ? Rolling-ball Algorithm (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 5748

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.