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

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

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

###
How To Add Salt And Pepper Noise On Image – C# Guide

###
How To Make Uniform Noise On Images – C# Guide

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.

Image Noise

Salt and pepper noise or impulse noise is one of the noise models we can use to simulate image data corruption in real life.

Image Noise

Uniform noise is one of the noise models we can use to simulate real life data corruption. This guide shows how to make in on images.

How To Make Alpha Trimmed Mean Filter For...

We use alpha trimmed mean filter to process image data in spatial domain. Furthermore, it’s one of the most useful order-statistic filters, because we can use it to reduce combined noises from images.

Order-statistic filters are spatial filters and this one is no exception. Therefore, to process image in spatial domain, we will need to move the filter across it. In other words, spatial filters act as a small window that encompasses pixels that fit inside.

It processes the encompassed image data inside it and outputs one intensity value per filter position. So we need to move it one pixel at a time to filter the entire image. Because of this process, our output image will have slightly smaller dimensions.

We can counteract this by zero padding our image and expand its dimensions before filtering to compensate for the loss.

This filter is a special case, because it can reduce either to arithmetic mean filter or to median filter. It depends on *d* variable we’ll see in the following formula. It basically trims lowest and highest intensity values from the set.

Okay, let’s break it down. The *g _{r}(s, t)* function represents all the remaining intensity values in the set after, we’ve already removed extreme values.

The *mn* is the product of filter dimensions, which also means, it’s the total of all the values inside the filter. Therefore, if we set the *d* variable to *0*, our filter becomes the same as arithmetic mean filter. On the other hand, if we set it to *mn – 1*, the filter becomes the median filter.

I wrote a function, which takes all values encompassed by the filter and stores them in array. After that, it removes the same amount of lowest and highest intensity values and computes output intensity value. To set the *d* variable, we set it as function parameter, but we need to set it’s halved value.

```
public static Bitmap Filter(this Bitmap image, int half_d)
{
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 = 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];`

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

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

` {`

` neighborhood[c] = new int[(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 i = 0; i < half_d; i++)`

` {`

` int min_idx = Array.IndexOf(neighborhood[c], neighborhood[c].Min());`

` int max_idx = Array.IndexOf(neighborhood[c], neighborhood[c].Max());`

` neighborhood[c] = neighborhood[c].Where((val, idx) => idx != min_idx).ToArray();`

` neighborhood[c] = neighborhood[c].Where((val, idx) => idx != max_idx).ToArray();`

` }`

` }`

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

` {`

` result[res_pixel_loc + c] = (byte)((1 / (Math.Pow(2 * r + 1, 2) - 2 * half_d)) * neighborhood[c].Sum());`

` }`

` }`

` }`

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

` result_image.UnlockBits(result_data);`

` return result_image;`

` }`

Alpha trimmed mean filter is kind of an universal order-statistic/mean filter, since we can change it into others. But not only that, it can also become something in between and that’s why it even trumps median filter.

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