Low pass filtering
Given that pixels corrupted by salt and pepper noise are high frequency components of an image, we should expect a low-pass filter should reduce them. So we might try filtering with an average filter:
>> a3=fspecial(’average’); >> t_sp_a3=filter2(a3,t_sp);
and the result is shown in figure 7.4(a). Notice, however, that the noise is not so much removed as “smeared” over the image; the result is not noticeably “better” than the noisy image. The effect is even more pronounced if we use a larger averaging filter:
>> a7=fspecial(’average’,[7,7]); >> t_sp_a7=filter2(a7,t_sp); and the result is shown in figure 7.4(b).
Median filtering
Median filtering seems almost tailor-made for removal of salt and pepper noise. Recall that the median of a set is the middle value when they are sorted. If there are an even number of values, the median is the mean of the middle two. A median filter is an example of a non-linear spatial filter; using a mask, the output value is the median of the values in the mask. For example:
60
We see that very large or very small values—noisy values—will end up at the top or bottom of the sorted list. Thus the median will in general replace a noisy value with one closer to its surroundings.
7.3. CLEANING SALT AND PEPPER NOISE 99
(a) averaging (b) averaging
Figure 7.4: Attempting to clean salt & pepper noise with average filtering
In Matlab, median filtering is implemented by the medfilt2 function: >> t_sp_m3=medfilt2(t_sp);
and the result is shown in figure 7.5. The result is a vast improvement on using averaging filters. As
Figure 7.5: Cleaning salt and pepper noise with a median filter
with most functions, medfilt2 takes an optional parameter; in this case a 2 element vector giving the size of the mask to be used.
If we corrupt more pixels with noise:
>> t_sp2=imnoise(t,’salt & pepper’,0.2);
100 CHAPTER 7. NOISE
(a) 20% salt & pepper noise (b) After median fitering
Figure 7.6: Using a
median filter on more noise
we can either try a second application of the median filter, the result of which is shown in figure 7.7(a) or try a median filter on the original noisy image:
>> t_sp2_m5=medfilt2(t_sp2,[5,5]); the result of which is shown in figure 7.7(b).
(a) Using medfilt2 twice (b) using a median filter
Figure 7.7: Cleaning 20% salt & pepper noise with median filtering
Rank-order filtering
Median filtering is a special case of a more general process called rank-order filtering. Rather than take the median of a set, we order the set and take the -th value, for some predetermined value of .
7.3. CLEANING SALT AND PEPPER NOISE 101 Thus median filtering using a mask is equivalent to rank-order filtering with . Similarly, median filtering using a mask is equivalent to rank-order filtering with . Matlab implements rank-order filtering with the ordfilt2 function; in fact the procedure for medfilt2 is really just a wrapper for a procedure which calls ordfilt2. There is only one reason for using rank-order filtering instead of median filtering, and that is that it allows us to choose the median of non-rectangular masks. For example, if we decided to use as a mask a cross shape:
then the median would be the third of these values when sorted. The command to do this is >> ordfilt2(t_sp,3,[0 1 0;1 1 1;0 1 0]);
In general, the second argument of ordfilt2 gives the value of the ordered set to take, and the third element gives the domain; the non-zero values of which specify the mask. If we wish to use a cross with size and width 5 (so containing nine elements), we can use:
>> ordfilt2(t_sp,5,[0 0 1 0 0;0 0 1 0 0;1 1 1 1 1;0 0 1 0 0;0 0 1 0 0])
An outlier method
Applying the median filter can in general be a slow operation: each pixel requires the sorting of at least nine values1. To overcome this difficulty, Pratt [16] has proposed the use of cleaning salt and
pepper noise by treating noisy pixels as outliers; that is, pixels whose grey values are significantly different from those of their neighbours. This leads to the following approach for noise cleaning:
1. Choose a threshold value .
2. For a given pixel, compare its value with the mean of the values of its eight neighbours. 3. If
, then classify the pixel as noisy, otherwise not.
4. If the pixel is noisy, replace its value with ; otherwise leave its value unchanged.
There is no Matlab function for doing this, but it is very easy to write one. First, we can calculate the average of a pixel’s eight neighbours by convolving with the linear filter
We can then produce a matrix consisting of 1’s at only those places where the difference of the original and the filter are greater than ; that is, where pixels are noisy. Then
will consist of ones at only those places where pixels are not noisy. Multiplying by the filter replaces noisy values with averages; multiplying
with original values gives the rest of the output.
1In fact, this is not the case with Matlab, which uses a highly optimized method. Nonetheless, we introduce a
102 CHAPTER 7. NOISE A Matlab function for implementing this is shown in figure 7.8. An immediate problem with the outlier method is that is it not completely automatic—the threshold must be chosen. An appropriate way to use the outlier method is to apply it with several different thresholds, and choose the value which provides the best results. Suppose we attempt to use the outlier method to clean
function res=outlier(im,d)
% OUTLIER(IMAGE,D) removes salt and pepper noise using an outlier method. % This is done by using the following algorithm:
%
% For each pixel in the image, if the difference between its grey value % and the average of its eight neighbours is greater than D, it is % classified as noisy, and its grey value is changed to that of the % average of its neighbours.
%
% IMAGE can be of type UINT8 or DOUBLE; the output is of type
% UINT8. The threshold value D must be chosen to be between 0 and 1. f=[0.125 0.125 0.125; 0.125 0 0.125; 0.125 0.125 0.125];
imd=im2double(im); imf=filter2(f,imd); r=abs(imd-imf)-d>0;
res=im2uint8(r.*imf+(1-r).*imd);
Figure 7.8: A Matlab function for cleaning salt and pepper noise using an outlier method
the noise from figure 7.1(b); that is, the twins image with 10% salt and pepper noise. Choosing
gives the image in figure 7.9(a). This is not as good a result as using a median filter: the affect of the noise has been lessened, but there are still noise “artifacts” over the image. In this case we have chosen a threshold which is too small. If we choose
, we obtain the image in figure 7.9(b), which still has smoe noise artifacts, although in different places. We can see that a lower values of tends to remove noise from dark areas, and a higher value of tends to remove noise from light areas. A mid-way valaue, round about
does in fact produce an acceptable result, although not quite as good as median filtering.
Clearly using an appropriate value of is essential for cleaning salt and pepper noise by this method. If is too small, then too many “non-noisy” pixels will be classified as noisy, and their values changed to the average of their neighbours. This will result in a blurring effect, similar to that obtained by using an averaging filter. If is chosen to be too large, then not enough noisy pixels will be classified as noisy, and there will be little change in the output.
The outlier method is not particularly suitable for cleaning large amounts of noise; for such situations the median filter is to be preferred. The outlier method may thus be considered as a “quick and dirty” method for cleaning salt and pepper noise when the median filter proves too slow.