Digital Image Processing
& Pattern Recognition
Lecture#3
Image Smoothing & Noise Removal
Prepared by: Dr. Gawed Nagi
Contents
This lecture will cover:
◼ Histogram Matching (Specification)
◼ Image Smoothing Filters
Mean Filter
Median Filter
Gaussian Filter
◼ Point Processing
Image negatives
Log Transformation
Power-Law (Gamma) Transformation
Piecewise Linear Transformation Functions
Histogram Matching (Specification)
Assume we have two images and each has
its specific histogram. So
Is it possible to modify one image based on
the contrast of another one? YES
In fact, this is the definition of the
histogram matching.
In other words, given images A, and B, it
is possible to modify the contrast level of
A according to B.
Histogram Matching (Specification)
In order to match the histogram of images
A and B, first equalizing the histogram of
both images. Then,
Mapping each pixel of A to B using the
equalized histograms. Then
Modifing each pixel of A based on B.
Histogram Matching (Specification)
Original Image Target Image Matched Image
Histogram Matching (Specification)
MATLAB Hints
% Example of Histogram Matching
A = imread("in.jpg");
Ref = imread("ref.jpg");
B = imhistmatch(A,Ref);
montage({A,Ref,B})
title('Input Image (Left) vs Reference Image (Right) &
Output Image (Down)’);
%{
subplot(2, 2, 1), imshow(A), title ('Input Image');
subplot(2, 2, 2), imshow(Ref), title ('Reference Image');
subplot(2, 2, 3), imshow(B), title ('Output Image');
%}
Image Enhancement Domains
Image Enhancement Domains
Neighborhood
Histogram Point
Processing
Processing Processing
(Filtering)
Image Smoothing & Enhancing
Image smoothing, Noise removing, edge
enhancement and boundary detection are
local operations in image processing.
A Local operation is an operation that
transfer input image into an output image
depending on the neighborhood values of
input coordinates.
It means, if pixel b[x,y] is a pixel in
output image, it will be result based on
the a[x,y], the same pixel in input image
and the neighborhood pixel values.
Neighbors of A Pixel
❖some of the points in ND (p) and N8 (p) fall outside the
image if (x,y) is on the border of the image.
Smoothing Spatial Filtering
Spatial
Filtering
Non-Linear
Linear Filtering
Filtering
Mean/Averaging
Median Filter
Filter
Max Filter
Gaussian Filter Min Filter
Linear Filtering
Convolution
convolution is simply the process of taking
a small matrix called the kernel and
running it over all the pixels in an image.
At every pixel, we’ll perform some math
operations involving the values in the
convolution matrix and the values of a
pixel and its surroundings to determine
the value for a pixel in the output image.
Mean / Averaging Filtering
(Neighborhood Averaging)
When an image is acquired by a camera or
other imaging system, normally the vision
system for which it is intended is unable to
use it directly.
The image may be corrupted by random
variations in intensity, variations in
illumination, poor contrast or noise that
must be handle with in the early stages of
vision processing.
Mean / Averaging Filtering
Therefore, mean filter is one of the
techniques which is used to reduce noise of
the images.
This is a local averaging operation and it is a
one of the simplest linear filter.
The value of each pixel is replaced by the
average of all the values in the local
neighborhood. Let f(i,j) is a noisy image
then the smoothed image g(x,y) can be
obtained by,
Where S is a neighborhood of (x,y) and n is the number of pixels in S.
Mean / Averaging Filtering
3x3 mean filtering,
1/9
Mean / Averaging Filtering
Mean / Averaging Filtering
Kernel 5x5
MATLAB Hints
% mean filter with 3X3 mask/window size
I = imread("Lena2.png");
N = imnoise(I,'salt & pepper',0.03);
K = ones(3,3)/9;
noise_free = imfilter(N,K);
subplot(2,2,1), imshow(I), title ('Original Image');
subplot(2,2,2), imshow(N), title ('Noisy Image');
subplot(2,2,3), imshow(noise_free), title ('After
Noisy Removal');
Median Filter
Median, in statistics, is the middle value of
the given list of data when arranged in an
order.
X = ordered list of values in data set
N = number of values in data set
Median Filter
Median Filter
MATLAB Hints
I = imread ("Lena2.png");
N = imnoise(I,'salt & pepper', 0.3);
red_channel = N (:, :, 1);
green_channel = N (:, :, 2);
blue_channel = N (:, :, 3);
red_channel = medfilt2(red_channel , [3 3]);
green_channel = medfilt2(green_channel , [3 3]);
blue_channel = medfilt2(blue_channel , [3 3]);
F = cat (3, red_channel , green_channel ,
blue_channel );
subplot(2, 1, 1), imshow(N), title ('Noisy Image');
subplot(2, 1, 2), imshow(F), title ('Image After Noise
Removal');
Gaussian Filter
instead of a box filter consisting of equal
filter coefficients, a Gaussian kernel is used.
Gaussian filtering is highly effective in
removing Gaussian noise from the image.
Gaussian filtering is used to blur images and
remove noise and detail.
Gaussian Filter
Gaussian function in two dimensions:
σ – the standard deviation
Gaussian Filter
Source Pixels
New Pixels
Gaussian Filter
5x5 Gaussian kernel
Finding out the values of a Gaussian Kernel
If the kernel size is 5x5 and the value of σ =1, We
have to find out all the values for this kernel. The
equation of Gaussian function is:
For a filter size x by y, the number of rows are x and
the number of columns are y.
Generally, The filter size consists of odd numbers, In
this case ,the values for rows are
-{(x-1)/2} to +{(x-1)/2}
and the values of columns are
-{(y-1)/2} to +{(y-1)/2} .
Finding out the values of a Gaussian Kernel
So, for filter size 5x5 , the row values are -2 to +2
and column values are -2 to +2. So, if we want to
calculate the 1st value of the kernel , we have to put
the row value and column value of the kernel for
that position in the Gaussian Function.
.
Finding out the values of a Gaussian Kernel
we can see that all the values are very small, if
we normalize all the values by dividing all the
values by 0.003 , we will get the following table:
MATLAB Hints
I = imread ("Lena2.png");
N = imnoise(I,'salt & pepper', 0.05);
red_channel = N (:, :, 1);
green_channel = N (:, :, 2);
blue_channel = N (:, :, 3);
g_Filter = fspecial (‘gaussian’, [5 5], 4);
red_channel = imfilter(red_channel , g_Filter );
green_channel = imfilter(green_channel , g_Filter );
blue_channel = imfilter(blue_channel , g_Filter );
MATLAB Hints
F = cat (3, red_channel , green_channel , blue_channel );
subplot(2, 1, 1), imshow(N), title ('Noisy Image');
subplot(2, 1, 2), imshow(F), title ('Image After Noise
Removal by Gaussian Filter');
Point Processing
Neighborhood
Histogram Point
Processing
Processing Processing
(Filtering)
Point Processing
Basic Gray Level Transformations:
image enhancement can be done by gray-
level transformation functions.
These are among the simplest of all image
enhancement techniques.
The values of pixels, before and after
processing, will be denoted by r and s,
respectively. s=T(r) Or
Where r is the pixels of the input image and s is the pixels of the
output image. T is a transformation function that maps each
value of r to each value of s.
Intensity Transformation Functions
Image Negatives /The Negative
Transformation
The negative of an image with gray levels
in the range [0, L-1] is obtained by using
the negative transformation
s = L - 1 - r.
The negative of an image with gray levels
in the range [0, L-1] is obtained by using
the negative transformation
Image Negatives /The Negative
Transformation
Image Negatives /The Negative
Transformation
The Negative Transformation
The Negative Transformation
MATLAB Hints
% reading the RGB file into the Matlab environment
I = imread("lena2.png");
subplot(1, 2, 1),
% displaying the RGB image
imshow(I);
title("Original image");
% levels of the 8-bit image
L = 2 ^ 8;
% finding the negative
neg = (L - 1) - I;
subplot(1, 2, 2),
% displaying the negative image
imshow(neg);
title("Negative Image")
Log Transformation
Log transformation to visualize patterns in
the dark regions of an image.
When the input grey level values may
have an extremely large rang of values.
E.g. Fourier Transform can have values in
the rang [0-106]
Log Transformation
Log transform inverse
Log Transformation
In the log transformation, the low-intensity
values are mapped into higher intensity
values.
It maps a narrow range of low gray levels to
a much wider range.
The inverse log transform is opposite to log
transform. It maps a narrow range of high
gray levels to a much wider range.
The inverse log transform expands the
values of light-level pixels while
compressing the darker-level value
Log Transformation
Log Transformation
MATLAB Hints
% MATLAB program to demonstrate log transformation of image
input_image =imread(‘tst.jpg’);
% Convert the image to double datatype for calculations
input_image = im2double(input_image);
% Constant to determine the nature of the log curve
c = 1;
% Perform the log transformation
log_transformed = c * log(1 + input_image);
% Display the original image and log-transformed image
subplot(1, 2, 1), imshow(input_image), title('Original Image’);
subplot(1, 2, 2), imshow(log_transformed), title('Log-
Transformed Image’);
% Save the log-transformed image
imwrite(log_transformed,'log_transformed_image.jpg');
Power-Law (Gamma) Transformations
Power-law transformations have the form
Variation in the value of γ varies the enhancement
of the images. Different display devices / monitors
have their own gamma correction, that’s why they
display their image at different intensity.
Power-Law (Gamma) Transformations
Power-Law (Gamma) Transformations
Power-Law (Gamma) Transformations
Power-Law (Gamma) Transformations
Power-Law (Gamma) Transformations
Power-Law (Gamma) Transformations
MATLAB Hints
% MATLAB program to demonstrate log transformation of image
input_image =imread(‘tst.jpg’);
% Convert the image to double datatype for calculations
input_image = im2double(input_image);
% Set a desired gamma value for the power law transformation
gamma = 0.4;
% Perform the power law transformation of image
power_law_image = input_image .^ gamma;
% Display the original and power law transformed images
subplot(1, 2, 1), imshow(input_image), title('Original Image’);
subplot(1, 2, 2), imshow(power_law_image), title('Power Law
Transformed Image’);
% Save the power law transformed image
imwrite(power_law_image,'power_law_transformed_image.jpg');
Piecewise Linear Transformation
Functions
Q&A