


The Art of Interface 
Article 8Hybrid median filterCategory. Digital image processing (DIP) software development. Abstract. The article is a practical tutorial for hybrid median filter understanding and implementation. Article contains theory, C++ source code, programming instructions and a sample applications. Limited offerProfessional Librow Calculatorvisitfor free
Download
7.4 MB for Windows
1. Introduction to hybrid median filterHybrid median filter is windowed filter of nonlinear class, that easily removes impulse noise while preserving edges. In comparison with basic version of the median filter hybrid one has better corner preserving characteristics. The basic idea behind filter is for any element of the signal (image) apply median technique several times varying window shape and then take the median of the got median values. To get acquainted with filter window idea in signal and image processing read our “Filter window, or filter mask” article. 2. Understanding hybrid median filterNow let us see, how hybrid version of the median filter works. In one phrase idea is like “apply median filter with crossmask, apply median filter with xmask and take the median of got results and element itself”. Hybrid median filter workflow depicted below in fig. 1. Fig. 1. Hybrid median filter workflow.Due to the nature of the workflow the filter dimension is not less than 2D. Now let us write down stepbystep instructions for processing by hybrid median filter. Hybrid median filter algorithm:
Now, when we have the algorithm, it is time to write some code — let us come down to programming. 3. Hybrid median filter programmingWe can see that operation of taking median — “order elements” and “take the middle element” — repeated three times in the list. So it is good idea to put those steps into separate function:
In the function above we are using a code optimization trick. Since everything we need is middle element, it is enough to order just a half of elements. Let us have 2D signal or image of length N×M as input and let we choose filter window of 3×3 size. The first step is window placing — we do that by changing index of the leading element:
Pay attention, that we are not processing first and last rows and columns. The problem is we cannot process elements at edges, because in this case the part of the filter window is empty. We will discuss below, how to solve that problem. The second step is picking up elements around with crosswindow, ok:
The third and fourth steps are done by our auxiliary function:
Fifth and sixth steps are picking up elements with xwindow:
Seventh and eighth steps are made again by our auxiliary function:
To complete ninth step enough to pick up our leading element itself:
Final steps are performed by our useful function:
At last, let us write down the entire algorithm as function:
Looks very straightforward. Type
4. Treating edgesFor all window filters there is some problem. That is edge treating. If you place window over an element at the edge, some part of the window will be empty. To fill the gap, signal should be extended. For hybrid median filter there is good idea to extend image symmetrically, like this: Fig. 2. Image extension.In other words we are adding lines at the top and at the bottom of the image and add columns to the left and to the right of it. So, before passing signal to our median 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 — image should not be NULL, and image sizes should be positive:
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. Hybrid median filter declaration fileNow we have three functions: helper function, hybrid median filter and entry point that makes preparations. Let us write code for header file.
Now all is ready. The code we have written is good both for Linux/Unix and Windows platforms. You can download full hybrid median filter source code here:
Full file listings are available online as well:
And now — a couple of applications to play around! 6. Color median filter: image restoration
We have created a couple of applications to show hybrid median filter capabilities in restoration images corrupted by impulse noise. The sample 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). 7. 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 Nbutton in toolbar and set noise level at 5–15%. Click OK. Then save corrupted image. Fig. 3. Corruption by impulse noise.8. Step 2: restore corrupted imageStart up hybridmedian.exe application. Load the saved corrupted image. Apply hybrid median filter by choosing Set >> Filter or clicking Fbutton in toolbar. See the result. If necessary, filter the image one more time. Fig. 4. Image restored by hybrid median filter.


