0% found this document useful (0 votes)
16 views37 pages

IP - Manual (1) 1

Uploaded by

khushishukla639
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)
16 views37 pages

IP - Manual (1) 1

Uploaded by

khushishukla639
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/ 37

211310142049

IMAGE PROCESSING
LAB MANUAL

Name - Khushi Shukla


Enrollment No - 211310142049
Class - CSE (AI-ML)

Batch - C3

INDEX
211310142049

Sr Practical Date Grade Sign


No.
1 Introduction to Matlab Commands.

2 Introduction to Image Manipulation


Commands.

3 Implementation of Gray Scale image, Log


image, Gamma image, Neighborhood of
Pixel, Masking of an Image, Bitget image
and Histogram plot.
4 Implementation of coding Linear
Transformation, Grey Level Slicing, Low
pass filter (Medium and Average).
5 Implementation of Histogram Equivalence
and Histogram Matching.

6 Implementation of Prewitt, Roberts, Canny


and Sobel edge detectors using built-in
function and through coding.

7 Implementation of Gaussian, Uniform,


Erlang, Salt & Paper, Exponential, Rayliegh
Noises.

Practical 1
Introduction to Matlab commands:
1) Matrix Creation:

A = [1,2,3;4,5,6;7,8,9]
211310142049

2) Matrix Multiplication:

B = [1,2,3;4,5,6;7,8,9]
A*B

3) Matrix Dot Multiplication:

A.*B

4) Matrix Division:

A/B
211310142049

5) Matrix Dot Multiplication:

A./B

6) Matrix Addition:

A+B

7) Matrix Size:

size(A)

8) Matrix Column Slicing:


211310142049

A(:,1)

9) Matrix Row Slicing:

A(1,:)

10) Matrix Flip up and down:

flipud(A)

11) Matrix Flip Right and Left: fliplr(A)

12) Matrix Rotate 90 degree:


rot90(A)
211310142049

13) Matrix Inverse:


A*B

14) Matrix Indexing:


A(2,3)
A(3,2)

15) Matrix Transpose:


A’

16) Matrix Zeroes:


Zeros(3,3)
211310142049

17) Matrix Ones:


Ones(3,3)

18) Who and Whos:


Who

19) SQRT:

d = 23.4123;
sqrt(d)

20) CEIL:

ceil(d)

21) ROUND:

round(d)
211310142049

22) FLOOR:

floor(d)

Output of 19 to 22:
211310142049

Practical 2
Image Manipulation Commands:
The commands for image manipulation will include:
• Imread
• Imshow
• Imfinfo
• Info.Filename
• Info.FileSize
• Info.Width
• Info.Height
• Info.BitDepth
• Info.ColorType
• Info.FileModDate
• Subplot
• RGB channel
• RGBtoGray Code:
% Reading and displaying an image img
= imread('coloredChips.png');
imshow(img)

% Image information info =


imfinfo('coloredChips.png');
filename = info.Filename filesize =
info.FileSize width = info.Width
height = info.Height bitdepth =
info.BitDepth colortype =
info.ColorType filemoddate =
info.FileModDate

% Subplot and channel separation


subplot(2, 2, 1); imshow(img)
redChannel = img(:, :, 1);
greenChannel = img(:, :, 2);
blueChannel = img(:, :, 3); subplot(2,
2, 2); imshow(redChannel) subplot(2,
211310142049

2, 3); imshow(greenChannel) subplot(2,


2, 4); imshow(blueChannel)

% RGB to Grayscale
grayImage = rgb2gray(img);
imshow(grayImage)
Output:
211310142049

Practical 3
Implementation of different techniques.

The points to be covered are:

1) Gray Scale Image:


a = imread('cameraman.tif');
l1 = 255; s1 =
l1 - 1 - a;
subplot(1, 2, 1); imshow(a); title('Original');
subplot(1, 2, 2); imshow(s1); title('Gray Scale
Image');

2) Log Image:
img = imread('cameraman.tif');
r2 = double(img); c2 = 1; l2
= 255; s2 = c2 * log(1 + r2);
t2 = l2 / (c2 * log(1 + l2));
b2 = uint8(t2 * s2);
subplot(1, 2, 1); imshow(img); title('Original');
subplot(1, 2, 2); imshow(b2); title('Logarithmic');
211310142049

