DIP - LabManual (2018-19) Modified (1) - Removed
DIP - LabManual (2018-19) Modified (1) - Removed
DIP - LabManual (2018-19) Modified (1) - Removed
EXPERIMENT NO. 1
Negative of an Image
% Image Negative
clear all;
i=imread ('cameraman.tif');
[m,n]=size(i);
o=zeros(m,n);
subplot(2,1,1), imshow(i);
title('original Image');
i=double(i);
o=255-i;
o=uint8(o);
subplot(2,1,2),imshow(o);
title('Negative Image')
13
DIGITAL IMAGE PROCESSING
clear all;
clc;
p=imread('cameraman.tif');
z=double(p);
[row,col]=size(z);
for i=1:1:row
for j=1:1:col
if((z(i,j)>100)&&(z(i,j)<250))
z(i,j)=255;
else
z(i,j)=z(i,j);
end
end
end
14
DIGITAL IMAGE PROCESSING
end
end
Subplot(2,1,1),imshow(p);
15
DIGITAL IMAGE PROCESSING
title('orignal image');
Subplot(2,1,2),imshow(uint8(z));
title('Gray level slicing without background')
16
DIGITAL IMAGE PROCESSING
Log Transform
% Logarithmic Transformation
clear all;
clc;
a=imread ('crow.jpg');
L=255;
c=L/log10(1+L);
d=c*log10(1+double(a));
subplot(1,2,1);
17
DIGITAL IMAGE PROCESSING
Zooming Operation
19
DIGITAL IMAGE PROCESSING
k=k+f;
end
figure,subplot(121),imshow(A),title('Original Image');
subplot(122),imshow(B),title('Shrinked Version');
% Zooming
for (i=1:f1:s2)
for (j=2:f1:s2-1)
C(i,j)= [C(i,j-1)+ C(i, j+1)]*0.5;
end
end
for(j=1:f1:s2)
for(i=2:f1:s2-1)
C(i,j)=[C(i-1,j)+C(i+1,j)]*0.5;
end
end
for (i=2:f1:s2-1)
for (j=2:f1:s2-1)
C(i,j)= [C(i,j-1)+ C(i, j+1)]*0.5;
end
end
figure,subplot(121),imshow(A),title('Original Image');
subplot(122),imshow(C),title('Zoomed Version');
%Output
Input Image ==> cameraman.tif
Enter the shrinking factor of the image: 2
Enter the factor by which the image is to be Zoomed: 2
20
DIGITAL IMAGE PROCESSING
21
DIGITAL IMAGE PROCESSING
EXPERIMENT NO. 2
Histogram Equalization
%Histogram equilization
clear all;
clc;
i=imread('kids.tif');
[m,n]=size(i);
subplot(2,2,1),imshow(i);
title('input image');
h=imhist(i);
subplot (2,2,2),stem(h);
axis([0 255 0 2000]);
xlabel('graylevel');
ylabel('number of pixels');
title('histogram');
g=histeq(i);
subplot(2,2,3),imshow(g);
title('histogram');
h=imhist(g);
subplot(2,2,4),stem(h);
axis([0 255 0 2000]);
xlabel('graylevel');
ylabel('number of pixels');
title('output image');
22
DIGITAL IMAGE PROCESSING
EXPERIMENT NO. 3
% AVERAGING FILTER
clc;
clear all;
close all;
a=imread('cameraman.tif');
b=imnoise(a,'salt & pepper');
h1=1/9*ones(3,3);
h2=1/25*ones(5,5);
c1=conv2(b,h1,'same');
c2=conv2(b,h2,'same');
c3=medfilt2(b,[3,3]);
c4=medfilt2(b,[5,5]);
subplot(2,2,1);imshow(a);title('original image');
subplot(2,2,2);imshow(b);title('salt and pepper image');
subplot(2,2,3);imshow(c3);title('3X3');
subplot(2,2,4);imshow(c4);title('5X5');
24
DIGITAL IMAGE PROCESSING
%SHARPENING FILTER
clc;
clear all;
close all;
a=imread('cameraman.tif');
%a=double(z);
h1=[-1 -1 -1;
-1 8 -1;
-1 -1 -1];
b=conv2(a,h1,'same');
c=double(a)+ double(b);
subplot(1,3,1);imshow(a);title('original image');
subplot(1,3,2);imshow(b);title('o/p of HP mask');
subplot(1,3,3);imshow(uint8(c));title('sharpened image');
25
DIGITAL IMAGE PROCESSING
EXPERIMENT NO.4
% Edge Detection
clear all;
close all;
a=imread('cameraman.tif');
Robert=edge(a,'Roberts');
prewitt=edge(a,'Prewitt');
sobel=edge(a,'Sobel');
log=edge(a,'log');
subplot(3,2,1:2),imshow(a),title('Original Image');
subplot(3,2,3),imshow(Robert),title('Roberts');
subplot(3,2,4),imshow(prewitt),title('Prewitt');
subplot(3,2,5),imshow(sobel),title('Sobel');
subplot(3,2,6),imshow(log),title('Laplacian of Gaussian');
27
DIGITAL IMAGE PROCESSING
EXPERIMENT NO. 5
clc;
clear all;
close all;
a=imread('cameraman.tif');
b=imresize(a,[1024 1024]);
[m,n]=size(b);
[counts,x]=imhist(b);
for i=1:256
proba(i)=counts(i)/(m*n);
end
code=huffman(proba);
% HUFFMAN FUNCTION
global CODE
CODE=cell(length(p),1);
if length(p)>1
p=p/sum(p);
s=reduce(p);
makecode(s,[]);
else
CODE={'1'};
end;
function s = reduce(p)
s=cell(length(p),1);
for i=1:length(p)
s{i}=i;
end
while numel(s)>2
[p,i]=sort(p);
p(2)=p(1)+p(2);
p(1)=[];
s=s(i);
s{2}={s{1},s{2}};
s(1)=[];
end
29
DIGITAL IMAGE PROCESSING
function makecode(sc,codeword)
global CODE
if isa(sc,'cell')
makecode(sc{1},[codeword 0]);
makecode(sc{2},[codeword 1]);
else
CODE{sc}=char('0'+codeword);
end
30
DIGITAL IMAGE PROCESSING
Experience: - 6:
% MORPHOLOGY OPERATORS %
% EROSION & DILATION
A=imread('C:\Users\sshah\Downloads\morph1.bmp');
B=imread('C:\Users\sshah\Downloads\morph2.bmp');
se0=[1 1 1;1 1 1;1 1 1];
dila=imdilate(A,se0);
ero=imerode(A,se0);
figure(1); subplot(1,3,1); imshow(A); title('Original Image');
subplot(1,3,2); imshow(dila); title('Dilated Image');
subplot(1,3,3); imshow(ero); title('Eroded Image');
A=imread('C:\Users\sshah\Downloads\morph1.bmp');
B=imread('C:\Users\sshah\Downloads\morph2.bmp');
se0=[1 1 1;1 1 1;1 1 1];
open =imopen(B,se0);
clo=imclose(B,se0);
figure(1); subplot(1,3,1); imshow(B); title('Original Image');
subplot(1,3,2); imshow(open); title('Open Image');
subplot(1,3,3); imshow(clo); title('Close Image');
% BOUNDARY DETECTION
A=imread('C:\Users\sshah\Downloads\morph1.bmp');
B=imread('C:\Users\sshah\Downloads\morph2.bmp');
se0=[1 1 1;1 1 1;1 1 1];
ero =imerode(A,se0);
boundary=A-ero;
figure(1); subplot(1,3,1); imshow(A); title('Original Image');
35
DIGITAL IMAGE PROCESSING
A=imread('C:\Users\sshah\Downloads\morph1.bmp');
B=imread('C:\Users\sshah\Downloads\morph2.bmp');
se0=[1 0 0;1 0 0;1 0 0];
se1=[0 1 1;0 1 1;0 1 1];
bc=imcomplement(B);
ero0=imerode(B,se0);
ero1=imerode(bc,se1);
hom=bitand(ero0,ero1);
figure(1); subplot(1,3,1); imshow(B); title('Original Image');
subplot(1,3,2); imshow(bc); title('Complement Image');
subplot(1,3,3); imshow(hom); title('Hit Or Miss Image');
36
DIGITAL IMAGE PROCESSING
EXPERIMENT NO. 7
% Image Segmentation %
I= imread('cell.tif');
subplot(3,2,1); imshow(I), title('original image');
[~, threshold] = edge(I, 'sobel');
fudgeFactor = .5;
BWs = edge(I,'sobel', threshold * fudgeFactor);
subplot(3,2,2); imshow(BWs), title('binary gradient mask');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
subplot(3,2,3); imshow(BWsdil), title('dilated gradient mask');
BWdfill = imfill(BWsdil, 'holes');
subplot(3,2,4);imshow(BWdfill);
title('binary image with filled holes');
BWnobord = imclearborder(BWdfill, 4);
subplot(3,2,5);imshow(BWnobord), title('cleared border image');
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
subplot(3,2,6);imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = I;
Segout(BWoutline) = 255;
figure, imshow(Segout), title('outlined original image');
37
DIGITAL IMAGE PROCESSING
EXPERIMENT NO. 8
% DIGITAL WATERMARKING %
A=imread('cameraman.tif');
B=imresize(rgb2gray(imread('C:\Users\sshah\Downloads\SomaiyaLogo.jpg')),[256 256]);
for i=1:256
for j=1:256
A(i,j)=bitand(A(i,j),223);
temp= bitand(B(i,j),128);
temp=temp/8;
c(i,j)=bitor(A(i,j),temp);
end
end
subplot(1,3,1); imshow(A);title('Original Image');
subplot(1,3,2); imshow(B);title('Mark Image');
subplot(1,3,3); imshow(c);title('WaterMarked Image');
39