A local maximum is a pixel or a particular region in an image that has a higher intensity or value than its neighboring pixels or regions. It represents a peak or a high point in the image data.
One way to find local maxima is by performing a local neighborhood analysis. For each pixel in the image, its neighborhood is examined, and if the pixel is the maximum within its neighborhood, it is considered a local maximum.
Local Maxima in an Image in Mahotas
We can find the local maxima in an image in Mahotas using the locmax() function. It takes an image as input and returns a binary mask where the local maxima are marked as True or 1.
The local_maxima() function in Mahotas uses a non-maximum suppression algorithm to locate the local maxima efficiently. By examining each pixel and its neighborhood, the function determines whether the pixel is the maximum within its local region.
This analysis allows for the detection of peaks or high points in the image data, which can play an important role for various applications such as feature extraction, object detection, and image segmentation.
The non−maximum suppression algorithm is used in object detection and edge detection tasks to eliminate redundant and weak detections by selecting the highest intensity or response value among neighboring pixels, thereby keeping only the local maxima and suppressing non−maximum values.
Using the locmax() Function
The locmax() function in Mahotas is used to identify local maxima in an input image efficiently. It takes a grayscale or single channel image as input and returns a binary mask where the local maxima are marked as True or 1.
Syntax
Following is the basic syntax of the locmax() function in mahotas −
mahotas.Locmax(image_name)
where, ”image_name” is the input image.
Example
Following is the basic example to find the local maxima in an image −
import mahotas as mh import numpy as np from pylab import imshow, show import matplotlib.pyplot as plt image = mh.imread(''nature.jpeg'', as_grey=True) maxima = mh.locmax(image) print("Maxima:", maxima) imshow(maxima) show()
Output
Following is the output of the above code −
Maxima: [[ True True True ... True True True] [ True True True ... True True True] [ True True True ... True True True] ... [False False False ... False False False] [False False False ... False False True] [ True False True ... False False True]]
The image displayed is as shown below −
Using the regmax() Function
We can also use the regmax() function in Mahotas for finding regional maxima in an image. A regional maximum is defined as a point in the image that has a higher intensity value than all of its neighboring pixels within a specified region.
The regmax() function accepts an image as the input parameter and returns a boolean image of the same size as an input image.
Following is the basic syntax of regmax function in mahotas −
regmax(image)
where, ”image” is a grayscale or color image on which regional maxima need to be identified.
Example
In here, we are trying to find the regional maxima within the connected regions in a grayscale image using the regmax() function −
import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(''sun.png'', as_grey=True) local_maxima_points = find_local_maxima(image) print(local_maxima_points)
Output
Output of the above code is as follows −
[[False False False ... False False False] [False False False ... False False False] [False False False ... False False False] ... [False False False ... False False False] [False False False ... False False False] [False False False ... True False False]]
Regional Maxima of a Colored Image
We can also find the regional maxima within the connected regions in a color image using the regmax() function.
Example
Now, we are trying to find the regional maxima within the connected regions in a color image using the regmax() function −
import mahotas as mh def find_local_maxima(image): regional_maxima = mh.regmax(image) return regional_maxima image = mh.imread(''tree.tiff'') local_maxima_points = find_local_maxima(image) print(local_maxima_points)
Output
We get the output as follows −
[[[False False False] [ True True True] [False False False] ... [False False False] [False False False] [False False False]] . . . [False False False] [False False False] [ True False False]]]