flipkart ads

MATLAB TUTORIAL ONE

Exploring the following MATLAB Commands* used to play with the images:
  • imread
  • imwrite
  • rgb2gray
  • imshow
  • imresize
  • imrotate
  • imhist
  • histeq
  • improfile
  • grayslice
  • phantom
  • colormap
  • imcomplement
  • imadd
  • imdivide
  • immultiply
  • imsubtract
  • im2bw
  • im2double
  • mat2gray
  • imtransform
  • imadjust
  • maketform
  • entropy
  • rangefilt
  • stdfilt
  • corr2
  • conv2
  • impixel
  • mean2
  • std2
  • imnoise
  • fspecial
  • imfilter
  • freqz2
  • dct2
  • idct2
  • fft2
  • ifft2  


  • Apart from the above useful commands, explore the Image Processing Toolbox from the MATLAB Help and go through the Examples and Demos for understanding the capabilities of the Image Processing Toolbox.
So to learn basic commands associated with image processing in MATLAB try these simple basic programs to see the effect on images by applying them.
Open your MATLAB then open editor and try the program below save it and run the program, add an image to your Matlab directory by simply drag and drop method into Matlab command window.


close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
size(a)
imwrite(a,'Arjun.jpg','jpg');    % imwrite function
subplot(2,2,1)
imshow(a)                       % imshow function
title('imread  stores image values in  a ')
b = imresize(a,[64,64]);        % imresize function
display('size of resized image to a new scale, stored in b')
size(b)
subplot(2,2,2)
imshow(b)                       % imshow function
title('imresize  resizes image to a new scale values, stored in  b ')
c = imrotate(a,30);              % imrotate function
display('size of rotated image at some angle, stored in c')
size(c)
subplot(2,2,3)
imshow(c)
title('imrotate  rotates image at given angle, stored in  c ')
d = rgb2gray(a);                % rgb2gray function
size(d)
display('size of grayscale image converted from original image, stored in d')
subplot(2,2,4)
imshow(d)

title('rgb2gray  converts rgb image in grayscale image, stored in  d ')






close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
size(a)
d = rgb2gray(a);                % rgb2gray function
display('size of grayscale image converted from original image, stored in d')
size(d)
subplot(2,2,1)
imshow(d)
title('rgb2gray  converts rgb image in grayscale image, stored in  d ')
subplot(2,2,3)
imhist(d);
title('imhist  displays a histogram for the grayscale image ')
xlabel('number of gray levels')
ylabel('probability of occurance')
f = histeq(d);
subplot(2,2,2)
imshow(f)
title('histeq displays an equalization of a histogram for the grayscale image ')
subplot(2,2,4)
imhist(f);
xlabel('number of gray levels')
ylabel('probability of occurance')

title('imhist  displays a histogram for the grayscale image ')





close all;
clear all;
clc;
a = imread('new.png');          % imread function
d = rgb2gray(a); 
display('size of original image, stored in a')
size(a)
subplot(2,2,1)
imshow(a)
title('rgb2gray  converts rgb image in grayscale image, stored in  d ')   
x = [19 427 416 77];
y = [96 462 37 33];
subplot(2,2,3)
improfile(a,x,y),grid on;
title('improfile computes the intensity values along a line or a multiline path in an image')
subplot(2,2,2)
grayslice(a,64)
title('grayslice thresholds the intensity image')
p = phantom('Modified Shepp-Logan',250);
subplot(2,2,4)
imshow(p)
title('phantom function')




close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
size(a)
subplot(2,2,1)
load flujet
image(a)
colormap(jet)
h = imcomplement(a);
title('colormap function')
subplot(2,2,2)
imshow(h)
title('imcomplement  computes complement of an image, stored in  h ')
i = imadd(a,200);
subplot(2,2,3)
imshow(i)
title('imadd  add the scalar value to the image, stored in  i ')
i = imdivide(a,0.2);
subplot(2,2,4)
imshow(i)
title('imdivide divides the image values by given , stored in  h ')





close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
size(a)
j = immultiply(a,20);
subplot(2,2,1)
imshow(j)
title('immultiply  multiplies the given scalar to the image values, stored in j')
k = imsubtract(a,60);
subplot(2,2,2)
imshow(k)
title('imsubtract  substract the given scalar from the image values, stored in k')
l = im2bw(a,0.2);
subplot(2,2,3)
imshow(l)
title('im2bw converts the grayscale image to a binary image, stored in l')
m = im2double(a);
subplot(2,2,4)
imshow(m)
title('im2bw converts the intensity image to double precision, stored in m')