3) Gamma Image (0.5):


a = imread('cameraman.tif');
r3 = double(a); c3 = 1; l3
= 255; g1 = 0.5; s3 = c3 *
(r3 .^ g1); t3 = l3 / (c3 *
(l3 .^ g1)); b3 = uint8(t3 *
s3);
subplot(1, 2, 1); imshow(a); title('Original');
subplot(1, 2, 2); imshow(b3); title('Gamma');

4) Gamma Image (0.5):


211310142049

a = imread('cameraman.tif');
r3 = double(a);
c3 = 1; l3 =
255; g1 = 1;
s3 = c3 * (r3 .^ g1); t3 =
l3 / (c3 * (l3 .^ g1)); b3 =
uint8(t3 * s3);
subplot(1, 2, 1); imshow(a); title('Original');
subplot(1, 2, 2); imshow(b3); title('Gamma');

5) Gamma Image (0.5):


a = imread('cameraman.tif');
r3 = double(a); c3 = 1; l3 =
255; g1 = 5; s3 = c3 *
(r3 .^ g1); t3 = l3 / (c3 *
(l3 .^ g1)); b3 = uint8(t3 *
s3);
subplot(1, 2, 1); imshow(a); title('Original');
subplot(1, 2, 2); imshow(b3); title('Gamma');
211310142049

6) Neighbourhood of Pixel:
a = magic(8); b = input('Enter Row: '); c =
input('Enter col: ');

N4 = [a(b - 1, c), a(b, c + 1), a(b, c - 1), a(b + 1,


c)]; N4

N8 = [a(b - 1, c - 1), a(b - 1, c), a(b - 1, c + 1),


a(b, c - 1), a(b, c + 1), a(b + 1, c - 1), a(b + 1,
c), a(b + 1, c + 1)];
N8

ND = [a(b - 1, c - 1), a(b - 1, c + 1), a(b + 1, c -


1), a(b + 1, c + 1)];
ND
211310142049

7) Masking:
a = ones(40); b =
zeros(40); c = [a
b; b a]; d = [b
b; a a]; A = 10 *
(c + d);
M = c .* d;
D = c ./ d;
S = c - d;
subplot(2, 3, 1); imshow(c); title('C'); subplot(2,
3, 2); imshow(d); title('D'); subplot(2, 3, 3);
imshow(A); title('A'); subplot(2, 3, 4); imshow(M);
title('M'); subplot(2, 3, 5); imshow(D);
title('D'); subplot(2, 3, 6); imshow(S);
title('S');
211310142049

8) Bitget Image:
a = imread('baby.jpg'); %
Obtain the bit planes b0
= double(bitget(a, 1));
b1 = double(bitget(a,
2)); b2 =
double(bitget(a, 3)); b3
= double(bitget(a, 4));
b4 = double(bitget(a,
5)); b5 =
double(bitget(a, 6)); b6
= double(bitget(a, 7));
b7 = double(bitget(a,
8));
% Show the images
subplot(3, 3, 1); imshow(a); title('Original');
subplot(3, 3, 2); imshow(b0); title('b0'); subplot(3,
3, 3); imshow(b1); title('b1'); subplot(3, 3, 4);
imshow(b2); title('b2'); subplot(3, 3, 5);
imshow(b3); title('b3'); subplot(3, 3, 6);
imshow(b4); title('b4'); subplot(3, 3, 7);
imshow(b5); title('b5'); subplot(3, 3, 8);
imshow(b6); title('b6'); subplot(3, 3, 9);
imshow(b7); title('b7');
211310142049

9) Histogram Plot:
a =
imread('cameraman.tif');
b = imhist(a); c =
histeq(a); d =
imhist(c);
subplot(2, 2, 1); imshow(a); title('Original');
subplot(2, 2, 2); imhist(a); title('Histogram');
subplot(2, 2, 3); imshow(c); title('Equalization');
subplot(2, 2, 4); imhist(c); title('Hist-Equal');
211310142049

