Experiment 4madsip Lab
Experiment 4madsip Lab
Experiment 4madsip Lab
AIM: Write a program to detect the edges of a coloured image using Sobel and
Prewitt operators and verify the results using the inbuilt commands.
MATLAB CODE:
This MATLAB code performs edge detection on an input image using both
manual implementation and built-in functions for Sobel and Prewitt operators.
CODE:
% Read the image
I = irnread('fishjpg');
1
BW_prewitt:....manual = zeros(size(I_gray));
% Detect edges using the Sobel and Prewitt operators (manual approach) for row
=2:size(l_gray, 1)-1 for col =2:size(I_gray, 2)-l
% Calculate the gradient using the Sobel operator
Gx = sum(sum(h sobel * l_gray(row-l:row+ l,
col-l:col+ I)));
Gy = sum(sum(h_sobel * l _gray( row- I :row+ I ,
col-l:col+ 1)'));
gradient_sobel = sqrt(Gx"2 + Gy"2);
2
figure ; subplot(3, 2, 1);
imshow(I); title('Original
lmaot;,.e')·
,
3
EXPERIMENT 2
AIM: Write a program to perform 'zoom in' and 'zoom out' operation on an image by
200% and 50% respectively using (a) nearest neighbour (b) bi-linear interpolation (c) bi-
cubic interpolation.
THEORY: In the context of image processing, zooming refers to altering the scale of an
image, either by enlarging or reducing it Let's delve into the details:
1. Zoom In (Minimization):
• Purpose: To compress the size of an image.
• Effect: The image is enlarged, making details more visible and clearer.
• Techniques: Various algorithms manipulate pixels to zoom in on specific
portions of the image.
• Example Imagine focusing on a specific area of a photograph to see
intricate details.
2. Zoom Out (Maximization):
• Purpose: To expand or increase the size of an image.
• Effect: The image is reduced, showing a broader view with less detail.
• Techniques: Like zooming in but applied to a larger area.
• Example Viewing a wide landscape photo where distant objects appear
smaller.
MATLAB CODE:
1. using loop
(a). Nearest neighbour zoom in
4
% Detennine the new dimensions
newHeight= round(size( originall mage, I) * scalingfactor);
newWidth = round(size(originallmage, 2) * scalingfactor);
% Initialize the resized image
resizedimage = uint8(zeros(newHeight, newWidth, size(originallmage, 3)));
% Iterate over each pixel in the resized image for
i = I :newHeight
for j = I :newWidth
% Calculate the corresponding pixel in the original image
x = round(i I scalingfactor);
y = roundU / scalingfactor);
% Ensure the coordinates are within bounds x
= min(max(x, I), size(originallmage, I)); y=
min(max(y, I), size(originallmage, 2));
% Assign the pixel value from the original image to the resized image
resizedimage(i, j, :) = original image(x, y, ); end
end
% Display the original and resized images
figure; subplot( I, 2, I);
imshow(original Image); title('Original
Image');
subplot( ), 2, 2); imshow(resizedJmage);
title('zoomed in Image');
5
% Initialize the resized image
resizedimage = uint8(zeros(newHeight, newWidth, size(originallmage, 3)));
% Assign the pixel value from the original image to the resized image
resizedlmage(i, j , :) = originallmage(x, y, :); end
end
clc; clea r;
close all; z=2; %zooming
factor
A=imread('kobi. png');
subplot(2,2,1) imshow(A)
title('original image');
B=imresize(A,2,"nearest");
subplot(2,2,2) imshow(B)
6
title('zoomed in image with nearest neighbour' );
D=imresize(A,2,"bicubic");
subplot(2,2,4) imshow(D)
title( 'zoomed in image with bicubic');
A= imread('peppers.png');
subplot(2,2,1) imshow(A)
title( 'original image');
7
Experiment - 1
AIM : To write a program to read a RGB image and perform the following operations
on the image:
1. Extract red, green, and blue components of the image and then combine them to
get the original image.
MATLAB CODE:
1.
clc;
clear;
close all;
A=imread('peppers.png');
subplot(2,3,1)
8
imshow(A)
title('original image');
R=A(,:, 1);
subplot(2,3,2)
imshow(R)
title('Highlighted Red colour') G=A(:, ,2);
subplot(2,3,3)
imshow(G)
title('Highlighted GREEN colour')
B=A(: ,:,3);
subplot(2,3,4)
imshow(B)
title('Highlighted BLUE colour')
combined=cat(3,R,G,B);
subplot(2,3,5)
imshow( combined)
title('combined ofRGB')
Y=rgb2gray(A);
subplot(2,3,6)
imshow(Y)
title('RGB to grayscale')
2.
clc; clear; close all;
image=zeros(400,300,3); %
Either use this (This is a
comment)
9
image(:,:,! )=77/255;
image(:, :,2)=150/255;
image(:,:,3)=29/255;
% or this can be used (this is a comment)
%R=77/255;
%G=l50/255;
%B=29/255;
%image=cat(3,R,G,B);
R=A(:,:, l);
10
G=A(:,:,2);
B=A(:,:,3);
P=cat(3,R,G,B);
subplot(2,3,3) imshow(P)
title('First image after adding RGB of it'); [height_ I,
width_ 1, colour_ I]=size(A); disp(height_ I);
<lisp( width_ I); <lisp(colour_ I);
[height_2, width_2, colour_2]=size(B);
disp(height_ 2); disp(width_ 2); <lisp( colour_ 2);
C=P+B; subplot(2,3,4)
imshow(C)
title(' Addition');
D=P-B; subplot(2,3,5)
imshow(D)
title('Subtraction');
4.
etc; clear; close all;
B=zeros(400,300,3);
B(:,:, I)=77/255;
B(:,:,2)= 150/255;
B(:,:,3)=29/255;
subplot(2,3,2)
imshow(B)
11
title('Second image')
A=imread('peppers.png');
subplot(2,3, I) imshow(A)
title('First image');
12
EXPERIMENT 3
AIM : Write a program to construct (i) 5 x5 Gaussian filter (ii) average filter (iii) median filter
and compare their results on an image corrupted by salt and pepper noise.
THEORY: noise refers to unwanted modifications that a signal may undergo during capture,
storage, transmission, processing, or conversion. Digital filter is a system that operates on a
sampled, discrete-time signal to reduce or enhance certain aspects of that signal. Unlike analog
filters, w hich work with continuous-time analog signals, digital filters perform mathematical
operations o n digitized signals or numerical data stored in a computer memory
MATLAB CODE:
Filters using loop:
A= imread('fish.j pg');
B=rgb2gray(A);
filter_size = 3;
for i = I :rows
for j = 1:cols
13
row_indices = max(I, i • floor(filter_size/2)):min(rows, i + floor(filter_size/2));
col_ indices = max( I, j - floor( filter_ size/2)):min(cols, j + floor( filter_ size/2 ));
end
end
imshow(input_i mage);
title('noisy image');
subplot(2, 2, 4);
imshow(filtered_ image);
subplot(2, 2, I);
imshow(A);
title{'Original Image');
subplot(2, 2, 2);
imshow(B);
title{'gray image');
I=imread(" fish.jpg");
subplot(2,2, I)
imshow(I)
14
title('original image')
i=rgb2gray(I);
subplot(2,2,2)
imshow(i)
title('gray image')
subplot(2,2,3) imshow(N)
A=createGaussianfilter(sigma);
F=imfilter(N,A); subplot(2,2,4)
imshow(F)
title('filter image')
filterSize = 5;
normalizationfactor = O;
15
gaussianfilter(i + filterRadius + I, j + filterRadius + I)= exp(-(iA2 + Y2) I (2 * sigmaA2));
normalizationfactor = normalizationfactor + gaussianfilter(i + filterRadius + I, j + filterRadius
+ I);
end
end
clc; clear
; close
all;
subplot(2,3, I)
imshow(I)
title('original image')
i=rgb2gray(I);
subplot(2,3,2)
imshow(i)
title{'gray image')
subplot(2,3,3)
imshow(N)
title('noisy image')
G=imgaussfilt(N,5);
subplot(2,3,4) imshow(G)
16
title('image after gausian fi lter')
width=S;
filter-ones( width)/width"2;
A=imfilter(N, filter);
subplot(2,3,5) imshow(A)
M=medfilt2(N);
subplot(2,3,6) imshow(M)
17