Introduction To MATLAB
Introduction To MATLAB
www.tripleninfotech.com
MATLAB Overview
What is MATLAB?
History of MATLAB
Strengths of MATLAB
Weaknesses of MATLAB
www.tripleninfotech.com
What is MATLAB?
problem-solving environment
Designed
for
computations, esp.
convenient
numerical
matrix manipulation,
MATLAB
MATLAB is a program for doing numerical computation.
It also contains
What is MATLAB?
Interpreted
Customized graphical-user-interface
building
WHY MATLAB
Ease of use
Rapid prototyping/debugging of
sophisticated code
www.tripleninfotech.com
HISTROY OF MATLAB
CONTI
The Mathworks, Inc. was created in 1984
The Mathworks is now responsible for
www.tripleninfotech.com
STRENGTH OF MATLAB
MATLAB is relatively easy to learn
MATLAB code is optimized to be relatively quick
programming language
MATLAB is interpreted, errors are easier to fix
Although primarily procedural, MATLAB does have
WEAKNESS OF MATLAB
MATLAB is NOT a general purpose programming
language
MATLAB is an interpreted language (making it for
MATLAB GUI
Launch Pad / Toolbox
Workspace
Current Directory
Command History
Command Window
www.tripleninfotech.com
WORKSPACE
Allows access to data
Area of memory managed through the
Command Window
Shows Name, Size (in elements),
www.tripleninfotech.com
help/demos
Toolbox is for use with specialized
www.tripleninfotech.com
CURRENT DIRECTORY
MATLAB, like Windows or UNIX, has a
current directory
any directory
COMMAND HISTROY
Allows access to the commands used
COMMAND WINDOW
Probably the most important part of the
GUI
Allows you to input the commands that
SIMPLE COMMANDS
who
whos
save
clear
load
www.tripleninfotech.com
WHO
who lists the variables currently in the
workspace.
As we learn more about the data
www.tripleninfotech.com
WHOS
whos is similar to who, but also gives size and
storage information
s = whos(...) returns a structure with these
SAVE
save saves workspace variables on disk
save filename stores all workspace variables
specified
workspace
variables
in
filename.mat. Use the * wildcard to save
only those variables that match the specified
pattern.
www.tripleninfotech.com
CLEAR
clear removes items from workspace,
name
clear name1 name2 name3 ...
www.tripleninfotech.com
LOAD
load - loads workspace variables from
disk
Examples of Syntax:
load
load
filename
load filename X Y Z
www.tripleninfotech.com
MATLAB FUNCTION
Functions are similar to scripts
Functions may take arguments
Functions may return one or more values
www.tripleninfotech.com
CONTI
Example function
function [output] = square(input)
output = input*input;
Body of functions can contain code just like
scripts could
www.tripleninfotech.com
MATH FUNCTION
Elementary functions (sin, cos, sqrt, abs,
help elfun
gamma, erf)
help specfun
type help elmat
type
www.tripleninfotech.com
GRAPH FUNCTIONS
plot linear plot
stem discrete plot
grid add grid lines
xlabel add X-axis label
ylabel add Y-axis label
title add graph title
subplot divide figure window
figure create new figure window
pause wait for user response
www.tripleninfotech.com
GETTING HELP
Using the Help Browser (.html, .pdf)
View getstart.pdf, graphg.pdf, using_ml.pdf
Type
help
help function, e.g. help plot
Running demos
type demos
type help demos
www.tripleninfotech.com
ARITHMATIC OPERATIONS
1) plus
- Plus
+
2) uplus
- Unary plus
+
3) minus
- Minus
4) uminus
- Unary minus
5) mtimes
- Matrix multiply
*
6) times
- Array multiply
.*
7) mpower
- Matrix power
^
8) power
- Array power
.^
9) mldivide - Backslash or left matrix divide
10) mrdivide - Slash or right matrix divide
11) ldivide - Left array divide
.\
12) rdivide - Right array divide
./
www.tripleninfotech.com
\
/
RELATIONAL OPERATIONS
1) eq
- Equal
- Not equal
==
2)
ne
3)
lt
4)
gt
- Greater than
>
5)
le
<=
6)
ge
- Less than
www.tripleninfotech.com
- Greater
than or equal
~=
<
>=
LOGICAL OPERATORS
1)Short-circuit logical AND
&&
2)Short-circuit logical OR
||
3) and
- Element-wise logical AND
or
not
xor
any
nonzero
8) all
nonzero
4)
5)
6)
7)
- Element-wise logical OR
|
- Logical NOT
~
- Logical EXCLUSIVE OR
- True if any element of vector is
- True if all elements of vector are
www.tripleninfotech.com
&
BITWISE OPERATORS
1) bitand
2)
bitcmp
3)
bitor
4)
bitmax
5)
6)
7)
8)
- Bit-wise AND.
- Complement bits.
- Bit-wise OR.
- Maximum floating point
integer.
bitxor
- Bit-wise XOR.
bitset
- Set bit.
bitget
- Get bit.
bitshift - Bit-wise shift.
www.tripleninfotech.com
COMMAND SYNTAX
numbers.)
www.tripleninfotech.com
FILE I/0
fopen
fclose
fprintf
fgetl / fgets
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
graph
Clc
close all
clear all
t=0:0.25:7;
y = sin(t);
plot(t,y,'r--') ;
xlabel('Time');
ylabel('Frequency');
title('sin wave');
grid on;
gtext('X axis 1 cm = 1');
gtext('Y axis 1 cm = 0.2');
legend('contious sinwave');
print -djpeg075 sinwave.jpg
www.tripleninfotech.com
IF LOOP
a=5;
if a > 6
disp('a is greater');
elseif a==0
disp('a is zero');
else
disp('a is smaller');
end
www.tripleninfotech.com
FOR LOOP
a=5;
for i=1:5
a=a+1
end
disp(a);
ANS a =10
www.tripleninfotech.com
While Loop
a=5;
while a < 10
a=a+1;
end
disp(a);
Ans a =10
www.tripleninfotech.com
Function
function c=add(a,b);
c=a+b;
return
function c=mul(a,b);
c=a*b;
return
Main
a=5;
b=6;
c=add(a,b);
disp(c);
d=mul(a,b);
disp(d);
www.tripleninfotech.com
Sample Program
load noissin;
figure;
plot(noissin);
figure;
c=cwt(noissin,1:10:500,'db4','plot');
www.tripleninfotech.com
Signal generation
t=0:0.01:4;
x=sin(2*pi*t);
x=cos(2*pi*t);
figure;
plot(t,x);
axis([0 2 -1 1]);
xlabel('Time (sec)');
ylabel('amplitude (Volt)');
figure;
tem(t,x);
xis([0 5 -1 1]);
xlabel('Time (sec)');
ylabel('amplitude (Volt)');
www.tripleninfotech.com
Exponential sequence
n=input('enter the length of exponential sequence =');
t=0:n;
a=input('Enter a value =');
y=exp(a*t);
figure;
stem(t,y);
axis([0 4 0 10]);
xlabel('Time (sec)');
ylabel('Amplitude (volt)');
title('Exponential Signal');
www.tripleninfotech.com
Ramp Sequence
n=input('Enter the length of sequence=');
t=0:n;
figure;
stem(t,t);
xlabel('samples (n)-->');
ylabel('Amplitude (volt)');
title('Ramp Sequence');
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
Sawtooth waveform
t=0:.001:1;
x=sawtooth(2*pi*50*t);
plot(t,x);
axis([0 .2 -1 1]);
www.tripleninfotech.com
Square waveform
t=0:.01:10
x=square(t,15);
plot(t,x);
www.tripleninfotech.com
Triangular waveform
t=-3:.1:3
y=tripuls(t);
plot(t,y);
www.tripleninfotech.com
Rectangular waveform
t=-3:.1:3;
y=2*rectpuls(t);
plot(t,y);
axis([-3 3 0 3]);
www.tripleninfotech.com
a =imread('flowers.tif');
imshow(a);
pixval on;
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
I = imread('rice.png');
J = imadd(I,50);
subplot(1,2,1), imshow(I)
subplot(1,2,2), imshow(J)
www.tripleninfotech.com
www.tripleninfotech.com
clc;
clear;
close all
a= imread('flowers.tif');
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
b=imresize(a,[256 256]);
imshow(b);
subplot(2,2,3);
c=rgb2gray(b);
imshow(c);
subplot(2,2,4);
d=im2bw(c);
imshow(d);
www.tripleninfotech.com
RGB component
a=imread('flowers.tif');
subplot(2,2,1);
imshow(a);
[r c p]=size(a)
R=a;
G=a;
B=a;
R(:,:,2:3)=0;
subplot(2,2,2);
imshow(R);
G(:,:,1)=0;
G(:,:,3)=0;
subplot(2,2,3);
imshow(G);
B(:,:,1)=0;
B(:,:,2)=0;
subplot(2,2,4);
imshow(B);
www.tripleninfotech.com
a = imread('cameraman.tif');
[r c]=size(a);
Len=r*c;
b=reshape(a,[1 Len]);
www.tripleninfotech.com
file
frm_cnt=file.NumFrames
str2='.bmp'
h = waitbar(0,'Please wait...');
for i=1:frm_cnt
frm(i)=aviread(filename,i); % read the Video file
frm_name=frame2im(frm(i)); % Convert Frame to image file
frm_name=rgb2gray(frm_name);%convert gray
filename1=strcat(strcat(num2str(i)),str2);
imwrite(frm_name,filename1);
waitbar(i/frm_cnt,h)
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
Store an Image,Audio
a =imread('cameraman.tif');
imwrite(a,'new.bmp');
a=wavread('test.wav');
wavwrite(d,44100,16,'nTEST.WAV');
www.tripleninfotech.com
I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
subplot(1,2,1);imshow(J)
subplot(1,2,2);imshow(K)
www.tripleninfotech.com
terminal window
script M-file
function M-file
.mex files
www.tripleninfotech.com
Terminal window
www.tripleninfotech.com
M file window
Series
of
Matlab
comma
nds
Matlab
Command
Dat
m-files
mat-files
Line
a
Command
sto
execution
Input functions
rag
like
DOS
Output
e/
command
loa
capabil
window
www.tripleninfotech.com
din
ity
www.tripleninfotech.com
Conti
The syntax
definition is:
for
MATLAB
function
www.tripleninfotech.com
Conti
www.tripleninfotech.com
Conti..
www.tripleninfotech.com
Conti..
cd E:\ OR
chdir E:\.
www.tripleninfotech.com
IMAGE PROCESSING
www.tripleninfotech.com
What is an Image
www.tripleninfotech.com
Types of image
www.tripleninfotech.com
Image format
www.tripleninfotech.com
Grayscale Image
Each pixel is a
shade of gray,
normally from 0
(black) to 255
(white).
This range means
that each pixel
can
be
represented
by
eight
bits,
or
www.tripleninfotech.com
exactly one byte.
Conti
Rotate image
a=imread(cameraman.tif)
figure
imshow(a)
b=imrotate(a,45)
figure
imshow(b)
www.tripleninfotech.com
Resize image
a=imread(sunset.jpg)
figure
imshow(a)
b=imresize(a,10)
figure
imshow(b)
www.tripleninfotech.com
Cropping an image
a=imread(winter.jpg)
figure
imshow(a)
b=imcrop(a,[10 20 30 40])
figure
imshow(b)
www.tripleninfotech.com
Complement image
z=imread(desret.jpg)
figure
imshow(z)
g=imcomplement(z)
figure
imshow(g)
www.tripleninfotech.com
Histogram an image
a=imread(cameraman.tif)
figure
imhow(a)
j=histeq(a)
figure
imshow(j)
www.tripleninfotech.com
IMTOOL
Example
imtool(peppers.png);
www.tripleninfotech.com
Indexed Image
www.tripleninfotech.com
Example
rgb=imread(peppers.png);
hsv=rgb2hsv(rgb);
figure;
imshow(hsv);
Yc=rgb2ycbcr(rgb);
Yi=rgb2ntsc(rgb);
figure; imshow(Yc);
figure; imshow(Yi);
www.tripleninfotech.com
Image Enhancement
a=imread(pout.tif);
figure; subplot(2,1,1); imshow(a);
subplot(2,1,2); imhist(a);
b=imadjust(a);
figure; subplot(2,1,1); imshow(b);
subplot(2,1,2); imhist(b);
c=histeq(a);
figure; subplot(2,1,1); imshow(c);
subplot(2,1,2); imhist(c);
d=adapthisteq(a);
figure; subplot(2,1,1); imshow(d);
www.tripleninfotech.com
subplot(2,1,2); imhist(d);
OVERVIEW
REGION OF INTEREST
DEBLURRING
EDGE DETECTION
www.tripleninfotech.com
Region of Interest
a=imread(pout.tif);
Figure;imshow(b);
b=roipoly(a);
figure;
imshow(b);
[r c]=find(b==1);
xmin=min(r);xmax=max(r);
ymin=min(c);ymax=max(c);
D=a(xmin:xmax,ymin:ymax);
Figure;imshow(d);www.tripleninfotech.com
Roipoly Example
a=imread(coins.png);
figure;imshow(a);
b=roipoly(a);
loc=find(b==1); loc1=find(b ~= 1);
pri=zeros(r,c); pri(loc)=a(loc);
sec=zeros(r,c); sec(loc1)=a(loc1);
figure;
imshow(pri,[]);
figure;
imshow(sec,[]); www.tripleninfotech.com
Roicolor example
a=imread(rice.png);
Figure(1);
imshow(a);
b=roicolor(a,170,210);
Figure(2);
imshow(b);
www.tripleninfotech.com
Roifill Example
a=imread(pout.tif);
figure(1);
Imshow(a);
bw=roipoly(a);
c=roifill(a,bw);
figure(2);
Imshow(c);
www.tripleninfotech.com
Roifilter example
a=imread(pout.tif);
figure(1);
Imshow(a);
bw=roipoly(a);
F=fspecial(unsharp);
Out=roifilt2(F,a,bw);
figure(2);
Imshow(Out);
www.tripleninfotech.com
www.tripleninfotech.com
Deblurring
a=imread(peppers.png);
figure(1); imshow(a);
H = fspecial('motion',20,45)
Blurred =imfilter(a,H,circular);
figure(2);
imshow(Blurred);
title('Motion Blurred Image');
De=deconvwnr(Blurred,H);
figure(3);
www.tripleninfotech.com
Imshow(De);
Image analysis
a=imread(peppers.png);
figure(1); imshow(a);
P=impixel(a);
disp(P);
a=imread(coins.png);
Figure(1); imshow(a);
b=improfile;
Disp(b);
a=imread(rice.png);
imcontour(a,2);
title(Contour plot); www.tripleninfotech.com
IMAGE PROCESSING
TECHNOLGIES &
APPLICATIONS
www.tripleninfotech.com
Digital Image
Processing?...
Applications
Transmission & Storage
Secret Communications
Watermarking
Copyright Protection
Segmentation
Image Retrieval
Web Applications
Recognition
Fusion
De-noising
Authentication
Medical & Satellite
Digital Cameras & Video codec
www.tripleninfotech.com
Compression
Types of Compression
Lossless Compression
Data
Recovered
Lossy Compression
Data
Some
Applications
B
I
Input Image
Quantization
Entropy Encoding T
S
T
R
E
Reconstructed
De-quantization Decoding
A
Image
M
S
www.tripleninfotech.com
Steganography
Original Image
Stego Image
Application
Preproces
s
Original
Image
Embeddin
g in Image
Stegoimag
e
Secret
Data
Extraction
from
Stegoimage
Secret
Data
www.tripleninfotech.com
Watermarking
Applications
Input Image
Copyright Image
DWT
Key
Embedding Process
www.tripleninfotech.com
Watermarking Extraction
Copyright Image
IDWT
Key
Extraction Process
www.tripleninfotech.com
Segmentation
Applications
In
Image Retrieval
Metadata based IR
Shape based IR
Applications
Fusion
Applications
www.tripleninfotech.com
Simulation &
Implementation
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
Fusion
www.tripleninfotech.com
www.tripleninfotech.com
Searching Process
www.tripleninfotech.com
Input Video
www.tripleninfotech.com
Compressed Video
www.tripleninfotech.com
www.tripleninfotech.com
TRANSFORMATION
www.tripleninfotech.com
Conti..
w
lo
horizontal edges
www.tripleninfotech.com
fr
cy
n
ue
q
e
vertical edges
le c y
d
i d u en
m eq
fr
Conti
2-D-DCT
N 1 N 1
2
(2 x 1)u
(2 y 1)v
F (u , v) C (u )C (v) f ( x, y ) cos[
] cos[
]
N
2N
2N
y 0 x 0
2-D-IDCT
2
f ( x, y )
N
(2 x 1)u
(2 y 1)v
C (u )C (v) F (u, v) cos[
] cos[
]
2N
2N
v 0 u 0
N 1 N 1
www.tripleninfotech.com
Conti
www.tripleninfotech.com
Conti
a=imread('cameraman.ti
f');
subplot(1,3,1);
imshow(a,[]);
title(Input image)
b=dct2(a);
subplot(1,3,2);
imshow(b,[]);
title('DCT');
c=idct2(b);
subplot(1,3,3);
imshow(c,[]);
www.tripleninfotech.com
www.tripleninfotech.com
Conti
www.tripleninfotech.com
Even + odd
Even + odd
Even odd
Image
Even - odd
HL
Even + odd
LL
LH
Even odd
www.tripleninfotech.com
HH
Conti..
a=imread('cameraman.tif')
;
[LL LH HL
HH]=dwt2(a,'haar');
Dec=[...
LL,LH
HL,HH
...
];
imshow(Dec,[]);
www.tripleninfotech.com
GUI
www.tripleninfotech.com
DIALOG BOX
warndlg('hello');
helpdlg('hello');
msgbox('hello');
www.tripleninfotech.com
errordlg('hello');
switch ButtonName,
case 'Food',
disp('Food is delivered');
case 'Clothing',
disp('The Emperor''s new clothes have arrived.')
case 'Money',
disp('A ton of money falls out the sky.');
end % switch
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
GUI
www.tripleninfotech.com
% hObject
% handles
if isequal(filename,0) | isequal(pathname,0)
M1 = imread( filename);
handles.M1 = M1;
axes(handles.axes1);
imshow(M1);
handles.filename1=filename;
guidata(hObject, handles);
end
www.tripleninfotech.com
GUI
www.tripleninfotech.com
MENU BAR
www.tripleninfotech.com
PUSH BUTTON
www.tripleninfotech.com
TOGGLE BUTTON
www.tripleninfotech.com
RADIO BUTTON
www.tripleninfotech.com
CHECKBOX
www.tripleninfotech.com
EDIT TEXT
www.tripleninfotech.com
STATIC TEXT
www.tripleninfotech.com
SLIDER
www.tripleninfotech.com
FRAME
www.tripleninfotech.com
LISTBOX
www.tripleninfotech.com
POPUP MENU
www.tripleninfotech.com
AXES
www.tripleninfotech.com
ALIGN OBJECTS
www.tripleninfotech.com
MENU EDITOR
www.tripleninfotech.com
M FILE EDITOR
www.tripleninfotech.com
PROPERTY INSPECTOR
www.tripleninfotech.com
www.tripleninfotech.com
RUN
www.tripleninfotech.com
EMPTY GUI
www.tripleninfotech.com
GENERATED M FILE
www.tripleninfotech.com
PUSH BUTTON
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
GO FOR CALLBACK
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
CHOOSE AXES
www.tripleninfotech.com
CHOOSE AXES
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
CODE
a =imread('cameraman.tif');
axes(handles.one);
imshow(a);
www.tripleninfotech.com
TOGGLE BUTTON
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
a=get(hObject,'Value');
if a ==1
a =imread('cameraman.tif');
axes(handles.one);
imshow(a);
else
a =imread('greens.jpg');
axes(handles.one);
imshow(a);
end
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
CHOOSE POPUPMENU
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
a=get(hObject,'String') ;
b=char(a);
c=imread(b);
axes(handles.one);
imshow(c);
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
SLIDER
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
GO FOR CALLBACK
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
a=get(hObject,'Value');
filename =handles.filename;
I = imread(filename);
J = imadd(I,50);
axes(handles.axes2);
imshow(J)
www.tripleninfotech.com
MENU EDITOR
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com
RUN
www.tripleninfotech.com
Further contact
E-Mail:tripleninfotech@gmail.com
project@tripleninfotech.com
Contact No
+91-9566234284
+91-9566763284
Like Facebook page
https://www.facebook.com/tripleninfotechiee
eprojecs
www.tripleninfotech.com
Triple N Infotech-Trichy
No 33,(Old No 14/1),
First Floor,
Kamachi Complex,
Madley Road,
T.Nagar
Salai Road,
Chennai-17
Trichy-18
044-42868371
0431-4050403
9566234284
7200021404
www.tripleninfotech.com
www.tripleninfotech.com
www.tripleninfotech.com