### How To Make Adaptive Median Filter For Images With C#

Adaptive median filter is much more effective at removing impulse noise, also known as salt and pepper noise, than traditional median filter.

Filter by Category

- C# Tutorial(85)
- C# Image Processing(69)
- Morphological Processes(20)
- Image Processing(16)
- Image Restoration and Reconstruction(16)
- Image Segmentation(13)
- C# Data Structures And Algorithms(11)
- Color Image Processing(8)
- Frequency Domain Filtering(8)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Order-Statistic Filters(4)
- Mean Filters(4)
- Sorting Algorithms(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- RGB to HSI Color Model(2)
- Adaptive Filters(2)
- Tone and Color Corrections(2)
- Linked Lists(2)
- Stacks(1)
- Queues(1)
- Point Detection(1)
- Line Detection(1)
- C# Arrays(1)
- Region Segmentation Using Superpixels(1)
- Region Segmentation With K Means Clustering(1)
- Region Splitting And Merging(1)
- Sorted Lists(1)
- Region Growing Segmentation(1)
- Digital Image Watermarking(1)
- Using Color In Image Segmentation(1)
- Social Games(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Landing Pages(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- C# Basics(1)

Back to Latest Articles
###
How To Make Adaptive Median Filter For Images With C#

###
How To Make Adaptive Filters For Local Noise In C#

###
How To Make Alpha Trimmed Mean Filter For Images

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

###
How To Make Max And Min Filter In Image Processing

###
How To Use Median Filter On Images – C# Guide

###
How To Use Contraharmonic Mean Filter – C# Guide

###
How To Use Harmonic Mean Filter On Images – C# Guide

###
How To Use Geometric Mean Filter On Image – C# Guide

###
How To Use Arithmetic Mean Filter On Images – C# Guide

Adaptive Filters

Adaptive median filter is much more effective at removing impulse noise, also known as salt and pepper noise, than traditional median filter.

Adaptive Filters

Adaptive local noise reduction filters are useful for processing images that have too much noise to deal with with other simpler filters.

Order-Statistic Filters

Alpha trimmed mean filter is a combination of mean filters and order-statistic filters. This guide shows how to use them with C#.

Order-Statistic Filters

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#.

Order-Statistic Filters

Max and min filter is one of the order-statistic filter we can use to process image data in spatial domain. This tutorial shows it with C#.

Order-Statistic Filters

Median filter is one of the order-statistic filters for processing image data in spatial domain. This guide shows how to apply it with C#.

Mean Filters

We can use contraharmonic mean filter to process image data in spatial domain. It's most effective against salt and pepper noise.

Mean Filters

Harmonic mean filter is of mean filters we can use to process image data in spatial domain. This guide shows how to apply it with C#.

Mean Filters

Geometric mean filter is one of mean filters we can use processing images in spatial domain. We use C# programming language to apply it here.

Mean Filters

Arithmetic mean filter is one of the simplest mean filters we could use to reduce noise from an image. Learn more about spatial filtering.

How To Make Adaptive Median Filter For...

We can use adaptive median filter for processing image data when traditional median filter fails to do the job. Furthermore, it can also preserve details and smooths non-impulsive noise from images.

Like any other spatial filter, it acts as a small window which encompasses a set of intensities on an image. Whatever the position on the image, it takes those intensity values and computes a new output one.

So to render the entire output image, we need to move it across the entire input image, one pixel at a time. In addition to that, it’s also more complicated, which means it needs more time to get the job done.

Adaptive filters change behaviour based on statistical characteristics of intensities inside filter region. This is also the reason why they have superior performance. However, they are still useful just for images which have only noise degradation.

Just like traditional median filter, is this also really good for removing salt and pepper noise. But this one is much better for this job. While traditional filter can’t handle impulse noise with probability distribution above 20 percent, adaptive median filter still works above that.

Adaptive filters, in general, work for multiple types of noises. So, while this filter works great for salt and pepper noise, it also preserves detail when smoothing other types of noises.

Adaptive median filter has quite complicated filter operations in comparison to other adaptive filter. Furthermore, it includes filter size increase during these operations, which is a kind of a special thing for this filter.

Filter operations work in two stages, which we can see from the following pseudo code.

Okay, let’s break it down. We’ll start with *z* variables, which represent intensity values. Furthermore, as we can see, they all have extra notations. So med notation stands for median, which represents the median value inside the filter.

We can also see that there are *min* and *max*, which are lowest and highest value inside the filter region. And lastly, xy notation represents the intensity value positioned on the x and y coordinates. It also has position in the center of the filter.

Another unknown is *S _{max}*, which represents maximum filter size. This is important because of the increasing filter size thing.

```
public static Bitmap Filter(this Bitmap image, int rmax)
{
int w = image.Width;
int h = image.Height;
```` BitmapData image_data = image.LockBits(`

` new Rectangle(0, 0, w, h),`

` ImageLockMode.ReadOnly,`

` 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 = 3;`

` 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;`

` byte[][] neighborhood = new byte[3][];`

` for (int c = 0; c < 3; c++)`

` {`

` neighborhood[c] = new byte[(int)Math.Pow(2 * r + 1, 2)];`

` int added = 0;`

` 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;`

` neighborhood[c][added] = buffer[kernel_pixel + c];`

` added++;`

` }`

` }`

` }`

` for (int c = 0; c < 3; c++)`

` {`

` if (neighborhood[c].median() > neighborhood[c].Min() && neighborhood[c].median() < neighborhood[c].Max())`

` {`

` if (neighborhood[c].Min() < buffer[pixel_location + c] && buffer[pixel_location + c] < neighborhood[c].Max())`

` {`

` result[res_pixel_loc + c] = buffer[pixel_location + c];`

` }`

` else`

` {`

` result[res_pixel_loc + c] = (byte)neighborhood[c].median();`

` }`

` }`

` else`

` {`

` r++;`

` x = r;`

` y = r;`

` if (r > rmax)`

` {`

` result[res_pixel_loc + c] = (byte)neighborhood[c].median();`

` }`

` }`

` }`

` }`

` }`

` Marshal.Copy(result, 0, result_data.Scan0, res_bytes);`

` result_image.UnlockBits(result_data);`

` return result_image;`

` }`

This filter aims to remove salt and pepper noise, smooths other noises while preserving details. It also reduces distortions like thickening or thinning.

I hope this guide was helpful. You can also **download the project** and try it out on your own images.

## Comments