Experiment 1: Digital Image
Experiment 1: Digital Image
Experiment 1: Digital Image
Fig1
Pixel of an Image: Pixel is the smallest element of an image. Each pixel stores a value
proportional to the light intensity at that particular location
n.
RGB to GRAY Conversion: Convert RGB (Color Image) into Grayscale Image. If R,
G, B are Red, Green and Blue Components of color Image then
𝑅+𝐺+𝐵
Gray scale Image I =
3
Read an image:
MATLAB Syntax: A = imread(filename, fmt) reads a grayscale or color image
from the file specified by the string filename. If the file is not in the current folder,
or in a folder on the MATLAB path, specify the full pathname.
MATLAB Example:
I=imread('peppers.png');
Display an Image
MATLAB Syntax: imshow(I) displays the grayscale image I.
MATLAB Example:
imshow(I); title('Peppers')
Size of an Image
MATLAB Syntax: [rows columns Plane]=size(image);
MATLAB Example:
[r c]=size(I);
Resizing of image
MATLAB Syntax: B = imresize(A, scale) returns image B that is scale times the
size of A. The input image A can be a grayscale, RGB, or binary image. If scale
is between 0 and 1.0, B is smaller than A. If scale is greater than 1.0, B is larger
than A.
B = imresize(A, [numrows numcols]) returns image B that has the number of
rows and columns specified by [numrows numcols]. Either numrows or
numcols may be NaN, in which case imresize computes the number of rows or
columns automatically to preserve the image aspect ratio.
MATLAB Example:
B=imresize(I,0.5);
imshow(B),title('RESIZED BY FACTOR OF 0.5');
B1=imresize(I,2);
imshow(B),title('RESIZED BY FACTOR OF 2');
Rotation of Image
MATLAB Syntax: B = imrotate(A,angle) rotates image A by angle degrees in a
counterclockwise direction around its center point. To rotate the image
clockwise, specify a negative value for angle
MATLAB Example:
C=imrotate(I,-90);
imshow(C),title('90 ROTATED CLOCKWISE');
Crop an Image
MATLAB Syntax: I2 = imcrop(I, rect) crops the image I. rect is a four-element
position vector[xmin ymin width height] that specifies the size and position of the
crop rectangle
MATLAB Example:
E=imcrop(I,[330 210 100 50]);
imshow(E),title('CROPPED IMAGE');
CODE:
clc;clear all; close all;
a=imread("peppers.png");
% imshow(a);
b=rgb2gray(a);
% imshow(b);
[r c]=size(b);
subplot(3,3,1);
imshow(a); xlabel('original image');
for n=1:8
for i=1:r
for j=1:c
k(i,j)=bitget(b(i,j),n);
end
end
d=strcat(int2str(n),'bit');
subplot(3,3,9-n+1); imshow(k, []); xlabel(d);
end
e=imread("C:\Users\5810706\Desktop\1.jpg");
figure; imshow(e);
Results:
Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
EXPERIMENT – 2
Aim- To write programs for low pass (smoothing filters) and high pass (sharpening filters)
filers.
MATLAB Code -
%% smoothening filters
I=imread(image.jpg');
Igr=rgb2gray(I);
% Adding noise to the image
Ino=imnoise(Igr,'salt & pepper',0.08);
figure,subplot(1,3,1),imshow(I),title('Coloured Original Image ');
subplot(1,3,2),imshow(Igr),title('grayScale Original Image');
subplot(1,3,3),imshow(Ino),title('Image with added Noise');
%Average filtering
Fa=1/9*[1,1,1;1,1,1;1,1,1]; %average Filter mask
Ia=imfilter(Ino,Fa);
figure,subplot(221),imshow(Ino),title('Noisy image');
subplot(222),imshow(Ia),title('Image after Average Filtering');
% weighted average filtering
Fwa=1/16*[1,2,1;2,4,2;1,2,1]; %weighted average filter mask
Iwa=imfilter(Ino,Fwa);
subplot(223),imshow(Iwa),title('Image after weighted Average Filtering');
%median filter
Im3=medfilt2(Ino,[3,3]);
subplot(224),imshow(Im3),title('Image after Median filtering[3x3 mask]');
Im5=medfilt2(Ino,[5,5]);
Im7=medfilt2(Ino,[7,7]);
figure,subplot(1,3,1),imshow(Im3),title('Median filtering by 3X3')
subplot(1,3,2),imshow(Im5),title('Median filtering by 5X5')
subplot(1,3,3),imshow(Im7),title('Median filtering by 7X7')
%% sharpening filters
Is=imread('im3.jpeg');
Isgr=rgb2gray(I);
Isob=edge(Isgr,'sobel');
Ipre=edge(Isgr,'prewitt');
Irob=edge (Isgr,'roberts');
figure,subplot(2,3,2),imshow(Isgr),title('grayScale Image');
subplot(2,3,4),imshow(Isob),title('Edge detection by sobel filter');
subplot(2,3,5),imshow(Ipre),title('Edge detection by prewitt filter');
subplot(2,3,6),imshow(Irob),title('Edge detection by roberts filter');
Results- A) Smoothening Filters:
1. Averaging filter
2. Weighted Average Filter
3. Median Filtering
B) Sharpening Filters (HPF)
1. Sobel’s Edge Detection:
2. Prewitt’s Edge Detection:
3. Robert’s Edge Detection :
Conclusion - Smoothening Filters and Sharpening Filters has been studied and results has been
obtained.
CODE:
clc;clear all; close all;
a=imread("peppers.png");
% imshow(a);
b=rgb2gray(a);
% imshow(b);
[r c]=size(b);
figure; subplot(3,3,1)
imshow(a); xlabel('original image');
e=imread("C:\Users\5810706\Desktop\1.jpg");
subplot(3,3,2); imshow(e); xlabel('binary image');
h=imresize(e,[r c]);
for i=1:r
for j=1:c
k(i,j)=bitget(b(i,j),8);
g(i,j)=bitset(b(i,j),8,h(i,j));
end
end
subplot(3,3,4); imshow(g, []); xlabel('water marked image');
subplot(3,3,3); imshow(k, []); xlabel('bit plane');
Result:
Fig. 1 a) Original Image, b) Binary Image to be added. c) Bit plane, d) Image added at 7th bit plane
location
Conclusion: Bit plane from a binary image has been added to the original image.
Criteria Total marks Marks obtained Comments
Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
Experiment 4
Aim- To implement low pass and high pass filters using Fourier Transform
Requirement- MATLAB 2019a
Theory:
2-D Fast Fourier Transform: Y = fft2(X) returns the two-dimensional Fourier transform of a
matrix using a fast Fourier transform algorithm, which is equivalent to computing fft(fft(X).').'.
If X is a multidimensional array, then fft2 takes the 2-D transform of each dimension higher than
2. The output Y is the same size as X.
I1=fft2(I);
I2=fftshift(I1);
subplot(3,3,3);imshow(I1); xlabel('2-d Fourier Transformed Image');
subplot(3,3,2); imshow(I); xlabel('Grayscaled Image');
subplot(3,3,1); imshow(E); xlabel('Original Image');
M=zeros([m n]);
R=100;
for i=1:m
for j=1:n
D= ((((i-(m*0.5))^2)+((j-(n*0.5))^2))^0.5) ;
if D<R
M(i,j)=1;
else
M(i,j)=0;
end
end
end
I3=I2.*M;
I4=ifftshift(I3);
I5=ifft2(I4);
subplot(3,3,4);imshow(I3,[]); xlabel('After masking');
subplot(3,3,5);imshow(I5,[]); xlabel('low pass filtered image');
Result:
Fig. 1 a) Original Image, b) Grayscaled Image, c) 2-D Fourier Transformed image, d) Image after
Masking, e) Low Pass filtered image
Fig. 2 a) Original Image, b) Grayscaled Image, c) 2-D Fourier Transformed image, d) Image after
Masking, e) High Pass filtered image.
Conclusion: Low pass and high pass filters using fast Fourier transform has been
implemented.
Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6