Scikit Image - Finding Local Maxima



In image processing, a maxima or maximum refers to the points or regions in a digital image where the pixel values are at their highest within a specific neighborhood. In contrast, finding maxima in an image involves identifying points or regions where the values are at their highest within a local neighborhood. Local maxima are significant peaks or high-intensity areas within the data. Finding maxima plays a significant role in various image analysis tasks, such as feature detection, object segmentation, and image enhancement.

The scikit image libraries provide functions like local_maxima() and h_maxima to find the local maxima in an image.

Using the skimage.morphology.local_maxima() function

The local_maxima() function is used to find local maxima in an n-dimensional array (image). Local maxima are defined as connected sets of pixels with equal gray levels (plateaus) that are strictly greater than the gray levels of all pixels in their neighborhood.

Syntax

Following is the syntax of this function −

skimage.morphology.local_maxima(image, footprint=None, connectivity=None, indices=False, allow_borders=True)

Parameters

  • image (ndarray): This is the n-dimensional array on which local maxima will be calculated.
  • footprint (ndarray, optional): The footprint, also known as the structuring element, is used to determine the neighborhood of each evaluated pixel. It must be a boolean array and have the same number of dimensions as the image. If neither footprint nor connectivity are given, all adjacent pixels are considered as part of the neighborhood.
  • connectivity (int, optional): This parameter is used to determine the neighborhood of each evaluated pixel. It specifies how pixels are considered neighbors. Pixels whose squared distance from the center is less than or equal to connectivity are considered neighbors. This parameter is ignored if the footprint is not None.
  • indices (bool, optional): If True, the output will be a tuple of one-dimensional arrays representing the indices (coordinates) of local maxima in each dimension. If False, the output will be a boolean array with the same shape as the image, where True indicates the position of local maxima, and False indicates other positions.
  • allow_borders (bool, optional): If True, it allows plateaus that touch the image border to be considered as valid maxima. If set to False, maxima that are on the image borders are not considered.

Return Value

It returns returns maxima, which can be either an ndarray or a tuple of ndarrays, depending on the value of the indices parameter.

Example

The following example demonstrates how to use the local_maxima() function to find local maxima in a grayscale image.

import numpy as np
from skimage.morphology import local_maxima
from skimage.measure import label
import matplotlib.pyplot as plt
from skimage import io, color, exposure 

# Load the input image
image = io.imread('Images/Tajmahal.jpg')

# For illustration purposes, we focus on a specific region of the image.
x_0 = 170
y_0 = 100
width = 250
height = 150

# Crop the image to the specified region
image = color.rgb2gray(image)[y_0:(y_0 + height), x_0:(x_0 + width)]

# Rescale the image intensity for visualization purposes.
image = exposure.rescale_intensity(image)

# Find local maxima by comparing to all neighboring pixels (maximal connectivity)
maxima1 = local_maxima(image)

# Label the local maxima to distinguish them
label_maxima1 = label(maxima1)

# Overlay the labeled local maxima on the original image
overlay = color.label2rgb(label_maxima1, image, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)])

# Plot the original image and the results
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

# Display the original binary image
ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].axis('off')
ax[0].set_title('Original Image')

# Display the Thinned image
ax[1].imshow(overlay, cmap=plt.cm.gray)
ax[1].axis('off')
ax[1].set_title('Local Maxima (Maximal Connectivity)')

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

In this image, you can notice that there were numerous local maxima, often arising from image noise. To address this, the h_maxima() function can be used to find local maxima while considering a minimum height requirement, denoted as "h." This 'h' value represents the difference in gray levels that must be descended from a point to reach a higher maximum and serves as a measure of local contrast.

Using the skimage.morphology.h_maxima() function

The h_maxima() function is used to find all maxima in an image that have a specified minimum height threshold (h). These local maxima are connected sets of pixels with gray levels strictly greater than the gray level of all pixels in their direct neighbourhood.

A local maximum M with a height of h is defined as a point where there exists at least one path connecting M to another local maximum that is either equal to or higher in value. Along this path, the minimum value should not decrease by more than h compared to the value at M. Additionally, there should be no path to any other equal or higher local maximum where the minimal value along that path is greater than M's value minus h. The global maxima of the image are also found by this function.

Syntax

Following is the syntax of this function −

skimage.morphology.h_maxima(image, h, footprint=None)

Parameters

  • image (ndarray): This is the input image on which maxima will be calculated.
  • h (unsigned integer): h is the minimal height threshold. Only maxima with a height (intensity difference) greater than or equal to h will be considered valid maxima.
  • footprint (ndarray, optional): This parameter specifies the neighborhood around each pixel that will be considered when determining if a pixel is a maximum. The neighborhood is expressed as an n-dimensional array of 1s and 0s. By default, it uses a neighborhood that corresponds to a 3x3 square for 2D images and a 3x3x3 cube for 3D images, following the maximum norm.

Return Value

It returns h_max, which is an ndarray representing the local maxima of height greater than or equal to h, including the global maxima. The resulting image is binary, where pixels belonging to the determined maxima are set to 1, and others are set to 0.

Example

The following example demonstrates how to identify maxima (peaks) in a specific region of an image using the h-maxima() function.

import numpy as np
from skimage.morphology import h_maxima
from skimage.measure import label
import matplotlib.pyplot as plt
from skimage import io, color, exposure

# Load the input image
image = io.imread('Images/Tajmahal.jpg')

# For illustration purposes, we focus on a specific region of the image.
x_0 = 170
y_0 = 100
width = 250
height = 150

# Crop the image to the specified region
image = color.rgb2gray(image)[y_0:(y_0 + height), x_0:(x_0 + width)]

# Rescale the image intensity for visualization purposes
image = exposure.rescale_intensity(image)

# Set the h parameter for h_maxima
h = 0.05

# Find h-maxima in the cropped and rescaled image
h_maxima_image = h_maxima(image, h)

# Label the h-maxima to distinguish them
labeled_h_maxima = label(h_maxima_image)

# Overlay the labeled h-maxima on the original image
overlay = color.label2rgb(labeled_h_maxima, image, alpha=0.7, bg_label=0, bg_color=None, colors=[(1, 0, 0)])

# Create subplots for displaying the original image and h-maxima
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
ax = axes.ravel()

# Display the original image
ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].axis('off')
ax[0].set_title('Original Image')

# Display the h-maxima overlay
ax[1].imshow(overlay, cmap=plt.cm.gray)
ax[1].axis('off')
ax[1].set_title('h-Maxima (h={})'.format(h))

plt.tight_layout()
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements