# How To Make Midpoint Filter For Images – C# Guide

Midpoint filter is a order-statistic filter and we use it to process image data in spatial domain. This guide shows how to make it with C#.

Andraz Krzisnik
How To Make Midpoint Filter For Images...

We use midpoint filter for processing image data in spatial domain. Therefore, we need to compute output pixel intensity values using local neighborhoods of pixel values from input image.

Each spatial filter works by taking a set of pixel values and outputing a single pixel value. Furthemore, it acts like a small window we place on top of an image. In order to filter the entire image, we have to move this window across the entire input image.

However, processing images with spatial filters will produce image with slightly smaller dimensions. So, if we want to counteract this, we should use zero padding on the input image before we filter it. This way we will compensate the loss of pixels and end up with the same sized image as input one.

## Midpoint filter

This filter is one of the order-statistic filters, which are all spatial filters. Furthermore, the output intensity values they produce aren’t computed, but chosen from ordered existing image data.

We already talked about median, max and min filters, which belong among order-statistic filters. Midpoint filter is kind of a mixture of max and min filter and arithmetic mean filter, which we also talked about in another post.

What it does, it basically takes minimum and maximum values from the local set, encompassed by the filter, and divides it by 2. To put this in more mathematical terms, let’s take a look at the following formula.

The g(s, t) in the formula represents intensity value, which has a position on coordinates s and t. These coordinates are relative to the filter. In other words, position of the filter doesn’t affect the intensity value of each output pixel, only values it encompasses do.

## C# code

Function that does all the work consists of a few nested for loops. As you will see in the code, other two for loops are there for moving the filter across the image. While inner three are there for processing image data inside the filter.

Each spatial filter outputs intensity values in the center position of the filter and we apply it three times, once for each color channel.

``````public static Bitmap Filter(this Bitmap image)
{
int w = image.Width;
int h = image.Height;

BitmapData image_data = image.LockBits(
new Rectangle(0, 0, w, h),
PixelFormat.Format24bppRgb);
int bytes = image_data.Stride * image_data.Height;
byte[] buffer = new byte[bytes];
Marshal.Copy(image_data.Scan0, buffer, 0, bytes);
image.UnlockBits(image_data);

int r = 1;
int wres = w - 2 * r;
int hres = h - 2 * r;

Bitmap result_image = new Bitmap(wres, hres);
BitmapData result_data = result_image.LockBits(
new Rectangle(0, 0, wres, hres),
ImageLockMode.WriteOnly,
PixelFormat.Format24bppRgb);
int res_bytes = result_data.Stride * result_data.Height;
byte[] result = new byte[res_bytes];
for (int x = r; x < w - r; x++)
{
for (int y = r; y < h - r; y++)
{
int pixel_location = x * 3 + y * image_data.Stride;
int res_pixel_loc = (x - r) * 3 + (y - r) * result_data.Stride;
double[] median = new double[3];
byte[][] neighborhood = new byte[3][];

for (int c = 0; c < 3; c++)
{
neighborhood[c] = new byte[(int)Math.Pow(2 * r + 1, 2)];
for (int kx = -r; kx <= r; kx++)
{
for (int ky = -r; ky <= r; ky++)
{
int kernel_pixel = pixel_location + kx * 3 + ky * image_data.Stride;
}
}
}

for (int c = 0; c < 3; c++)
{
result[res_pixel_loc + c] = (byte)((neighborhood[c].Min() + neighborhood[c].Max()) / 2);
}
}
}

Marshal.Copy(result, 0, result_data.Scan0, res_bytes);
result_image.UnlockBits(result_data);
return result_image;
}
``````

## Conclusion

Midpoint filter is useful for randomly distributed noise like Gaussian or uniform noise. However it will slightly blur the image while doing its thing.

### Related Articles

#### C# Tutorial: How To Apply Erosion To An Image

Erosion is a morphological process, where pixels at an object boundaries are removed. Erosion and dilation are a pair of basic morphological transformations, which are completely...

Posted on by Andraz Krzisnik

#### C# Tutorial: Contrast Stretching with Normalization

This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...

Posted on by Andraz Krzisnik