0% found this document useful (0 votes)
10 views19 pages

Adsip

The document describes an experiment to detect edges in a color image using Sobel and Prewitt operators. It defines edge detection and the different operators. The code converts the image to grayscale, then manually and automatically applies the operators to detect edges, displaying the results.

Uploaded by

Silkie Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Adsip

The document describes an experiment to detect edges in a color image using Sobel and Prewitt operators. It defines edge detection and the different operators. The code converts the image to grayscale, then manually and automatically applies the operators to detect edges, displaying the results.

Uploaded by

Silkie Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

EXPERIMENT NO : 6

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.

SOFTWARE TOOL USED : MATLAB (Mathswork)

THEORY: Histogram equalization is a method to process images in order to improve the


contrast of an image by modifying the intensity distribution of the histogram. The objective of
this technique is to give a linear trend to the cumulative probability function associated with
the image. The processing of histogram equalization relies on the use of the cumulative
probability function (cdf). The cdf is a cumulative sum of all the probabilities lying in its domain.
This method is widely used in medical images to improve the views of bone structures in x-
ray images, and to better photograph the areas that are over or under-exposed. The
disadvantage of this method is that it is indiscriminate. 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.

MATLAB PROGRAM :

% Load the RGB image


rgbImage = imread('download.jpeg');

% Convert the RGB image to grayscale


grayImage = rgb2gray(rgbImage);

% Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)


claheImage = adapthisteq(grayImage, 'ClipLimit', 0.02, 'NumTiles', [8, 8]);

% Apply Brightness Preserving Bi-Histogram Equalization (BPBiHE)


alpha = 0.1;
beta = 0.1;
bpbiheImage = bpbihe(grayImage, alpha, beta);

% Display the original image, CLAHE image, and BPBiHE image


figure;
subplot(1, 3, 1);
imshow(rgbImage);
title('Original Image');
subplot(1, 3, 2);
imshow(claheImage);
title('CLAHE Image');
subplot(1, 3, 3);
imshow(bpbiheImage);
title('BPBiHE Image');

function outputImage = bpbihe(inputImage, alpha, beta)


% Compute histogram
[counts, ~] = imhist(inputImage);
% Compute cumulative distribution function
cdf = cumsum(counts);
% Normalize CDF
cdf = cdf / cdf(end);
% Compute the cut-off values
lowCut = find(cdf > alpha, 1);
highCut = find(cdf >= (1 - beta), 1, 'last');
% Compute the new intensity values
newValues = zeros(256, 1, 'uint8');
for i = 1:256
if i <= lowCut
newValues(i) = uint8((alpha * lowCut * i) / lowCut);
elseif i > lowCut && i < highCut
newValues(i) = uint8(((i - lowCut) * 255) / (highCut - lowCut));
else
newValues(i) = uint8(((i - highCut) * (255 - highCut)) / (255 - highCut));
end
end
% Apply the new intensity values to the input image
outputImage = newValues(inputImage + 1);
end

SIMULATION OUTPUT: Histogram equalization performed with and without inbuilt function.
EXPERIMENT NO :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.

SOFTWARE TOOL USED : MATLAB (Mathswork).

THEORY:In image processing, noise is an undesirable component that can degrade


the quality of an image. There are several types of noise, including salt and pepper
noise, speckle noise, and Gaussian noise. These types of noise can occur during image
acquisition, transmission, or processing.
Salt and Pepper Noise: This type of noise appears as random, isolated pixels in the
image with very high or very low intensity values, resembling grains of salt and pepper
scattered on an image. It can be caused by errors in data transmission or faults in the
image sensor.
Speckle Noise: Speckle noise is a granular noise that appears as grain-like patterns in
images, similar to the noise produced by adding salt and pepper to an image. It is often
found in images acquired through ultrasound, synthetic aperture radar (SAR), and other
coherent imaging systems.
Gaussian Noise: Gaussian noise is a type of statistical noise that follows a Gaussian
(normal) distribution. It is characterized by random variations in pixel intensity values
and can be caused by factors such as electronic interference in sensors or errors in
data transmission.
Wiener Filter: The Wiener filter is a method used for signal and image processing to
reduce noise in a signal or image. It is particularly effective when the characteristics of
the noise and the original signal are known or can be estimated. The Wiener filter works
by minimizing the mean square error between the original signal and the estimated
signal, taking into account the signal-to-noise ratio (SNR) of the image.
In the context of image deblurring and denoising, the Wiener filter can be applied to
restore images that have been degraded by blur or noise. By estimating the
characteristics of the blur or noise, the Wiener filter can effectively reduce these
artifacts and improve the overall quality of the image.