Practical 4
Implementation of Transformation:

1) Coding Linear Transformation


a = imread('cameraman.tif'); img
= im2double(a);
c = 1; g
= 0.6;
[row, col] = size(img); log_img
= zeros(row, col); power_img =
zeros(row, col);

for i = 1:row

for j = 1:col
log_img(i, j) = c * log(1 + img(i, j));
power_img(i, j) = c * img(i, j) .^ g;
211310142049

end
end
subplot(1, 3, 1); imshow(a); title('Original');
subplot(1, 3, 2); imshow(log_img);
title('Logerithmic');
subplot(1, 3, 3); imshow(power_img); title('Power');

2) Grey Level Slicing


a = imread('cameraman.tif');

[row, col] = size(a);


t2 = 255;
t1 = round(t2 / 1.4);

for i = 1:row

for j = 1:col

if a(i, j) > t1 && a(i, j) < t2


img1(i, j) = a(i, j); % with background
img2(i, j) = 255; % without background
else
img1(i, j) = 0;
img2(i, j) = 0;
end

end
end
subplot(1, 3, 1); imshow(a); title('Original');
subplot(1, 3, 2); imshow(img1); title('With
background');
subplot(1, 3, 3); imshow(img2); title('Without
background');
211310142049

3) Low Pass Filter i= imread('cameraman.tif'); j


= imnoise(i, 'salt & pepper', 0.10);
f1 = medfilt2(j); f2 =
medfilt2(j, [3 3]); f3 =
medfilt2(j, [10 10]);

subplot(2, 2, 1); imshow(i); title('Original');


subplot(2, 2, 2); imshow(j); title('Noise');
subplot(2, 2, 3); imshow(f2); title('3x3 mask');
subplot(2, 2, 4); imshow(f3); title('10x10 mask');

4) Low Pass Filter (Medium and Average)


211310142049

i = imread('cameraman.tif'); g1 =
fspecial('average', [3 3]); g2 =
fspecial('average', [10 10]);

b1 = imfilter(i, g1); b2
= imfilter(i, g2);

subplot(1, 3, 1); imshow(i); title('Original');


subplot(1, 3, 2); imshow(b1); title('3x3 avg mask');
subplot(1, 3, 3); imshow(b2); title('10x10 avg mask');

Practical 5
Implementation of Histograms:
1) Histogram Equivalence
a = imread('circuit.tif');
r = size(a, 1); c =
size(a, 2); ah =
uint8(zeros(r, c));
nk = r * c; f =
zeros(256, 1); pdf =
zeros(256, 1); cdf =
zeros(256, 1); cum =
zeros(256, 1); out =
zeros(256, 1);

for i = 1:r

for j = 1:c
value = a(i, j);
211310142049

f(value + 1) = f(value + 1) + 1;
pdf(value + 1) = f(value + 1) / nk; end
end sum = 0; L = 255;
for i = 1:size(pdf) sum =
sum + f(i); cum(i) = sum;
cdf(i) = cum(i) / nk;
out(i) = round(cdf(i) * L);
end
for i = 1:r

for j = 1:c
ah(i, j) = out(a(i, j) + 1);
end
end
subplot(2, 2, 1); imshow(a); title('original image');
subplot(2, 2, 2); imhist(a); title('Hist'); subplot(2,
2, 3); imshow(ah); title('equalized image');
subplot(2, 2, 4); imhist(ah); title('equalized
histogram');

2) Histogram Matching
211310142049

a = imread('circuit.tif'); b
= imread('cameraman.tif');
graylevels = 1:256;
a_occurrences = zeros(size(graylevels, 1));
for pixel = 1:length(graylevels)
a_occurrences(pixel, 1) = sum(a(:) ==
graylevels(pixel));
end
N = sum(a_occurrences); a_pdf =
zeros(size(graylevels, 1)); for
pixel = 1:length(graylevels)
a_pdf(pixel, 1) = a_occurrences(pixel, 1) / N;
end
a_cdf = zeros(size(graylevels, 1));

for pixel = 1:length(graylevels)


a_cdf(pixel, 1) = sum(a_pdf(1 : pixel));
end
a_sk = a_cdf * 256;
a_newHistVals = round(a_sk, 0);

