Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/334746022

Implementation of Weighted Median Filters in Images

Technical Report · July 2019


DOI: 10.13140/RG.2.2.18606.66887

CITATION READS
1 5,691

1 author:

Juan Aguirre Escudero


Pennsylvania State University
3 PUBLICATIONS 1 CITATION

SEE PROFILE

All content following this page was uploaded by Juan Aguirre Escudero on 29 July 2019.

The user has requested enhancement of the downloaded file.


The Pennsylvania State University
School of Science Engineering & Technology
Electrical Engineering

Implementation of Weighted Median Filters in Images

Prepared By: Juan Enrique Aguirre


Prepared For: Dr. Morales Aldo

Topics in Digital Signal Processing EE 553


July 7, 2019
ABSTRACT
In this paper we described the non-linear weight median filters on images. We demonstrated
the applications by reducing the noise while keeping a good resulting image implemented in
our MATLAB script where we let the user input the weights of the matrix.

INTRODUCTION
Choosing a corresponding filter on image processing could be prove valuable if we understand
what we want to see as the end result. Such is the case for our nonlinear filter class “weight
median filter” in which has two intrinsic properties: edge preservation and efficient noise
attenuation with robustness against impulsive-type noise [1]. The fact that preserves the edges
of the image helps the visual perception of the image structure while reducing the noise, but at
the same time it also creates small spots of flat intensity, that affect sharpness [2]. For the
following application we have to consider that a color image contains three planes “rgb” red,
green, and blue. Hence, we can implement the median filtering with a particular filter
window/mask “pixel-matrix size” on each of the planes and combine them together for a result
of a restored image.

THEORY
The weighted median (WM) filter was first presented as an overview of the standard median
filter, where a nonnegative integer weight is assigned to each position in the filter window [1].
Color assigned by a median filter determined by colors of the majority of neighboring pixels
within the filter region and because of this approach this filter is considered robust since single
high or low values “outliers” on the pixel intensity cannot influence our result unlike linear
average. To compute result, each pixel value within filter region is inserted W(i,j) times to create
extended pixel vector as seen on Fig.1.

Figure 1: Weight Median Filter Pixel Representation.


IMPLEMENTATION
After we cleared and close all previous work, we proceed to read a color image into our MATLAB
workspace. For convenience, I used an existing image from Toolbox Image demos called
peppers.png, besides that if image is not found it will alert the user with an Error “x does not
exist” as seen on Fig.2 -3. I changed the name to “pepperss” for the error. We also should notice
that the number of color bands should be equal to 3 for this to work since this application is for
an RGB color planes “color image”. If we were to put in our baseFilename to be cameraman.tif
it would not work since this is a black and white photo.

Figure 2: MATLAB Code for Reading Color Image.

Figure 3: Alert Dialog when Image is Non-existing.

On the second part of our code Fig.4, as requested in our assignment, we request the user to
input the weights by letting them define on the Command window nxm values where each output
pixel contains the median value in the m-by-n neighborhood around the corresponding pixel in
the input image. We also display the color image, while extracting each individual color plane.
Then we introduce some salt & pepper “impulse” noise with a noise density of 0.05. We have to
keep in mind individual noise at each color plane[3]. For our median filter we use the MATLAB
built function medfilt2(channel, [m n]) [4].

Figure 4: MATLAB Code & Command Window for Part-b.

Fig.5 covers part-c of our code in which we find the noise at each color plane and proceed to
apply the weight median filter and bring all 3 color planes by concatenating them using the “cat”
function. In our first example we used a 3x3 nxm weight matrix.

Figure 5: MATLAB Code for Part-c.


RESULTS
On Fig.6 I combined images in a subplot of a 3x3 center arrangement, where our first display is
the Original Color Image “peppers.png” followed by the Image with Salt and Pepper Noise with a
noise density of 0.05, then we finalized by presenting the Restored Color Image in where we
concatenate the weighted median filtered images from each color plane using a 3X3 “mxn”
neighborhood around the corresponding pixel in the input image.

Figure 6: Color Weight Median Filter Application Images “3X3”

Then I proceed to change the window of the matrix size of neighbouring pixels from 3x3 to
60x60 a bigger array “way out of a normal application scope” so we can observe some noise on
our restored Image as seen on Fig. 7. Here we start seeing that the weighted median is getting
more of the purple background in our peppers due to the bigger array.
Figure 7: Color Weight Median Filter Application Images “60X60”

CONCLUSIONS
Implementing the corresponding filter for our image processing can be interesting and useful
since we get to see the results of it. The weight median filter for our color RGB planes maintain
a good image structure since this particular filter focus on edge preservation and efficient noise
attenuation with robustness against impulsive-type noise such as the salt and pepper noise.
This filter in particular assigns weights number of “votes” to filter positions of the neighboring
pixels within the pixel region hence the importance to keep a relevant smaller array such as a
3x3 or 5x5. Image postprocessing is definitely a big part of the DSP community due to the
numerous applications we see and use around us, such as the application presented on this
paper.

REFERENCES
[1] Morales, Aldo. “Lesson 8 Assignment: Implementation of Weighted Median Filters in
Images.” www.canvas.psu, July 2019.

[2] Agu, Emmanuel. “Digital Image Processing (CS/ECE 545) Lecture 4: Filters (Part 2) & Edges
and Contours.” Https://Web.cs.wpi.edu, Worcester Polytechnic Institute,
web.cs.wpi.edu/~emmanuel/courses/cs545/S14/slides/lecture04.pdf.

[3] Lim, Jae S., Two-Dimensional Signal and Image Processing, Englewood Cliffs, NJ, Prentice Hall,
1990, pp. 469-476.

[4] “medfilt2.” 2-D Median Filtering - MATLAB, www.mathworks.com/help/images/ref/medfilt2.html.

[5] Cochin, Elysi. “Adaptive Weighted Median Filter.” Adaptive Weighted Median Filter - MATLAB Answers -
MATLAB Central, MathWorks, 14 Jan. 2013, www.mathworks.com/matlabcentral/answers/58712-
adaptive-weighted-median-filter.

Appendix -A
MATLAB CODE (below in gray my coding and modifications) rest taken from web [5]
%EE553 _HW 8
%Juan Aguirre
%The following program Implements Weighted Median Filters
%for color images. While Letting the user pick the weights.
clc;
clear;
close all;
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 10;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';

% Get the full filename, with path


fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);

%Define the window of pixel regions to be used by the Median Filter


disp('Please choose neighboring pixel matrix nxm for the Median filter')
prompt = 'What is the m value? ';
m = input(prompt)
prompt= 'What is the n value?';
n= input (prompt)

% Display the original color image.


subplot(3, 1, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);

% Generate a noisy image. This has salt and pepper noise independently on
% each color plane with a 0.05 intensity so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', 0.05);
subplot(3, 3, 5);
imshow(noisyRGB);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, 1);
greenChannel = noisyRGB(:, :, 2);
blueChannel = noisyRGB(:, :, 3);

% Median Filter the channels


redMF = medfilt2(redChannel, [m n]);
greenMF = medfilt2(greenChannel, [m n]);
blueMF = medfilt2(blueChannel, [m n]);
% Find the noise in the red.
noiseImage = (redChannel == 0 | redChannel == 255);
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == 0 | greenChannel == 255);
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);

% Find the noise in the blue.


noiseImage = (blueChannel == 0 | blueChannel == 255);
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);

% Reconstruct the noise free RGB image


rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
subplot(3, 3, 8);
imshow(rgbFixed);
title('Restored Color Image', 'FontSize', fontSize);

View publication stats

You might also like