In this instruction, the median filtering which is a widely used image filtering algorithm for de-noising will be described step by step.
For color image, RGB Values of the pixels should be extracted. Matlab can be used for this operation.
Example Code:
impixel(I)
P = impixel(I,c,r)
P = impixel(X,map)
P = impixel(X,map,c,r)
[c,r,P] = impixel(___)
P = impixel(x,y,I,xi,yi)
[xi,yi,P] = impixel(x,y,I,xi,yi)
(Source: Mathworks.com)
Figure 1. RGB Code examples
If the image is a gray scaled image, then the gray scale values of the pixels which are between 0 and 255 should be obtained. It can be also executed in Matlab.
Example Code:
I = imread('test.jpg');
GrayscaleImage = [];
I = double(I);
for i = 1:size(I, 1)
for j = 1:size(I, 2)
GrayscaleImage(i, j) = (I(i, j, 1) + I(i, j, 2) + I(i, j, 3)) / 3;
end
end
GrayscaleImage = uint8(GrayscaleImage);
(Source: Mathworks.com)
Figure 2. Gray-Scale Code Examples
The mask size should be chosen according to the image type and the noise level of the image. For the images which include close pixels which have significantly different values, It might be better using small kernel (mask size) because It can be prevent blurring.
Figure 3. Mask Size Examples
The pixel which the median filter is applied should be placed in the center of the mask.
Figure 4. Median Calculation
The median value of the mask components is calculated.
The centered pixel is replaced by the median value.
Figure 5. Animated Step4 and Step5