Adsip
Adsip
AIM : 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.
MATLAB PROGRAM :
SIMULATION OUTPUT: Histogram equalization performed with and without inbuilt function.
EXPERIMENT NO :7
MATLAB PROGRAM :
% Load the image
original = imread('download (1).jpeg');
original = im2double(original);
originalImage = rgb2gray(original);
subplot(2, 4, 2);
imshow(saltPepperNoiseImage);
title('Salt & Pepper Noise', 'FontSize', 5);
subplot(2, 4, 3);
imshow(speckleNoiseImage);
title('Speckle Noise', 'FontSize', 5);
subplot(2, 4, 4);
imshow(gaussianNoiseImage);
title('Gaussian Noise', 'FontSize', 5);
subplot(2, 4, 6);
imshow(wienerSaltPepper);
title('Wiener Salt & Pepper', 'FontSize', 5);
subplot(2, 4, 7);
imshow(wienerSpeckle);
title('Wiener Speckle', 'FontSize', 5);
subplot(2, 4, 8);
imshow(wienerGaussian);
title('Wiener Gaussian', 'FontSize', 5);
AIM: Write a program to read 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 dct2 function computes the two-dimensional
discrete cosine transform (DCT) of an image. 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. For this reason, the DCT is often used in image compression
applications. For example, the DCT is at the heart of the international standard lossy image
compression algorithm known as JPEG.
CODE:
clc;
clear;
close all;
a = imread('download (2).jpeg');
subplot(2,2,1);
imshow(a);
title('Original Image');
b = rgb2gray(a);
subplot(2,2,2);
imshow(b);
title('Gray Scale Image');
c = dct2(b);
subplot(2,2,3);
imshow(log(abs(c)), []);
title('DCT image');
im = idct2(c);
subplot(2,2,4);
imshow(uint8(im));
title('IDCT Image');
AIM: Write a program to detect the edges of a coloured image using Sobel and Prewitt
operators and verify the results using the built-in commands.
THEORY:
The techniques used in this experiment are first-order derivative techniques, namely:
1. Prewitt Operator
2. Sobel Operator
CODE:
clc;
clear;
close all;
% Detect edges using the Sobel and Prewitt operators (manual approach)
for row = 2:size(I_gray, 1)-1
for col = 2:size(I_gray, 2)-1
% Calculate the gradient using the Sobel operator
Gx = sum(sum(h_sobel .* I_gray(row-1:row+1, col-1:col+1)));
Gy = sum(sum(h_sobel .* I_gray(row-1:row+1, col-1:col+1)'));
gradient_sobel = sqrt(Gx^2 + Gy^2);
% Calculate the gradient using the Prewitt operator
Gx = sum(sum(h_prewitt .* I_gray(row-1:row+1, col-1:col+1)));
Gy = sum(sum(h_prewitt .* I_gray(row-1:row+1, col-1:col+1)'));
gradient_prewitt = sqrt(Gx^2 + Gy^2);
subplot(2, 3, 2);
imshow(I_gray);
title('Grayscale Image');
subplot(2, 3, 3);
imshow(BW_sobel_manual);
title('Sobel Manual');
subplot(2, 3, 4);
imshow(BW_prewitt_manual);
title('Prewitt Manual');
subplot(2, 3, 5);
imshow(BW_sobel_auto);
title('Sobel Auto');
subplot(2, 3, 6);
imshow(BW_prewitt_auto);
title('Prewitt Auto');
Result: Results have been verified using operators and 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. In this mask we have two further classifications
one is Positive Laplacian Operator and other is Negative Laplacian Operator. Another
difference between Laplacian and other operators is that unlike other operators Laplacian
didn’t take out edges in any particular direction but it take out edges in following classification.
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;
clear;
i = imread('dog.jpg');
[x1,y1] = size(i);
disp([x1, y1]);
A = rgb2gray(i);
figure,imshow(A);
title('Original Image');
I1=A;
I=zeros(size(A));
I2=zeros(size(A));
F1=[0 1 0;1 -4 1; 0 1 0];
F2=[1 1 1;1 -8 1; 1 1 1];
A=padarray(A,[1,1]);
A=double(A);
for i=1:size(A,1)-2
for j=1:size(A,2)-2
I(i,j)=sum(sum(F1.*A(i:i+2,j:j+2)));
end
end
I=uint8(I);
[x2, y2] = size(I);
disp([x2, y2]);
figure,imshow(I);
title('Filtered Image');
B=I1-I;
[x3, y3] = size(B);
disp([x3, y3]);
figure,imshow(B);
title('Edge Detected Image Using Laplacian operator');
OUTPUT:
EXPERIMENT - 9
Aim- Write a program to add Gaussian noise to a grayscale image and recover original image
by appropriate filter. Calculate the parameter MSE, PSNR of the original and recovered image.
Theory- PSNR is used to measure the quality of reconstruction of lossy and lossless
compression (e.g., for image compression). The signal in this case is the original data, and
the noise is the error introduced by compression. When comparing compression codecs,
PSNR is an approximation to human perception of reconstruction quality. Although a higher
PSNR generally indicates that the reconstruction is of higher quality, in some cases it may not.
PSNR is most easily defined via the mean squared error.
MATLAB Code
clc;
close all;
P=imread('dog.jpg');
A = imnoise(P,'salt & pepper', 0.02);
err = immse(A, P);
fprintf('\nThe mean-squared error of original image is %0.4f\n', err);
PSNR=10*log10(256*256/err);
fprintf('\nPSNR of original image: %9.7f dB', PSNR);
subplot(1,2,1);
imshow(P);
title('Original Image');
P = im2double(P);
I=rgb2gray(P);
figure(3)
imshow(I);
title('Input Grayscale Image');
J = imnoise(I,'gaussian');
figure(4)
imshow(J);
title('Image with gaussian noise');
denoisedImage = conv2(double(J), ones(3)/9, 'same');
figure(5)
imshow(denoisedImage);
title('Filtered Image');
InputImage=J;
ReconstructedImage=denoisedImage; n=size(InputImage);
M=n(1);
N=n(2);
A = imnoise(ReconstructedImage,'salt & pepper', 0.02);
err = immse(A, ReconstructedImage);
fprintf('\n\nThe mean-squared error of recovered image is %0.4f\n', err);
PSNR=10*log10(256*256/err);
fprintf('\nPSNR of recovered image: %9.7f dB', PSNR);
Result and OUTPUT:
The mean-squared error of original image is 428.7453
Aim- Write a program to perform bit slicing of color image and plot them.
Theory-
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 analyze the contribution
of each bit plane to the overall image appearance and it can be utilized to different image
processing operations like image enhancement, image compression and feature extraction.
MATLAB Code
clc;
clear;
close all;
A = imread('flower.jpg');
B = rgb2gray(A);
for i = 1:size(B, 1)
for j = 1:size(B, 2)
MSB(i, j) = bitand(B(i, j), bin2dec('10000000'));
LSB(i, j) = bitand(B(i, j), bin2dec('00000001'));
Second(i, j) = bitand(B(i, j), bin2dec('01000000'));
Third(i, j) = bitand(B(i, j), bin2dec('00100000'));
Fourth(i, j) = bitand(B(i, j), bin2dec('00010000'));
Fifth(i, j) = bitand(B(i, j), bin2dec('00001000'));
Sixth(i, j) = bitand(B(i, j), bin2dec('00000100'));
Seventh(i, j) = bitand(B(i, j), bin2dec('00000010'));
end
end
subplot(2, 4, 1);
imshow(MSB);
title('Most Significant Bit', 'FontSize', 5);
subplot(2, 4, 2);
imshow(LSB);
title('Least Significant Bit', 'FontSize', 5);
subplot(2, 4, 3);
imshow(Second);
title('Second Bit', 'FontSize', 5);
subplot(2, 4, 4);
imshow(Third);
title('Third Bit', 'FontSize', 5);
subplot(2, 4, 5);
imshow(Fourth);
title('Fourth Bit', 'FontSize', 5);
subplot(2, 4, 6);
imshow(Fifth);
title('Fifth Bit', 'FontSize', 5);
subplot(2, 4, 7);
imshow(Sixth);
title('Sixth Bit', 'FontSize', 5);
subplot(2, 4, 8);
imshow(Seventh);
title('Seventh Bit', 'FontSize', 5);
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.
MATLAB CODE:
% Importing the image
I = imread("download (2).jpeg");
subplot(2, 3, 1);
imshow(I);
title("Original image");
% Dilated Image
se = strel("line", 7, 7);
dilate = imdilate(I, se);
subplot(2, 3, 2);
imshow(dilate);
title("Dilated image");
% Eroded image
erode = imerode(I, se);
subplot(2, 3, 3);
imshow(erode);
title("Eroded image");
% Opened image
open = imopen(I, se);
subplot(2, 3, 4);
imshow(open);
title("Opened image");
% Closed image
close = imclose(I, se);
subplot(2, 3, 5);
imshow(close);
title("Closed image");
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.
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.
OTSU method: OTSU is a global adaptive banalization threshold image segmentation
algorithm, it is put forward by Japanese scholars OTSU in 1979. This algorithm takes the
maximum inter class variance between the background and the target image as the
threshold selection rule. From the principle of the OTSU method, the maximum between-
class variance method is also its alias. It divides the image into foreground and background
according to its gray scale characteristics. When the best threshold is taken, the difference
between the two parts is the largest. OTSU algorithm uses the maximum inter-class variance
that is relatively common as the measure standard.
MATLAB Code-
% Load the image
originalImage = imread('cat.jpeg');
grayImage = rgb2gray(originalImage);
% Apply Canny edge detection
cannyEdges = edge(grayImage, 'canny');
% Apply Otsu's method
level = graythresh(grayImage);
otsuBinary = imbinarize(grayImage, level);
% Display the original image and the segmented images
figure;
subplot(1, 3, 1);
imshow(originalImage);
title('Original Image');
subplot(1, 3, 2);
imshow(cannyEdges);
title('Canny Edge Detection');
subplot(1, 3, 3);
imshow(otsuBinary);
title('Otsu''s Method');