a_new = zeros(size(a));

for row = 1:size(a, 1)


for col = 1:size(a, 2)
a_new(row, col) = a_newHistVals(a(row, col) +
1);
end end
a_newOccur = zeros(size(graylevels, 1));

for pixel = 1:length(graylevels)


a_newOccur(pixel, 1) = sum(a_new(:) ==
graylevels(pixel));
end
b_occurrences = zeros(size(graylevels, 1));

for pixel = 1:length(graylevels)


b_occurrences(pixel, 1) = sum(b(:) ==
graylevels(pixel)); end

N = sum(b_occurrences);
b_pdf = zeros(size(graylevels, 1));

for pixel = 1:length(graylevels)


b_pdf(pixel, 1) = b_occurrences(pixel, 1) / N;
211310142049

end
b_cdf = zeros(size(graylevels, 1));

for pixel = 1:length(graylevels)


b_cdf(pixel, 1) = sum(b_pdf(1 : pixel));
end b_sk = b_cdf *
256;
b_newHistVals = round(b_sk, 0);

b_new = zeros(size(b));

for row = 1:size(b, 1)


for col = 1:size(b, 2)
b_new(row, col) = b_newHistVals(b(row, col) +
1);
end end
b_newMatched = zeros(size(a)); for
row = 1:size(b_new, 1) for col =
1:size(b_new, 2) intensity =
b_new(row, col); [dummy,
matching_index] =
min(abs(a_newHistVals - intensity));
b_newMatched(row, col) =
a_newHistVals(matching_index);
end
end
a_new = uint8(a_new); b_new
= uint8(b_new);
b_newMatched = uint8(b_newMatched);

subplot(3, 2, 1); imshow(a); title('Original A');


subplot(3, 2, 2); imshow(b); title('Original B');
subplot(3, 2, 3); imhist(a_new); title('Histogram A');
subplot(3, 2, 4); imhist(b_new); title('Histogram B');
subplot(3, 2, 5); imhist(b_newMatched);
title('Histogram B Matched'); subplot(3, 2, 6);
imshow(b_newMatched); title('Histogram B Matched
Image');
211310142049

Practical 6
Implementation of Edge detectors:
1) Built-in Functions
a = imread('circuit.tif');
a = rgb2gray(a); e1 =
edge(a, 'prewitt'); e2 =
edge(a, 'roberts'); e3 =
edge(a, 'canny'); e4 =
edge(a, 'sobel');

subplot(2, 2, 1); imshow(e1); title('Prewitt');


subplot(2, 2, 2); imshow(e2); title('Roberts');
subplot(2, 2, 3); imshow(e3); title('Canny');
subplot(2, 2, 4); imshow(e4); title('Sobel');
211310142049
2)

Prewitt Edge Detector


a = imread('cameraman.tif'); gxp
= [-1 0 1; -1 0 1; -1 0 1]; gyp =
[-1 -1 -1; 0 0 0; 1 1 1]; igxp =
conv2(gxp, a); igyp = conv2(gyp,
a); igxp = igxp / 255; igyp =
igyp / 255; gp = sqrt(igxp .^ 2 +
igyp .^ 2); e = edge(a,
'prewitt');

subplot(3, 1, 1); imshow(a); title('Original');


subplot(3, 2, 3); imshow(igxp); title('Gx'); subplot(3,
2, 4); imshow(igyp); title('Gy'); subplot(3, 2, 5);
imshow(e); title('Prewitt'); subplot(3, 2, 6);
imshow(gp); title('G');
3)

Roberts Edge Detector


a = imread('cameraman.tif'); gxr
= [1 0; 0 -1]; gyr = [0 1; -1 0];
igxr = conv2(gxr, a); igyr =
conv2(gyr, a); gr = sqrt(igxr .^
2 + igyr .^ 2);
e = edge(a, 'roberts');

subplot(3, 2, 1); imshow(a); title('Original');


subplot(3, 2, 3); imshow(igxr); title('Gx'); subplot(3,
2, 4); imshow(igyr); title('Gy'); subplot(3, 2, 5);
imshow(e); title('Roberts'); subplot(3, 2, 6);
imshow(gr); title('G');
4)

