The median filter is normally used to reduce noise in an image, somewhat like the mean filter. However, it often does a better job than the mean filter of preserving useful detail in the image.
Mean filter is particularly effective at removing ‘salt and pepper’ type noise. The median filter works by moving through the image pixel by pixel, replacing each value with the median value of neighbouring pixels.
The pattern of neighbours is called the “window”, which slides, pixel by pixel over the entire image. The median is calculated by first sorting all the pixel values from the window into numerical order, and then replacing the pixel being considered with the middle (median) pixel value. In below matrix images RED one is the initial data and then this data is sorted and Median is calculated in the GREEN one and the median is 124. So 124 is replaced in the actual image pixel at that point, for which the operation is to be specified.
a = imread('image1.jpg'); % reading image aab = rgb2gray(a); % converting to gray b = [1 1 1; 1 1 1; 1 1 1;]/9; % Mean filter convolution c = imfilter(aab,b); % mean filtering d = medfilt2(aab); % median filtering subplot(2,2,1); imshow(aab); title('Original Image'); subplot(2,2,2); imshow(c); title('Mean filter'); subplot(2,2,3); imshow(d); title('Median Filtered Image');