close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
size(a)
m = mat2gray(a,[0.8 0.1]);
subplot(2,2,1)
imshow(m)
title('mat2gray  converts the matrix of image to the intensity image, stored in m')
tform = maketform('a',[1 0 0; .5 1 0; 0 0 1]);
n = imtransform(a,tform);
subplot(2,2,2)
imshow(n)
title('imtransform  ransforms the image A according to the 2-D spatial transformation, stored in n')
o = imadjust(a,[0.1;0.9],[0.2;0.9]);
subplot(2,2,3)
imshow(o)
title('Adjust image intensity values or colormap , stored in o')
T = maketform('affine',[.5 0 0; .5 2 0; 0 0 1]);
tformfwd([10 20],T)
p = imtransform(a,T);
subplot(2,2,4)
imshow(p)
title('Creates spatial transformation structure (TFORM) , stored in p')






close all;
clear all;
clc;
a = imread('Audi A9_214.jpg');
q = entropy(a)
r = rangefilt(a)
A = rand(3)
B = rand(4)
D = rand(3)
display('convolution of A and B')
C = conv2(A,B)
display('correlation of A and D')
c = corr2(A,D)
b = stdfilt(a);
display('displaying value of pixel which was clicked on an output image')
d = impixel(a)
display('computes mean value for image a')
m = mean2(a)
display('computes standard variance for image a')
n = std2(a)
o = imnoise(a)
size(a)
subplot(2,2,1)
imshow(a)
title('original image')
subplot(2,2,2)
imshow(r)
title('image after using function  rangefilt')
subplot(2,2,3)
imshow(b)
title('image after using function  stdfilt')
subplot(2,2,4)
imshow(o)
title('image after using function  imnoise')
Output –
A =
    0.3773    0.1774    0.3131
    0.7743    0.3386    0.2156
    0.0057    0.5241    0.2778
B =
    0.0954    0.9668    0.9908    0.3074
    0.9177    0.9252    0.7247    0.7215
    0.2934    0.8329    0.4984    0.9551
    0.0127    0.3989    0.5377    0.2296
D =
    0.1657    0.6841    0.9552
    0.9906    0.3217    0.2197
    0.5798    0.5987    0.1407
convolution of A and B
C =
    0.0360    0.3817    0.5752    0.5945    0.3648    0.0963
    0.4202    1.2928    1.8401    1.4725    0.6726    0.2922
    0.8218    1.4490    2.0389    2.5028    1.1624    0.5400
    0.2372    1.3833    1.7528    2.0358    1.2195    0.4783
    0.0115    0.4717    1.0751    0.9440    0.8327    0.3148
    0.0001    0.0090    0.2157    0.3939    0.2697    0.0638
correlation of A and D                   computes mean value for image a           computes standard variance for image a
c = 0.3665                                            m =  130.5352                                                     n =   91.3458

ans = 800   960     3







close all;
clear all;
clc;
a = imread('Avengers_230.jpg');          % imread function
display('size of original image, stored in a')
size(a)

subplot(2,2,1)
imshow(a)
title('original image')

b = fspecial('motion',20,45);
c = imfilter(a,b,'replicate');
subplot(2,2,2);
imshow(c);title('Motion Blurred Image');

d = fspecial('disk',10);
e = imfilter(a,d,'replicate');
subplot(2,2,3);
imshow(e); title('Blurred Image');

f = fspecial('unsharp');
g = imfilter(a,f,'replicate');
subplot(2,2,4);
imshow(g); title('Sharpened Image');






close all;
clear all;
clc;
a = imread('new.png');          % imread function
display('size of original image, stored in a')
d = rgb2gray(a);
size(a)
b = dct2(d);
subplot(2,2,1)
imshow(b)
title('dct of given image using function dct2')
b(abs(b) < 10) = 0;
K = idct2(b);
subplot(2,2,2)
imshow(K,[0 255])
title('reconstruct the image using the inverse DCT function idct2')
f = fft2(a);
subplot(2,2,3)
imshow(f)
title('fft of given image using function fft2')
g = ifft2(f);
subplot(2,2,4)
imshow(g)
title('inverse fft of given image using function ifft2')


No comments:

Post a Comment

flipkart products