DIP Matlab Program
DIP Matlab Program
DIP Matlab Program
1
Aim: Introduction to the MATLAB commands
• a=[1 2 3; 4 5 6; 7 8 9]
• b=[3 :6]
• t =1:10
• k =2:-0.5:-1
• x= [1:4; 5:8]
• y = [1 2 5 1]
Practical No. 3
Aim: Write a program to perform various matrices arithmetic operation
• a=[1 2 3; 4 5 6; 7 8 9]
• b=[3 4 5; 1 4 5; 3 5 6]
• sum= a+b;
• sub=a-b;
• mult= a*b;
Practical No. 4
Aim: Write a program to prints out Pascal’s triangle:
• a=1
• while length(a) < 10
• a = [0 a] + [a 0]
• end
Practical No. 5
Aim: Write a program Plot the function sin(x) between 0≤x≤4π using linspace
• x=linspace(0,4*pi,100);
• y=sin(x);
• plot(y);
Practical No. 6
Aim: Write a program Plot the function sin(x) between 0≤x≤4π using stem function
• x=linspace(0,4*pi,100);
• y=sin(x);
• stem(y);
• stem(x,y);
Practical No. 7
Aim: Write a program to import Digital Images and convert the class of image into gray, and binary, also adjust the
intensity levels
a = imread('peppers.png');
subplot(1,4,1);
imshow(a);
title(‘Original Imae’);
b = rgb2gray(a);
subplot(1,4,2);
imshow(b);
title(‘Gray Image’);
c = im2bw(a);
subplot(1,4,3);
imshow(c);
title(‘Binary Image’);
d = imadjust(b);
subplot(1,4,4);
imshow(d);
title(‘Intensity Adjust’);
Practical No. 8
Aim: Write a program to import Digital Images and reduce the size of the image
(50%)
• I = imread('peppers.png’);
• J = imresize(I, 0.5);
• Subplot(1,2,1);
• imshow(I);
• title('Original Image’);
• Subplot(1,2,2);
• imshow(J);
• title(‘Resize Image’);
• sz1=size(I);
• sz2=size(J);
8
Practical No. 9
AIM: Write a program to import Digital Images and
clear perform arithmetic operations
close all;
a=imread('cameraman.tif');
b=imread('rice.png');
c=a+b; % addition
subplot(1,3,1);
imshow(c);
title(‘ADD. of Image 1 & 2’);
d=a-b; % subtraction
subplot(1,3,2);
imshow(d);
title(‘SUB. of Image 1 & 2’);
e=a.*b; % multiplication
subplot(1,3,3);
imshow(e);
title(‘Mult. of Image 1 & 2’);
9
Practical No. 10
AIM: Write a program to import Digital Images and
perform logical AND operations
• a= imread('cameraman.tif’);
• b= imread 'rice.png’);
• a1=im2bw(a);
• Subplot(1,3,1);
• imshow(a1);
• title(‘Binary Image a’);
• b1=im2bw(b);
• Subplot(1,3,2);
• imshow(b1);
• title(‘Binary Image b’);
• C=and(a1,b1);
• Subplot(1,3,3);
• imshow(c);
Practical No. 11
AIM: To import Digital Images and perform logical
OR operations
• a= imread('cameraman.tif’);
• b= imread(‘'rice.png’);
• a1=im2bw(a);
• Subplot(1,3,1);
• imshow(a1);
• title(‘Binary Image a’);
• b1=im2bw(b);
• Subplot(1,4,2);
• imshow(b1);
• title(‘Binary Image b’);
• C=or(a1,b1);
• Subplot(1,3,2);
• imshow(c);
• title(‘OR Image‘);
Practical No. 12
AIM: To import Digital Images and perform logical
NOT operations
• a= imread('cameraman.tif’);
• a1=im2bw(a);
• subplot(1,2,1);
• imshow(a1);
• title(‘Binary Image a’);
• C=not(a1);
• subplot(1,2,2);
• imshow(C);
• title(‘NOT Image‘);
Practical No. 13
AIM: To Select a Digital Image and perform Basic
Intensity Transformation “Image Negative”
I1=imread(‘tree.jpg’);
Subplot(1,2,1);
Imshow(i1);
I2=i1;
For r =1:256
For c =1:256
I2(r,c)=255-i2(r,c);
End
End
Subplot(1,2,2);
Imshow(i2);
Practical No. 14
AIM: To Select a Digital Image and perform Basic
Intensity Transformation “Logarithmic Transformation”
img = imread('Sampleimage.jpg');
r = double(img);
C = 1;
S = C * log(1 + r);
T = 255/(C * log(256));
B = uint8(T * S);
imshow(B);
title('Log Transformation');
Practical No. 15
AIM: To Select a Digital Image and perform Basic
Intensity Transformation “Power Law Transformation”
img = imread('Sampleimage.jpg’);
subplot(1,2,1);
imshow(img);
r = double(img);
G = 0.6;
S = C * (r .^G);
T = 255/(C * (255 .^G));
O = uint8(T * S);
subplot(1,2,2);
imshow(O);
title('Power Law Transformation');
Practical No. 16
Aim: To Select a Digital Image and Plot a Histogram
• I = imread("pout.tif");
• subplot(1,3,1);
• imshow(I);
• subplot(1,3,2:3);
• imhist(I)
Practical No. 17
Aim: To Select a Digital Image, perform Histogram
Equalization and plot the histogram.
• I = imread(“abc.tif");
• subplot(1,3,1)
• imshow(I)
• subplot(1,3,2:3)
• imhist(I)
• J = histeq(I);
• subplot(1,3,1)
• imshow(J)
• subplot(1,3,2:3)
• imhist(J)
Practical No. 18
Aim: To import a Digital Image and apply an image averaging
filter.
• I = imread('cameraman.tif’);
• subplot (1, 2, 1);
• imshow(I);
• title('Original Image');
• C=fspecial('average’,[3,3]);
• d=imfilter(I,C);
• subplot (1, 2, 2);
• imshow(d);
• title(‘3x3 average filter’);
Practical No. 19
Aim: To import a Digital Image and apply a median
filter.
• I = imread('cameraman.tif’);
• subplot (1, 3, 1);
• imshow(I);
• title('Original Image');
• J = imnoise(I,'salt & pepper',0.02);
• subplot (1, 3, 2);
• imshow(J);
• title(‘Noise Image');
• K = medfilt2(J);
• subplot (1, 3, 3);
• imshow(K);
• title('filtered image');
Practical No. 20
Aim: To import a Digital Image and apply a min and
• Original=imread('cameraman.tif’);
• BW = im2bw(Original,0.6);
max filter.
• minf=@(x) min(x(:));
• maxf=@(x)max(x(:));
• min_Image=nlfilter(BW,[3 3],minf);
• max_Image=nlfilter(BW,[3 3],maxf);
• subplot(2,2,1);
• imshow(BW);
• title('Original');
• subplot(2,2,2);
• imshow(min_Image);
• title('Min');
• subplot(2,2,3);
• imshow(max_Image);
• title('Max');
Practical No. 21
Aim: To import a Digital Image and apply a
Laplacian filter.
• A = imread('peppers.png');
• sigma = 0.4;
• alpha = 0.5;
• B = locallapfilt(A, sigma, alpha);
• imshowpair(A, B, 'montage')
Practical No. 22
Aim: To import a Digital Image and apply a FFT.
• I=imread('cameraman.tif');
• F=fft2(double(I));
• S=fftshift(F);
• L=log2(S);
• A=abs(L);
• imagesc(A)
Practical No. 23
Aim: To import a Digital Image and apply a frequency
domain filter.
• I = imread("cameraman.tif");
• I = im2double(I);
• F = fspecial("average",3);
• Ipad = padarray(I,[3-1 3-1],0,"post");
• Fpad = padarray(F,[256-1 256-1],0,"post");
• Ifft = fft2(Ipad);
• Ffft = fft2(Fpad);
• Offt = Ifft.*Ffft;
• Opad = ifft2(Offt);
• O = Opad(2:end-1,2:end-1);
Practical No. 24
Aim: To import a Digital Image and Perform various Morphological operation erosion
• I=imread(‘cameraman.tif');
• I=im2bw(I);
• se=ones(5, 5);
• [P, Q]=size(se);
• In=zeros(size(I, 1), size(I, 2));
• for i=ceil(P/2):size(I, 1)-floor(P/2)
• for j=ceil(Q/2):size(I, 2)-floor(Q/2)
• on=I(i-floor(P/2):i+floor(P/2), j-floor(Q/2):j+floor(Q/2));
• nh=on(logical(se));
• In(i, j)=min(nh(:));
• end
• end
• imshow(In);
Practical No. 25
Aim: To import a Digital Image and Perform various Morphological operation dilation
• I=imread(‘cameraman.tif');
• I=im2bw(I);
• se=ones(5, 5);
• [P, Q]=size(se);
• In=zeros(size(I, 1), size(I, 2));
• for i=ceil(P/2):size(I, 1)-floor(P/2)
• for j=ceil(Q/2):size(I, 2)-floor(Q/2)
• on=I(i-floor(P/2):i+floor(P/2), j-floor(Q/2):j+floor(Q/2));
• nh=on(logical(se));
• In(i, j)=max(nh(:));
• end
• end
• imshow(In);