Canny Edge Detector


a = imread('cameraman.tif');
gxc = [-1 0 1; -2 0 2; -1 0 1];
gyc = [-1 -2 -1; 0 0 0; 1 2 1];
igxc = conv2(gxc, a);
igyc = conv2(gyc, a);
igxc = igxc / 255; igyc
= igyc / 255;
gc = sqrt(igxc .^ 2 + igyc .^ 2);
e = edge(a, 'canny');

subplot(3, 2, 1); imshow(a); title('Original');


subplot(3, 2, 3); imshow(igxc); title('Gx'); subplot(3,
2, 4); imshow(igyc); title('Gy'); subplot(3, 2, 5);
imshow(e); title('Canny'); subplot(3, 2, 6);
imshow(gc); title('G');
5)

Sobel Edge Detector

a = imread('cameraman.tif');
gxs = [-1 0 1; -2 0 2; -1 0 1];
gys = [1 2 1; 0 0 0; -1 -2 -1];
igxs = conv2(gxs, a); igys =
conv2(gys, a); igxs = igxs /
255; igys = igys / 255;
gs = sqrt(igxs .^ 2 + igys .^ 2);
e = edge(a, 'sobel');

subplot(3, 2, 1); imshow(a); title('Original');


subplot(3, 2, 3); imshow(igxs); title('Gx'); subplot(3,
2, 4); imshow(igys); title('Gy'); subplot(3, 2, 5);
imshow(e); title('Sobel'); subplot(3, 2, 6);
imshow(gs); title('G');
6)
Practical 7
Implementation of Noises:
1) Gaussian
img = imread('Cameraman.tif');
a = 1; b
= 25;
[row, col] = size(img);
noise = a + (b - a) * randn([row, col]);
gaus = double(img) + noise; gaus
= uint8(gaus);

subplot(1, 2, 1); imshow(img); title('Original');


subplot(1, 2, 2); imshow(gaus); title('Gaussian
Noise');

Uniform
img =
imread( a =
1; b = 250;
gau = zeros(row, col);

for i = 1:row

for j = 1:col
2)
'Cameraman.tif');

noise = a + (b - a) * rand();
gau(i, j) = noise;
end end
img_with_noise = double(img) + gau; img_with_noise
= min(max(img_with_noise, 0), 255);
img_with_noise = uint8(img_with_noise);

subplot(2, 1, 1); imshow(img); title('Original');


subplot(2, 1, 2); imshow(img_with_noise);
title('Uniform Noise');

Erlang
img =
imread( a =
0.05; b = 5;
k = 1 / a;
R = zeros(row, col);

for j = 1:b
R = R + k * log(1 - rand(row, col));
end
3)
'Cameraman.tif');

erl = double(img) + R; erl


= uint8(erl);

subplot(1, 2, 1); imshow(img); title('Original');


subplot(1, 2, 2); imshow(erl); title('Erlang Noise');

Salt & Paper


img =
imread( a =
0.02; b =
0.05; R =
img;
X = rand(row, col);
R(X <= a) = 0; c
= a + b;
d = find(X > a & X <= c);
R(d) = 255;

subplot(1, 2, 1); imshow(img); title('Original');


subplot(1, 2, 2); imshow(R); title('Salt and Pepper
Noise');
4)
'Cameraman.tif');

Exponential
img =
imread( a =
0.05; k = 1 /
a;
R = k * log(1 - rand(row, col));
expo = double(img) + R; expo =
uint8(expo);

subplot(1, 2, 1); imshow(img); title('Original');


subplot(1, 2, 2); imshow(expo); title('Exponential
Noise');
5)
'Cameraman.tif');

Rayliegh
img =
imread( a =
0; b = 25;
noise = a + (-b * log(1 - rand([row, col])));
rayel = double(img) + noise; rayel
= uint8(rayel);

subplot(1, 2, 1); imshow(img); title('Original');


subplot(1, 2, 2); imshow(rayel); title('Rayleigh
Noise');
6)
'Cameraman.tif');

You might also like