MATLAB PROGRAM :
% Load the image
original = imread('download (1).jpeg');
original = im2double(original);
originalImage = rgb2gray(original);

% Add noise to the image


saltPepperNoiseImage = imnoise(originalImage, 'salt & pepper', 0.1);
speckleNoiseImage = imnoise(originalImage, 'speckle', 0.04);
gaussianNoiseImage = imnoise(originalImage, 'gaussian', 0.01);

% Apply Wiener filter for each noise type


wienerSaltPepper = wiener2(saltPepperNoiseImage, [5, 5]);
wienerSpeckle = wiener2(speckleNoiseImage, [5, 5]);
wienerGaussian = wiener2(gaussianNoiseImage, [5, 5]);

% Display the original image and the denoised images


figure;
subplot(2, 4, 1);
imshow(originalImage);
title('Original Image', 'FontSize', 5);

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);

SIMULATION OUTPUT and RESULT:


deblurring and denoising operation using Wiener filter on an image corrupted by salt and
pepper, speckle and Gaussian noise successfully performed.
EXPERIMENT NO :8

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.

TOOLS USED: MATLAB R2012b

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');

SIMULATION AND RESULT:


DCT and IDCT function successfully applied on the image.
EXPERIMENT NO :4

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.

TOOLS USED: MATLAB R2012b

THEORY:

• An edge in an image is a discontinuity in the image brightness.


• Edge detection is a technique used to identify regions in the image where the brightness
changes sharply.
• This sharp change in intensity value is observed at the local minima or local maxima in the
image histogram, using the first-order derivative.

The techniques used in this experiment are first-order derivative techniques, namely:

1. Prewitt Operator
2. Sobel Operator

CODE:
clc;
clear;
close all;

% Load the colored image


originalImage = imread('download (2).jpeg');

% Convert the image to grayscale


I_gray = rgb2gray(originalImage);

% Convert grayscale image to double


I_gray = im2double(I_gray);

% Initialize the output images for manual approach


BW_sobel_manual = zeros(size(I_gray));
BW_prewitt_manual = zeros(size(I_gray));

% Define the Sobel and Prewitt operators


h_sobel = [1 0 -1; 2 0 -2; 1 0 -1];
h_prewitt = [1 1 1; 0 0 0; -1 -1 -1];

% 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);

% Store the edge strength as the pixel value


BW_sobel_manual(row, col) = gradient_sobel;
BW_prewitt_manual(row, col) = gradient_prewitt;
end
end

% Detect edges using Sobel operator (built-in function)


BW_sobel_auto = edge(I_gray, 'sobel');

% Detect edges using Prewitt operator (built-in function)


BW_prewitt_auto = edge(I_gray, 'prewitt');

% Display the results


figure;
subplot(2, 3, 1);
imshow(originalImage);
title('Original Image');

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.

Software used- MATLAB R2012b

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.

Software used- MATLAB R2012b

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

PSNR of original image: 21.8428060 dB

The mean-squared error of recovered image is 0.0065

PSNR of recovered image: 70.0064536 Db


EXPERIMENT- 10

Aim- Write a program to perform bit slicing of color image and plot them.

Software used- MATLAB

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);

% Initialize matrices for each bit plane


MSB = zeros(size(B));
LSB = zeros(size(B));
Second = zeros(size(B));
Third = zeros(size(B));
Fourth = zeros(size(B));
Fifth = zeros(size(B));
Sixth = zeros(size(B));
Seventh = zeros(size(B));

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

% Display all bit planes in one figure using subplots


figure;

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);

RESULT and OUTPUT:


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.

Software Used- MATLAB

Theory-Morphological Operations is a broad set of image processing operations that


process digital images based on their shapes. In a morphological operation, each image
pixel is corresponding to the value of other pixel in its neighborhood. By choosing the shape
and size of the neighborhood pixel, you can construct a morphological operation that is
sensitive to specific shapes in the input image. Morphological operations apply a structuring
element called strel in Matlab, to an input image, creating an output image of the same size.

Types of Morphological operations

Dilation: Dilation adds pixels on the object boundaries.


Erosion: Erosion removes pixels on object boundaries.
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:
% 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");

RESULT AND OUTPUT


EXPERIMENT – 12

Aim- Write a program to perform different image segmentation using (a) Canny edge
detection (b) Ostu method and compare their results.

Software Used- MATLAB

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');

RESULT AND OUTPUT:-

You might also like