Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Experiment 1

Aim: To study the basic image processing tools


a)
Input an image
b)
Resize and the image
c)
Convert the image into binary Grayscale
d)
Extract the RGB planes of color image.
e)
Extract the bit plane of grayscale image.
Requirement- MATLAB 2019a
Theory:
Digital Image: An image is a two-dimensional signal (MATRIX).
It is defined by the mathematical function f(x,y) where x and y are rows and
columns of matrix.
f(x,y) gives the pixel value at that point of an 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');

Extract Red, Green and Blue Channel of an Image


MATLAB Example:
I=imread('peppers.png');
figure(3),subplot(2,2,1),imshow(I),title('COLOUR IMAGE');
K=I(:,:,1);
subplot(2,2,2),imshow(K),title('RED CHANNEL');
L=I(:,:,2);
subplot(2,2,3),imshow(L),title('GREEN CHANNEL');
M=I(:,:,3);
subplot(2,2,4),imshow(M),title('BLUE CHANNEL');

RGB to Gray Conversion


MATLAB Syntax: I = rgb2gray(RGB) converts the truecolor image RGB to the
grayscale intensity image I.
MATLAB Example:
N = rgb2gray(I);
imshow(N),title('Gray Image');
Bit Plane Extraction:
MATLAB Syntax: C = bitget(A, bit) returns the value of the bit at position bit in A. Operand A
must be an unsigned integer, a double, or an array containing unsigned integers, doubles or both.
The bit input must be a number between 1 and the number of bits in the unsigned integer class of
A (e.g., 32 for the uint32 class).

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:

Original Image RESIZED BY FACTOR OF 0.5 RESIZED BY FACTOR OF 2

90 ROTATED CLOCKWISE CROPPED IMAGE RED CHANNEL

GREEN CHANNEL BLUE CHANNEL Gray Image


Conclusion: Basic study of image processing using MATLAB is done.

Criteria Total marks Marks obtained Comments

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.

Requirement- MATLAB 2019a


Theory-
J = imnoise(I,'salt & pepper',m) adds Salt & pepper noise with mean m and variance of 0.01.
B = imfilter(A,h) filters the multidimensional array A with the multidimensional filter h and returns the
result in B.
J = medfilt2(I,[m n]) performs median filtering, where each output pixel contains the median value in
the m-by-n neighborhood around the corresponding pixel in the input image.
N1=edge(Na,'sobel') performs edge detection either by sobel’s, prewitt’s or robert’s detection technique.

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.

Criteria Total marks Marks obtained Comments


Concept (A) 2
Implementation (B) 2
Performance (C) 2
Total 6
Experiment 3
Aim: To set a bit plane at a specific bit location.
Requirement- MATLAB 2019a
Theory:
Bit Plane Extraction:
MATLAB Syntax: C = bitget(A, bit) returns the value of the bit at position bit in A. Operand A
must be an unsigned integer, a double, or an array containing unsigned integers, doubles or both.
The bit input must be a number between 1 and the number of bits in the unsigned integer class of
A (e.g., 32 for the uint32 class).

Setting bit Plane at a specific location:


MATLAB Syntax: intout = bitset(A,bit) returns the value of A with position bit set to 1 (on).

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.

Shifting zero-frequency component to center of spectrum: Y =fftshift(X)


rearranges a Fourier transform X by shifting the zero-frequency component to the center of the
array.
 If X is a vector, then fftshift swaps the left and right halves of X.
 If X is a matrix, then fftshift swaps the first quadrant of X with the third, and the second
quadrant with the fourth.
 If X is a multidimensional array, then fftshift swaps half-spaces of X along each
dimension.

2-D inverse fast Fourier transform: X = ifft2(Y)


returns the two-dimensional discrete inverse Fourier transform of a matrix using a fast Fourier
transform algorithm. If Y is a multidimensional array, then ifft2 takes the 2-D inverse transform
of each dimension higher than 2. The output X is the same size as Y.
Inverse zero-frequency shift: x= ifftshift(Y) rearranges a zero-frequency-shifted Fourier
transform Y back to the original transform output. In other words, ifftshift undoes the result
of fftshift.
 If Y is a vector, then ifftshift swaps the left and right halves of Y.
 If Y is a matrix, then ifftshift swaps the first quadrant of Y with the third, and the second
quadrant with the fourth.
 If Y is a multidimensional array, then ifftshift swaps half-spaces of Y along each
dimension.
CODE:

clc; close all;


E=imread('C:\Users\5810706\Desktop\1.jpg');
I=rgb2gray(E);
[m,n]=size(I);

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.

Criteria Total marks Marks obtained Comments

Concept (A) 2

Implementation (B) 2

Performance (C) 2

Total 6

You might also like