|The Helpful Mathematics||
+38 044 577 28 76
Alpha-trimmed mean filter
Category. Digital signal and image processing (DSP and DIP) software development.
Abstract. The article is a practical tutorial for alpha-trimmed mean filter understanding and implementation. Article contains theory, C++ source code, programming instructions and sample applications.
Download alpha-trimmed mean filter for Win32 (zip, 603 Kb)
1. Introduction to alpha-trimmed mean filter
Alpha-trimmed mean filter is windowed filter of nonlinear class, by its nature is hybrid of the mean and median filters. The basic idea behind filter is for any element of the signal (image) look at its neighborhood, discard the most atypical elements and calculate mean value using the rest of them. Alpha you can see in the name of the filter is indeed parameter responsible for the number of trimmed elements.
To get acquainted with filter window idea in signal and image processing read our “Filter window, or filter mask” article.
2. Understanding alpha-trimmed mean filter
Now let us see, how to get alpha-trimmed mean value in practice. The basic idea here is to order elements, discard elements at the beginning and at the end of the got ordered set and then calculate average value using the rest. For instance, let us calculate alpha-trimmed mean for the case, depicted in fig. 1.Fig. 1. Alpha-trimmed mean calculation.
Thus, to get an alpha-trimmed mean we are to order elements, eliminate elements at the beginning and at the end of the got sequenced collection and get average of the remaining elements. That is all now alpha-trimmed mean calculated and signal, 1D in our case, filtered by alpha-trimmed mean filter! Let us make resume and write down step-by-step instructions for processing by alpha-trimmed mean filter.
Alpha-trimmed mean filter algorithm:
A couple of words about alpha parameter responsible for trimmed elements. In practice alpha is the number of elements to be discarded, for instance, in our case alpha is two. Since our filter is symmetric one alpha is an even nonnegative number less than size of the filter window. Minimum value for alpha parameter is zero and in this case alpha-trimmed mean filter degenerates into mean filter. Maximum value for alpha is filter window size minus one and in this case filter degenerates into median filter.
Now, when we have the algorithm, it is time to write some code — let us come down to programming.
3. 1D alpha-trimmed mean filter programming
In this section we develop 1D alpha-trimmed mean filter with window of size 5. Let us have 1D signal of length N as input. The first step is window placing we do that by changing index of the leading element:
Pay attention, that we are starting with the third element and finishing with the last but two. The problem is we cannot start with the first element, because in this case the left part of the filter window is empty. We will discuss below, how to solve that problem.
The second step is picking up elements around, ok:
The third step is putting window elements in order. But we will use a code optimization trick here. As soon as we need only middle part of the ordered set we can omit putting in order last α/2 elements. So, it is enough to process just 5-α/2 elements:
To discard elements at the beginning of the ordered set let us define the proper constant:
The final step taking the average:
At last, let us write down the entire algorithm as function:
4. Treating edges
For all window filters there is some problem. That is edge treating. If you place window over first (last) element, the left (right) part of the window will be empty. To fill the gap, signal should be extended. For alpha-trimmed mean filter there is good idea to extend signal symmetrically, like this:Fig. 2. Signal extension.
So, before passing signal to our alpha-trimmed mean filter function the signal should be extended. Let us write down the wrapper, that makes all preparations.
As you can see, our code takes into account some practical issues. First of all we check our input parameters signal should not be NULL, signal length should be positive, alpha parameter should be even nonnegative number less than window size:
Second step we check case N=1. This case is special one, because to build extension we need at least two elements. For the signal of 1 element length the result is the signal itself. As well pay attention, our alpha-trimmed mean filter works in-place, if output parameter
Now let us allocate memory for signal extension.
And check memory allocation.
Now we are building extension.
Finally, everything is ready filtering!
And to complete the job — free memory.
Since we are using memory management function from standard library, we should include its header.
5. 2D alpha-trimmed mean filter programming
In 2D case we have 2D signal, or image. The idea is the same, just now alpha-trimmed mean filter has 2D window. Window influences only the elements selection. All the rest is the same: ordering elements, trimming the got set and calculating average of the remaining elements. So, let us have a look at 2D alpha-trimmed mean filter programming. For 2D case we choose window of 3×3 size.
6. Treating edges in 2D case
As for 1D case in 2D case we should extend our input image as well. To do that we are to add lines at the top and at the bottom of the image and add columns to the left and to the right.Fig. 3. Image extension.
Here is our wrapper function, that does that job.
7. Alpha-trimmed mean filter library
Now we have four functions, two of them are for processing 1D signals by alpha-trimmed mean filter, and other two are for filtering 2D images. It is time to put everything together and create small alpha-trimmed mean filter library. Let us write code for header file.
Our library is ready. The code we have written is good both for Linux/Unix and Windows platforms. You can download full alpha-trimmed mean filter library source code here:
Full listings of library files are available online as well:
And now a couple of applications to play around!
8. Alpha-trimmed mean filter: image restoration
Download alpha-trimmed mean filter for Win32 (zip, 603 Kb)
We have created a couple of applications to show alpha-trimmed mean filter capabilities in restoration images corrupted by impulse noise. The demo package includes 4 files — two applications, sample image and description:
Be aware of the fact, that this sample uses OpenGL, so it should be supported by your system (usually that is the case).
9. Step 1: prepare corrupted image
We have created impulse noise generator that will help us to prepare corrupted image. Start up corrupter.exe application and load image to be corrupted. Choose Set >> Corruption... or click N button in toolbar and set noise level at 5–15%. Click OK. Then save corrupted image.Fig. 4. Corruption by impulse noise.
10. Step 2: restore corrupted image
Start up alphatrimmedmean.exe application. Load the saved corrupted image. Set alpha parameter to 6: Set >> Alpha... or click α button in toolbar and select 6 in dialog's combo-box, click OK.Fig. 5. Alpha parameter selection.
Apply alpha-trimmed mean filter by choosing Set >> Filter or clicking F-button in toolbar. See the result. If necessary, filter the image once more.Fig. 6. Image restored by alpha-trimmed mean filter.