Scikit Image − Rolling-ball Algorithm



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 plt
from skimage import io, restoration

# Load an image
image = io.imread('Images/Cloud.jpg')

# Use the rolling-ball algorithm to estimate the background
background = restoration.rolling_ball(image)

# Create a figure with three subplots for visualization
fig, 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 background
ax[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 - background
ax[2].imshow(result, cmap='gray')
ax[2].set_title('Result (Original - Background)')
ax[2].axis('off')

plt.tight_layout()
plt.show()

Output

Rolling Algorithm

Example

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

import matplotlib.pyplot as plt
from 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 algorithm
background_inverted = restoration.rolling_ball(image_inverted, radius=45)

# Calculate the filtered image by subtracting the estimated background from the inverted image
filtered_image_inverted = image_inverted - background_inverted

# Invert both the filtered image and the estimated background to restore their original orientation
filtered_image = util.invert(filtered_image_inverted)
background = util.invert(background_inverted)

# Create a figure with three subplots for visualization
fig, 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 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

Rolling Algorithm

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 plt
from skimage import io, restoration

# Load an image
image = io.imread('Images/Cloud.jpg')

# Create an ellipsoid-shaped kernel with specified dimensions
kernel_size = (70.5 * 2, 70.5 * 2)
kernel_radius = 70.5 * 2
kernel = restoration.ellipsoid_kernel(kernel_size, kernel_radius)

# Use the rolling-ball algorithm to estimate the background
background = restoration.rolling_ball(image, kernel=kernel)

# Create a figure with three subplots for visualization
fig, 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 background
ax[1].imshow(background, cmap='gray')
ax[1].set_title('Background')
ax[1].axis('off')

# Calculate and plot the resulting image (original - background)
result = image - background
ax[2].imshow(result, cmap='gray')
ax[2].set_title('Result (Original - Background)')
ax[2].axis('off')

plt.tight_layout()
plt.show()

Output

Rolling Algorithm
Advertisements