


The Helpful Mathematics  +38 044 572 93 47 

Article 7Alphatrimmed mean filterCategory. Digital signal and image processing (DSP and DIP) software development. Abstract. The article is a practical tutorial for alphatrimmed mean filter understanding and implementation. Article contains theory, C++ source code, programming instructions and sample applications. Download alphatrimmed mean filter for Win32 (zip, 603 Kb) Download alphatrimmed mean filter C++ source code (zip, 2 Kb)
1. Introduction to alphatrimmed mean filterAlphatrimmed 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 alphatrimmed mean filterNow let us see, how to get alphatrimmed 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 alphatrimmed mean for the case, depicted in fig. 1. Fig. 1. Alphatrimmed mean calculation.Thus, to get an alphatrimmed 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 alphatrimmed mean calculated and signal, 1D in our case, filtered by alphatrimmed mean filter! Let us make resume and write down stepbystep instructions for processing by alphatrimmed mean filter. Alphatrimmed 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 alphatrimmed 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 alphatrimmed mean filter programmingIn this section we develop 1D alphatrimmed 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:
Now:
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:
Type
4. Treating edgesFor 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 alphatrimmed mean filter there is good idea to extend signal symmetrically, like this: Fig. 2. Signal extension.So, before passing signal to our alphatrimmed 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 alphatrimmed mean filter works inplace, 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 alphatrimmed mean filter programmingIn 2D case we have 2D signal, or image. The idea is the same, just now alphatrimmed 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 alphatrimmed mean filter programming. For 2D case we choose window of 3×3 size.
6. Treating edges in 2D caseAs 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. Alphatrimmed mean filter libraryNow we have four functions, two of them are for processing 1D signals by alphatrimmed mean filter, and other two are for filtering 2D images. It is time to put everything together and create small alphatrimmed 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 alphatrimmed mean filter library source code here: Download alphatrimmed mean filter C++ source code (zip, 2 Kb) Full listings of library files are available online as well:
And now — a couple of applications to play around! 8. Alphatrimmed mean filter: image restorationDownload alphatrimmed mean filter for Win32 (zip, 603 Kb) We have created a couple of applications to show alphatrimmed 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 imageWe 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 imageStart 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 combobox, click OK. Fig. 5. Alpha parameter selection.Apply alphatrimmed mean filter by choosing Set >> Filter or clicking Fbutton in toolbar. See the result. If necessary, filter the image once more. Fig. 6. Image restored by alphatrimmed mean filter.
Write to the author of the article — Sergey Chernenko. 


