Lab File
Lab File
(MNNIT)
Allahabad INDIA
Lab report on
ADVANCED DIGITAL SIGNAL & IMAGE PROCESSING
(EC-18201)
Using MATLAB
(Session-2023-24)
Aim: Write a program to read a RGB image and perform the following operations on the image.
(c) Extract red, green, and blue components of the image and then combine them to get the
original image.
(d) Convert the RGB image into grayscale image using equation.
Y=(77/256)R + (150/256)G + (29/256)B
Theory:
An image is an array, or a matrix, of square pixels (picture elements) arranged in columns and
rows. RGB Image In RGB or color, images, the information for each pixel requires a tuple of
numbers to represent. So we need a three-dimensional matrix to represent an image. Almost all
colors in nature can be composed of three colors: red (R), green (G), and blue (B). So each
pixel can be represented by a red/green/blue tuple in an RGB image.
Grayscale Image The grayscale image adds a color depth between black and white in the binary
image to form a grayscale image. Such images are usually displayed as grayscales from the
darkest black to the brightest white, and each color depth is called a grayscale, usually denoted
by L. In grayscale images, pixels can take integer values between 0 and L-1.
MATLAB Code:
clc;
close all;
clear;
img=imread('image.jpeg');
R=img(:,:,1);
G=img(:,:,2);
B=img(:,:,3);
a = zeros(size(img, 1), size(img, 2));
x1=cat(3,R,G,B);
subplot(2,3,4);
imshow(x1);
title('RGB Image');
subplot(2,3,5);
imshow(gray_img);
title('Grayscale Image');
Output:
Fig 1.1: After extracting red, green and blue components of an image, Combined original
Image, Grayscale Image
Result:
Matlab code is written to read an RGB image and perform the operations of extraction of red,
green, blue components of the image. Further, RGB image is converted to grayscale using
given equation.
Experiment-2
Aim: Write a program to perform ‘Zoom In’ and ‘Zoom Out’ operation on an image by 200%
and 50% using (a) nearest neighbor (b) bi-linear interpolation (c) bi-cubic interpolation.
Theory:
Zooming means enlarging a picture in a sense that the details in the image became more visible
and clear. Zooming an image has many wide applications ranging from zooming through a
camera lens, to zoom an image on internet e.t.c.
You can zoom something at two different steps. The first step includes zooming before taking
an particular image. This is known as pre processing zoom. This zoom involves hardware and
mechanical movement. The second step is to zoom once an image has been captured. It is done
through many different algorithms in which we manipulate pixels to zoom in the required
portion.
MATLAB Code:
% Zoom in by 200%
zoomed_in_nearest = imresize(image, 2, 'nearest');
zoomed_in_bilinear = imresize(image, 2, 'bilinear');
zoomed_in_bicubic = imresize(image, 2, 'bicubic');
% Display results
figure;
subplot(4, 2, 1), imshow(image), title('Original Image');
subplot(4, 2, 2), imshow(zoomed_in_nearest), title('Zoomed In (Nearest
Neighbor)');
subplot(4, 2, 3), imshow(zoomed_in_bilinear), title('Zoomed In (Bilinear)');
subplot(4, 2, 4), imshow(zoomed_in_bicubic), title('Zoomed In (Bicubic)');
subplot(4, 2, 5), imshow(zoomed_out_nearest), title('Zoomed Out (Nearest
Neighbor)');
subplot(4, 2, 6), imshow(zoomed_out_bilinear), title('Zoomed Out (Bilinear)');
subplot(4, 2, 7), imshow(zoomed_out_bicubic), title('Zoomed Out (Bicubic)');
Output:
Fig 2.1 Original image, Zoomed in nearest, Zoomed in Bilinear, Zoomed in Bicubic, Zoomed out
Nearest, Zoomed out Bilinear, Zoomed out Bicubic
Result:
Zooming in and zooming out of image was successfully performed using Matlab.
Experiment-3
Aim: Write a program to construct (i) 5×5 Gaussian filter (ii) average filter (iii) median filter
and compare their results on an image corrupted by salt and pepper noise.
Theory:
Image noise is random variation of brightness or color information in the images captured. It
is degradation in image signal caused by external sources. Images containing multiplicative
noise have the characteristic that the brighter the area the noisier it. But mostly it is additive.
We can model a noisy image as A(x,y) = H(x,y) + B(x,y) Where, A(x,y)= function of noisy
image, H(x,y)= function of image noise , B(x,y)= function of original image.
MATLAB Code:
% Construct filters
gaussian_filter = imgaussfilt(noisy_image, 2);
average_filter = imfilter(noisy_image, ones(5)/25, 'symmetric');
median_filter = medfilt2(noisy_image, [5, 5]);
Fig 3.1 Original image, Gaussian added image, salt and pepper added image and recovered
images.
Result:
The 5*5 Gaussian, average and median filters were applied on an image corrupted by salt and
peppernoise and the required image was constructed.
Experiment-4
Aim: Write a program to detect the edges of a colored image using (a) Sobel operator (b)
Prewitt operator and verify the results using the inbuilt commands.
Theory:
Edges:
(1)Abrupt change in the intensity of pixels.
(2)Discontinuity in image brightness or contrast.
(3)Usually, edges occur on the boundary of two regions.
Edge Detection: Identifying points/edges in a digital image at which the image brightness
changes sharply or has a discontinuity. It is extensively used in image segmentation when we
want to divide the image into areas corresponding to different object.
One popular method for detecting edges is using the Sobel operator. The Sobel operator is a
gradient-based edge detection algorithm that uses two 3x3 convolution kernels, one for
detecting edges in the horizontal direction and one for detecting edges in the vertical direction.
The Prewitt operator is a popular method for detecting edges in images. It works by convolving
an image with a small kernel, typically a 3x3 matrix, and computing the gradient of the image.
The magnitude of the gradient indicates the strength of the edge, while the direction indicates
the orientation of the edge
MATLAB Code:
subplot(3,2,4);
imshow(edge(input_image_gray, 'sobel'));
title('Filtered Image (Inbuilt Sobel)');
subplot(3,2,5);
imshow(filtered_image_prewitt);
title('Filtered Image - Prewitt');
subplot(3,2,6);
imshow(edge(input_image_gray, 'prewitt'));
title('Filtered Image (Inbuilt Prewitt)');
Output:
Result:
Matlab program is successfully written to detect the edges of a colored image using Sobel
operators and results are verified using the in-built commands.
Experiment-5
Aim: Write a program to detect the edges of an image using second order derivatives i.e.
Laplacian operator.
Theory:
The Laplacian is a 2-D isotropic measure of the 2nd spatial derivative of an image. The Laplacian
of an image highlights regions of rapid intensity change and is therefore often used for edge
detection. The major difference between Laplacian and other operators like Prewitt, Sobel,
Robinson and Kirsch is that these all are first order derivative masks but Laplacian is a second
order derivative mask.
Positive Laplacian: Positive Laplacian we have standard mask in which center element of the
mask should be negative and corner elements of mask should be zero.
Negative Laplacian: In negative Laplacian operator we also have a standard mask, in which center
element should be positive. All the elements in the corner should be zero and rest of all the
elements in the mask should be -1.
MATLAB Code:
clc;
close all;
clear;
x = imread('image.jpeg');
subplot(2,2,1);
imshow(x);
title('Original Image');
x2 = im2double(x);
x1 = rgb2gray(x2);
[m,n]=size(x1);
%negative laplacian operator
Ln=[0 -1 0;-1 4 -1;0 -1 0];
%positive laplacian operator
Lp=[0 1 0;1 -4 1;0 1 0];
for i=1:m-2
for j=1:n-2
Ip(i,j)=sum(sum(Lp.*x1(i:i+2,j:j+2)));
In(i,j)=sum(sum(Ln.*x1(i:i+2,j:j+2)));
end
end
k = fspecial('laplacian',0);
y = imfilter(x1,k);
subplot(2,2,2);
imshow(y);
title('Using in-built function');
subplot(2,2,3);
imshow(Ip);
title('Positive laplacian operator');
subplot(2,2,4);
imshow(In);
title('Negative laplacian operator');
Output:
Result:
Matlab program is successfully written to detect the edges of a colored image using
Laplacian operator.
Experiment-6
Objective: Convert the RGB image into grayscale. Write a program to perform histogram
equalization using (a) CLAHE method (b) Brightness preserving Bi-Histogram equalization
(BPBiHE) method on a gray scale image and compute the PSNR and MSE of the output image.
Theory:
Histogram equalization (HE) produces a transformation function that is adaptive, in the sense that
it is based on the histogram of a given image. However, once the transformation function for an
image has been computed, it does not change unless the histogram of the image changes.
Histogram equalization achieves enhancement by spreading the levels of the input image over a
wider range of the intensity scale. HE is useful in some applications to be able to specify the
shape of the histogram that we wish the processed image to have. The method used to generate a
processed image that has a specified histogram is called histogram matching or histogram
specification.
MATLAB Code:
% Convert to uint8
output_image = uint8(output_image);
end
Output:
Result:
MATLAB program is written and implemented to perform histogram equalization using (a)
CLAHE method (b) Brightness preserving Bi-Histogram equalization (BPBiHE) method on a
gray scale image.
Experiment-7
Aim: Write a program to perform deblurring and denoising operation using Wiener filter on
an image corrupted by salt and pepper, speckle and Gaussian noise and compare results.
Theory:
Salt and Pepper noise: It is an impulse type of noise in nature. This noise can be caused by sharp
and sudden disturbances in the image signal. It presents itself as sparsely occurring white and
black pixels.
Gaussian noise: Gaussian noise arises in an image due to factors such as electronic circuit noise
and sensor noise due to poor illumination or high temperature.
Speckle noise: Speckle noise is a common type of noise, which is the main noise in medical
ultrasonic images. Speckle noise is usually considered as multiplicative noise. The noise
intensity in a certain region of image is related to the grayscale value of the noise-free image in
that region.
MATLAB Code:
% Load the image
original_image = imread('image.jpeg');
original_image_gray = rgb2gray(original_image); % Convert to grayscale if
necessary
% Add noise
salt_and_pepper_noise = imnoise(original_image_gray, 'salt & pepper', 0.05);
speckle_noise = imnoise(original_image_gray, 'speckle', 0.02);
gaussian_noise = imnoise(original_image_gray, 'gaussian', 0, 30/255);
% Define Wiener filter parameters
kernel_size = 5;
kernel = ones(kernel_size) / kernel_size^2;
noise_var = 25; % Assuming noise variance, you may need to adjust this based
on your image and noise level
% Deblurring and denoising using Wiener filter
deblurred_salt_and_pepper = deconvwnr(salt_and_pepper_noise, kernel,
noise_var);
deblurred_salt_and_pepper = im2uint8(mat2gray(deblurred_salt_and_pepper));
deblurred_speckle = deconvwnr(speckle_noise, kernel, noise_var);
deblurred_speckle = im2uint8(mat2gray(deblurred_speckle));
deblurred_gaussian = deconvwnr(gaussian_noise, kernel, noise_var);
deblurred_gaussian = im2uint8(mat2gray(deblurred_gaussian));
% Display results
figure;
subplot(2, 3, 1), imshow(salt_and_pepper_noise), title('Salt & Pepper
Noise');
subplot(2, 3, 2), imshow(deblurred_salt_and_pepper), title('Deblurred (Salt &
Pepper)');
subplot(2, 3, 3), imshow(speckle_noise), title('Speckle Noise');
subplot(2, 3, 4), imshow(deblurred_speckle), title('Deblurred (Speckle)');
subplot(2, 3, 5), imshow(gaussian_noise), title('Gaussian Noise');
subplot(2, 3, 6), imshow(deblurred_gaussian), title('Deblurred (Gaussian)');
Output:
Fig. 7.1 Salt & Pepper Noise added image, Deblurred (Salt and Pepper) image, Speckle Noise added
image, Deblurred(Speckle) image, Gaussian noise added image, Deblurred (Gaussian) image
Aim: Write a program to read the RGB image and calculate the DCT coefficients of the image
and recover the original image after applying Inverse DCT.
Theory:
The Discrete Cosine Transform (DCT) represents an image as a sum of sinusoids of varying
magnitudes and frequencies. The DCT is often used in image compression applications. The
DCT has the property that, for a typical image, most of the visually significant information
about the image is concentrated in just a few coefficients of the DCT.
A Discrete Cosine Transform (DCT) expresses a finite sequence of data points in terms of a
sum of cosine functions oscillating at different frequencies. The Discrete Cosine Transform
(DCT) helps separate the image into parts (or spectral sub-bands) of differing importance (with
respect to the image's visual quality). The DCT is similar to the discrete Fourier transform: it
transforms a signal or image from the spatial domain to the frequency domain.
MATLAB Code:
Output:
Aim: Add Gaussian noise to a grayscale image and then recover the original image by
appropriate filter. Calculate the parameter MSE, and PSNR of the original and recovered
image.
Theory:
Gaussian noise is a type of additive noise that is commonly used to simulate noise in digital
images. In this experiment, the Gaussian noise is added to the grayscale image using
MATLAB's built-in 'imnoise' function. The noise can be controlled by adjusting the mean and
standard deviation of the Gaussian distribution.
After adding the noise, an appropriate filter is used to recover the original image. The choice
of filter depends on the characteristics of the noise and the image. For instance, if the noise is
characterized by high frequency components, a low-pass filter such as a Gaussian filter or
median filter may be used to suppress the noise.
Once the original image is recovered, the MSE and PSNR of the original and recovered images
are calculated to evaluate the quality of the recovered image. The MSE measures the average
squared difference between the original and recovered images, while the PSNR measures the
ratio of the peak signal power to the noise power, expressed in decibels (dB). A higher PSNR
value indicates better image quality.
MATLAB Code:
Output:
Fig 9.1 Original Image, , Image with Gaussian Noise, Recovered Image
Result:
Gaussian noise is successfully added to a grayscale image and recovered using filter. Further,
MSE, and PSNR values are also calculated for original and recovered images.This experiment
provides hands-on experience with image noise reduction techniques using MATLAB and
demonstrates the importance of evaluating the quality of the recovered image using objective
measures such as MSE and PSNR.
Experiment-10
Aim: Write a program to perform bit slicing of a color image and plot them.
Theory:
The experiment involves bit slicing of a color image and plotting the individual bit planes to
analyse the image at the binary level. Bit slicing is a technique that involves extracting the bits
of an image that correspond to a particular binary digit or bit plane. The purpose of this
technique is to analyse the contribution of each bit plane to the overall image appearance, and
it can be used in digital image processing for image enhancement, compression, and feature
extraction.
MATLAB Code:
Result:
Bit slicing is performed successfully on a colored image. By plotting the individual bit planes
of a color image, the experiment also provides insight into how the different bits of an image
contribute to its visual appearance.
Experiment-11
Aim: Write a program to perform different morphological operation (a) Dilation (b) Erosion
(c) Open (d) Close on a colored image and perform edge thinning operation using it.
Theory:
Dilation: Dilation is an operation that “grows” or “thickens” objects in a binary image. The
specific manner and extent of this thickening is controlled by a shape which is known as
“structuring element”.
Erosion: Erosion “shrinks” or “thins” objects in any binary image. The manner and extent of
shrinking is controlled by structuring element.
Open: The opening operation erodes an image and then dilates the eroded image, using the same
structuring element for both operations.
Close: The closing operation dilates an image and then erodes the dilated image, using the same
structuring element for both operations.
MATLAB Code:
Result: We have successfully written a code to to perform different image segmentation using
Canny edge detection and Ostu method.
Experiment-12
Aim: Write a program to perform different image segmentation using (a) Canny edge
detection (b) Ostu method and compare their results.
Theory:
Image Segmentation: Image segmentation subdivides an image into its constituent regions or
objects. The level to which the sub-division is carried depend on the problem being solved.
Segmentation should stop when the objects of interest in an application have been isolated.
Image segmentation has the characteristics of gray value and edge information of the target
object, which provides conditions for image processing such as image semantic understanding.
Its purpose is to extract the object of interest from an image with a complex background, which
means to segment the regions of interest with consistent characteristics in the image. Image
segmentation has been widely used in medical image analysis, work-piece nondestructive
analysis, radar image analysis, geological exploration and other fields, and the quality of
segmentation directly affects the subsequent understanding of images.
Canny edge detection: It mainly finds edges by looking for local maxima of the gradient of 𝑓(𝑥,
𝑦). The gradient is calculated is using the derivative of a Gaussian filter. The method uses two
thresholds to detect strong and week edges and included week edges in the output only if they
are connected to strong edges. Therefore, this method is more likely to detect true week edges.
MATLAB Code:
Output:
Result: We have successfully written a code to to perform different image segmentation using
Canny edge detection and Ostu method.