# Minimum, Maximum, and Median Filters

## Morphological Filters: Minimum and Maximum

Morphological image processing is a technique introducing operations for transforming images in a special way which takes image content into account. The most common morphological operations are minimum (also known as dilation) and maximum (erosion) filters. The minimum filter extends object boundaries, whereas the maximum filter erodes shapes on the image.

In morphological filters, each pixel is updated based on comparing it against surrounding pixels in the running window. The running window is an image area around a current pixel with a defined radius. For example, if we specify the radius = 1, the running window will be a 3x3 square around the target pixel.

Transformations differ for all morphological operations.

### The Minimum Filter

The transformation replaces the central pixel with the darkest one in the running window.

For example, if you have text that is lightly printed, the minimum filter makes letters thicker.

The following code example makes the text bold using the Minimum(Int32) method as shown in the picture below:

C#
bitmap.Transforms.Minimum(2);


The picture below shows the result of the snippet:

Here is the same example using Minimum transform:

C#
using (var bitmap = new Bitmap(@"Images\in.png"))
using (var minimum = new Minimum())
{
using (var newbitmap = minimum.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.png");
}


### The Maximum Filter

The maximum and minimum filters are shift-invariant. Whereas the minimum filter replaces the central pixel with the darkest one in the running window, the maximum filter replaces it with the lightest one.

For example, if you have a text string drawn with a thick pen, you can make the sign skinnier.

The following two snippets use the Maximum(Int32) method and the Maximum class, respectively, to make the text lighter:

C#
bitmap.Transforms.Maximum(2);

C#
using (var bitmap = new Bitmap(@"Images\in.png"))
using (var maximum = new Maximum(2))
using (var newbitmap = maximum.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.png");


The picture below shows the result of these snippets:

### Other Morphological Operations

Graphics Mill provides only two morphological filters. However, you can get the opening and closing transformations by combining the minimum and maximum filters.

The opening filter is comprised of the maximum filter followed by the minimum one. The opening filter is used to remove small objects while preserving large shapes on the image, as the picture below illustrates:

This picture was produced with the following snippet:

C#
bitmap.Transforms.Maximum(1);
bitmap.Transforms.Minimum(1);


The closing filter consists of the minimum filter followed by the maximum one. The closing filter can be used for smoothing images. For example, you have a sketch drawn with a pen. You can see the result after applying the opening filter on the following picture on the right:

This image was produced with the following code example:

C#
bitmap.Transforms.Minimum(1);
bitmap.Transforms.Maximum(1);


## The Noise Filter: Median

The median filter is a very popular image transformation which allows the preserving of edges while removing noise.

Just like in morphological image processing, the median filter processes the image in the running window with a specified radius, and the transformation makes the target pixel luminosity equal to the mean value in the running window. This filter works well for removing noise, especially impulse noise. The following code example uses the Median(Int32) method to reduce "salt and pepper" noise as shown in the picture below:

C#
bitmap.Transforms.Median(1);


Here is the same example using the Median class:

C#
using (var bitmap = new Bitmap(@"Images\in.png"))
using (var median = new Median(1))
using (var newbitmap = median.Apply(bitmap))
newbitmap.Save(@"Images\Output\out.png");