The maximum filter replaces each pixel value of a Digital Image with the maximum value(i.e., the value of the brightest pixel) of its neighbourhood pixel window. It is the opposite of what the minimum filter does to an Image.
Applying the maximum filter removes the negative outlier noise present in a Digital Image.
When a maximum filter is applied, the darker objects present in the image are eroded. This way maximum filter is called an erosion filter. With respect to the lighter pixels, some call this as a dilation filter.
To be clear, brighter objects are dilated and the darker objects are eroded upon applying a maximum filter to a Digital Image.
Applying max filter using Pillow:
- To apply any filter to an Image using Python and Pillow, the first step is to load the image from a file using Image.open(). This creates an Image object and loads the image information into it.
- The ImageModule of Pillow - the Python Image Processing Library provides various filters that can be applied onto a Digital Image. The collection of filters include maximum filter, minimum filter, median filter, blur filter, sharpening filter, smoothening filter and other filters.
- Invoking the filter() method with the name of a filter class applies the required filter to an Image. In the example provided, filter() method is called with ImageFilter.MaxFilter.
- The example Python program applies ImageFilter.MaxFilter ten times in a for loop. In the resultant image, the black regions are eroded with brighter pixels.
#----- Python example program for applying a maximum filter to a digital image -----
# Method to apply the filter
# Load the image
# Apply maximum filter
# Display images
Before applying the maximum filter to the Digital Image using Python and Pillow:
After applying the maximum filter 10x times to the Digital Image using Python and Pillow: