IP - Manual (1) 1
IP - Manual (1) 1
IMAGE PROCESSING
LAB MANUAL
Batch - C3
INDEX
211310142049
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
A.*B
4) Matrix Division:
A/B
211310142049
A./B
6) Matrix Addition:
A+B
7) Matrix Size:
size(A)
A(:,1)
A(1,:)
flipud(A)
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)
% RGB to Grayscale
grayImage = rgb2gray(img);
imshow(grayImage)
Output:
211310142049
Practical 3
Implementation of different techniques.
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
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');
6) Neighbourhood of Pixel:
a = magic(8); b = input('Enter Row: '); c =
input('Enter col: ');
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:
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');
for i = 1:row
for j = 1:col
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
i = imread('cameraman.tif'); g1 =
fspecial('average', [3 3]); g2 =
fspecial('average', [10 10]);
b1 = imfilter(i, g1); b2
= imfilter(i, g2);
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));
a_new = zeros(size(a));
N = sum(b_occurrences);
b_pdf = zeros(size(graylevels, 1));
end
b_cdf = zeros(size(graylevels, 1));
b_new = zeros(size(b));
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');
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');
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);
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');
Exponential
img =
imread( a =
0.05; k = 1 /
a;
R = k * log(1 - rand(row, col));
expo = double(img) + R; expo =
uint8(expo);
Rayliegh
img =
imread( a =
0; b = 25;
noise = a + (-b * log(1 - rand([row, col])));
rayel = double(img) + noise; rayel
= uint8(rayel);