You are on page 1of 61

------------------------------------------------------

------------------------------------------------------------

-------------------------------------------------------

-------------------------------------

-------------------------------------------

----------------------------------------------

------------------------------------------------------

-----------------------------------------------------

------------------------------------------------------

---------------------------------------------------

---------------------------------------------------

------------------------------

--------------------------------------------

---------------------------------------

-------------------------------------------------

--------------------------------------------------

---------------------------------------------

-----------------------------------

------------------------------------
-----------------------------------

--------------------------------------

----------------------------------

- --------------------------------

-------------------------------------

--------------------------------------------

---------------------------------------

--------------------------------------------------

-----------------------------------------------------

------------------------------------------------------

---------------------------------------

---------------------------------------------

---------------------------------------------------------

-------------------------------------------------

-----------------------------

-----------------------------------

-------------------------------------------------

----------------------------------------------------------

----------------------------------------------------------

------------------------------------------

----------------------------------------------------------
------------------------------------------------

------------------------------------------------

---------------------------------------------------

--------------------------------------------

-----------------------------------------------------

---------------------------------------------

-----------------------------------------

---------------------------------------------

------------------------------

------------------------------------------

---------------------------------------------

-----------------------------------

.........................................................................................
..........................................................................................
......................................................................................
.........................................................................................
..........................................................................................
.................................................................................
........................................................................................
..................................................................................................
..........................................................................................
....................................................................................
...................................................................................
.....................................................................................
........................................................................................
.........................................................................................
........................................................................................
.........................................................................................
.........................................................................................
........................................................................................
...................................................................................
.................................................................................
........................................................................................
...........................................................

---------------------------------------------

------------------------------

-----------------------------------------

-------------------------------------------------- : if

-------------------------------------------------

--------------------------------------------- for

-------------------------- enter

-------------------------------- 10s

------------------------------------------------- while

----------------------------------------------------- fprintf

----------------------------------------------- menu
------------------------------------------------------ switch

--------------------------------------------------------

...............................................................................................
.............................................................................................

----------------------------------------------------

- -------------------------------- R

---------------------------- image show

-------------------

------- im2bw()

------------- mat2gray()

------------------ imread(),imwrite()

---------------------------------------------------

............................................................. imresize()
........................................................................
............................................. imrotate()
.............................................. imcrop

------------------------

....................................... 3x3

-----------------------------------------------------

............................ conv2()

............................................................................................... imfilter()
......................... fspecial()
..... ftrans2()
....................................................................................
................................................ FIR

-----------------------------------

-------------------------------------------------------

------------------------------------------

---------------------------------------------

-------------------------------------------------

--------------------------------------- object detection by white filter

-----------------------------------------------------
mfile

Help

salehijun@gmail.com
Learning MATLAB

a =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

a(4:-1:1,:)

ans =
16 17 18 19 20
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5

a(:,5:-1:1)

ans =
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
20 19 18 17 16

f=[1,2,3,...
4,5,6]

f =
1 2 3 4 5 6

size(a)

ans =
4 5

a =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

[L,C]=size(a)

L =
4
C =
5

eye(2)

ans =
1 0
0 1

eye(3)

ans =
1 0 0
0 1 0
0 0 1

eye(4)

ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

eye(2,3)

ans =
1 0 0
0 1 0

eye(4,3)

ans =
1 0 0
0 1 0
0 0 1
0 0 0

zeros(3)

ans =
0 0 0
0 0 0
0 0 0

zeros(2,3)

ans =
0 0 0
0 0 0

ones(3)

ans =
1 1 1
1 1 1
1 1 1

ones(2,3)
ans =
1 1 1
1 1 1

rand(3)

ans =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214

rand(2,3)

ans =
0.4447 0.7919 0.7382
0.6154 0.9218 0.1763

magic(3)

ans =
8 1 6
3 5 7
4 9 2

magic(4)*ones(4)

ans =
34 34 34 34
34 34 34 34
34 34 34 34
34 34 34 34

inv(a)

??? Error using ==> inv


Matrix must be square.

e=[1 2 3
4 5 6
7 8 9]

e =
1 2 3
4 5 6
7 8 9

inv(e)

ans =
1.0e+016 *
-0.4504 0.9007 -0.4504
0.9007 -1.8014 0.9007
-0.4504 0.9007 -0.4504

e^(-1)

ans =
1.0e+016 *
-0.4504 0.9007 -0.4504
0.9007 -1.8014 0.9007
-0.4504 0.9007 -0.4504

det(e)

ans =
0

e'

ans =
1 4 7
2 5 8
3 6 9

e =
1 2 3
4 5 6
7 8 9

f=[1 1+2*i
2 2+3*i]

f =
1.0000 1.0000 + 2.0000i
2.0000 2.0000 + 3.0000i

f'

ans =
1.0000 2.0000
1.0000 - 2.0000i 2.0000 - 3.0000i

f.'

ans =
1.0000 2.0000
1.0000 + 2.0000i 2.0000 + 3.0000i

trace(c)

ans =
40

c =
1 2 3 4
6 7 8 9
11 12 13 14
16 17 18 19

tril(c)

ans =
1 0 0 0
6 7 0 0
11 12 13 0
16 17 18 19

c =
1 2 3 4
6 7 8 9
11 12 13 14
16 17 18 19

triu(c)

ans =
1 2 3 4
0 7 8 9
0 0 13 14
0 0 0 19

diag(c)

ans =
1
7
13
19

diag(ans)
ans =
1 0 0 0
0 7 0 0
0 0 13 0
0 0 0 19

help inf
INF Infinity.
INF returns the IEEE arithmetic representation for positive
infinity. Infinity is also produced by operations like
dividing by
zero, eg. 1.0/0.0, or from overflow, eg. exp(1000).
INF('double') is the same as INF with no inputs.
INF('single') is the single precision representation of
INF.
INF(N) is an N-by-N matrix of INFs.
INF(M,N) or INF([M,N]) is an M-by-N matrix of INFs.
INF(M,N,P,...) or INF([M,N,P,...]) is an M-by-N-by-P-by-...
array of INFs.
INF(...,CLASSNAME) is an array of INFs of class specified
by CLASSNAME.
CLASSNAME must be either 'single' or 'double'.
See also Na<a href="matlab:help n">n</a>, <a
href="matlab:help isinf">isinf</a>, <a
href="matlab:help isfinite">isfinite</a>, <a
href="matlab:help isfloat">isfloat</a>.
Reference page in Help browser
<a href="matlab:doc inf">doc inf</a>

format short

1/3

ans =
0.3333
format long

1/3

ans =
0.33333333333333

format bank

1/3

ans =
0.33

format

1/3

ans =
0.3333

format hex

pi

ans =
400921fb54442d18

format rat

pi
ans =
355/113
format +

22

ans =
+

-22

ans =
-

ans =

triu(c)*tril(c)

diary

abs(-1.2)

ans =
1.2000

a=[-1.2 2]

a =
-1.2000 2.0000

abs(a)

ans =
1.2000 2.0000
b=[-1.2 1.2-2*i]

b =
-1.2000 1.2000 - 2.0000i
abs(b)

ans =
1.2000 2.3324

ceil(1.2)

ans =
2

ceil(-1.2)

ans =
-1

floor(1.2)

ans =
1

floor(-1.2)

ans =
-2

round(1.2)

ans =
1

round(1.8)

ans =
2

round(-1.8)
ans =
-2

round(-1.2)
ans =
-1

fix(1.8)

ans =
1

fix(-1.8)

ans =
-1

sqrt(2)

ans =
1.4142

sqrt(4)

ans =
2

c=2-4i

c =
2.0000 - 4.0000i

angle(c)

ans =
-1.1071

d=1+i
d =
1.0000 + 1.0000i

angle(d)
ans =
0.7854

ans*180/pi

ans =
45

real(d)

ans =
1

imag(d)

ans =
1

d =
1.0000 + 1.0000i

complex(2,6)

ans =
2.0000 + 6.0000i

conj(1+2*i)

ans =
1.0000 - 2.0000i

gcd(12,3)

ans =
3

gcd(5,3)
ans =
1

lcm(12,3)

ans =
12

lcm(5,3)

ans =
15

sign(1-(2*3)+4)

ans =
-1

sign(-11)

ans =
-1

sign(11)

ans =
1

sign(0)

ans =
0

sign(10000)

ans =
1

log(2)
ans =
0.6931
log10(1000)

ans =
3

log2(8)

ans =
3

mod(5,2)

ans =
1

nchoosek(5,3)

ans =
10

a=pi/2

a =
1.5708

sin(a)

ans =
1

cos(a)

ans =
6.1232e-017

tan(a)
ans =
1.6331e+016

cot(a)

ans =
6.1232e-017

asin(a)

ans =
1.5708 - 1.0232i

acos(a)

ans =
0 + 1.0232i

atan(a)

ans =
1.0039

acot(a)
ans =
0.5669

asinh(a)

ans =
1.2334

acosh(a)

ans =
1.0232

atanh(a)

ans =
0.7525 + 1.5708i

acoth(a)

ans =
0.7525

sec(a)

ans =
1.6331e+016

csc(a)

ans =
1
sech(a)

ans =
0.3985

csch(a)

ans =
0.4345

x=0:0.1:pi/2;

y=sin(x);

plot(x,y)

z=cos(x);

plot(x,y)

hold on

Current plot held

plot(x,z)

hold off

figure(2)
salam='salam mahdi'

salam =
salam mahdi

salam

salam =
salam mahdi

a='salam'

a =
salam

a =
salam

axis([1 2 0 1])

axis([1 2 0 1 5 6])

plot(x,y)

grid

plot(x,y,'r')

plot(x,y,'b')
plot(x,y,'k')

plot(x,y,'g')

plot(x,y,'y')

plot(x,y,'c')

plot(x,y,'w')

plot(x,y,'m')

plot(x,y,'-')

plot(x,y,'--')

plot(x,y,'-.')

plot(x,y,':')

plot(x,y,'*')

plot(x,y,'o')
plot(x,y,'^')

plot(x,y,'d')

plot(x,y,'s')

plot(x,y,'v')

plot(x,y,'p')

plot(x,y,'h')

plot(x,y,'x')

plot(x,y,'-.rs')

plot(x,y,'-.sr')

plot(x,y,'linewidth',2,'markersize',12,'markerfacecolor','r','markered
gecolor', [0.4 0.2 0.3])

plot(x,y,'s','linewidth',2,'markersize',12,'markerfacecolor','r',
'markeredgecolor',[0.4 0.2 0.3])

Linewidth :

Markersize :
Markerfacecolor :

Markeredgecolor :

[0.4 0.2 0.3] :

x=[1:0.1:5];
y=sin(x);
plot(x,y,'sr' ,'linewidth',2,'markersize',12,'markerfacecolor',[0.4 1
0.6]);xlabel('x axis'); ylabel('y axis');

subplot(2,3,1);plot(x,y,'sr','linewidth',2,'markersize',12,'markerface
color',[0.4 1 0.6]);xlabel('x axis');ylabel('y
axis');title('rasm');text(2,0,'(2,0)');

plot(x,y,'sr' ,'linewidth',2,'markersize',12,'markerfacecolor',[0.4 1
0.6]);grid;xlabel ('x axis');ylabel('y axis');title('rasm'
);text(2,0,'(2,0)');
teta=0:.1:2*pi;
c=1-2*sin(teta);
polar(teta,c);

semilogx(x,y);

semilogy(x,y);
loglog(x,y);

comet(x,y);

bar(x,y);
barh(x,y);

hist(y,x);

x=-pi:.1:pi;
bar(x,exp(x.^2));
hist(y,x)

stairs(x,y);
stem(x,y);

pie(x)

a=[1 3 4 2];
b=[1 0 0 1];
pie(a,b)
c=[1 0 0 9];
pie(a,c)

t=0:0.1:2*pi;
x=sin(t);
y=cos(t);
plot3(x,y,t)

[x y]=meshgrid(0:.1:10,0:3);
z=peaks(x,y);

mesh(x,y,z)
meshz(x,y,z)

meshc(x,y,z)

surf(x,y,z)
[x,y,z]=cylinder(2+cos(t))
surf(x,y,z)

x=[1 3 0.5 2.5 2];


e=[0 0 1 0 0];
pie3(x,e);

fplot('sin',[-pi 2*pi])
c=[1 0 9;1 3 9;2 5 7];
det(c)
ans =
-33

syms x
int(1/(1+x^2))
ans =
atan(x)

syms x y
int(y/(1+x),x)
ans =
y*log(1+x)

int(y/(1+x),x,0,1)
ans =
y*log(2)

diary

mfile

m (save as) text

help
X=input('please enter a number for X')

Input('please enter a phrase','s')

: if

If(a~=b)|(a==b)
disp('this is an example')
end

~=

'elseif','else'

for

For i=1:2:50
..

.
End
mfile mfile go to

/n _

clc clear mfile

n
enter
pause
10s
pause(10)

while

While i=0
.

.
i=i+1
end

fprintf

Fprintf('epsilon is equal to %6.2e in %2.3d',epsilon,i)


engineering E

decimal D

fix point F

%6.2

break return

menu

Menu('choose one of these','factorial','i^2','i^3')

mfile

mfile
switch

stat.m stat.m

function [mean,stdev] = stat(x)


x 1x2 stat mean , stdev
stat
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));

f = sym('2*x')
int(f)

int(f,1,2)

bmp, jpg, pcx, png, tif, xwd


Image Read
R = imread ( 'C:\Documents and Settings\hp\My Documents\My Pictures\me.jpg' ) ;

whos R

Name Size Bytes Class

r 300x225x3 202500 uint8 array


imfinfo ( 'C:\Documents and Settings\hp\My Documents\My Pictures\me.jpg' )

Filename: [1x60 char]


FileModDate: '15-Jan-2008 02:31:54'
FileSize: 9770
Format: 'jpg'
FormatVersion: ''
Width: 290
Height: 368
BitDepth: 24
ColorType: 'truecolor'
FormatSignature: ''
NumberOfSamples: 3
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {}

Image Show

imshow(R)

Imwrite ( R , 'C:\Documents and Settings\hp\My Documents\My Pictures\test.jpg' ) ;


jpg test R
I = imread ( ' cameraman.tif ' );
BW = dither (I);
Imview (BW)
Imshow(I)

I BW
Imshow() Imview()

im2bw()

v = imread ( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


BW = im2bw (v,0.5);
Imshow (BW)
figure
imshow (v)
im2bw()

v BW
mat2gray()

m=magic(100);
g=mat2gray(m);
imshow(g)

imread(),imwrite()

v= imread( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


imwrite( v , 'C:\Documents and Settings\hp\Desktop\131339-b.tif' );
tif v imwrite()

imresize()

v= imread ( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


r= imresize (v,2);
whos r v
v imresize()

Name Size Bytes Class


r 732x958x3 2103768 uint8 array
v 366x479x3 525942 uint8 array

r= imresize ( r,[150 100] );


whos r v

Name Size Bytes Class


r 150x100x3 45000 uint8 array
v 366x479x3 525942 uint8 array

imrotate()

ro= imrotate ( r,90,'bilinear' );


imshow (ro)
figure
Imshow (r)

r ro

imcrop

v= imread ( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


c= imcrop;
imshow (c)

v c

cubic nearest bilinear


3x3

r= imread ( 'tire.tif' );
f= inline ( 'max(x(:))' );
r2= nlfilter ( r,[3 3],f );
imshow(r2)
figure
imshow(r)

r r2

c++ inline link f

conv2()
i=magic(1000);
f=magic(10);
c=conv2(i,f);
whos i f c

Name Size Bytes Class


c 1009x1009 8144648 double array
f 10x10 800 double array
i 1000x1000 8000000 double array

mesh(c)
imfilter()

r= imread ( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


f= ones(5,5)/25;
r2= imfilter (r,f);
imshow (r2)

r r2

fspecial()

r= imread ( 'C:\Documents and Settings\hp\Desktop\131339-b.jpg' );


fi= fspecial ( 'unsharp' );
r2= imfilter (r,fi);
imshow (r)
figure
Imshow (r2)

r r2

'average' averaging filter


'disk' circular averaging filter
'gaussian' Gaussian lowpass filter
'laplacian' filter approximating the 2-D Laplacian operator
'log' Laplacian of Gaussian filter
'motion' motion filter
'prewitt' Prewitt horizontal edge-emphasizing filter
'sobel' Sobel horizontal edge-emphasizing filter
'unsharp' unsharp contrast enhancement filter

h = fspecial ( 'average',hsize ) = h = fspecial ( 'average',[3 3] )


h = fspecial ( 'disk',radius ) = h=fspecial ( 'disk',5 )
h = fspecial ( 'gaussian',hsize,sigma ) = h=fspecial ( 'gaussian',[3 3],0.5 )
h = fspecial ( 'laplacian',0<alpha<1 ) = h=fspecial ( 'laplacian',0.2 )
h = fspecial ( 'log',hsize,sigma ) = h=fspecial ( 'log',[5 5],0.5 )
h = fspecial ( 'unsharp',0<alpha <1) = h=fspecial ( 'unsharp',0.2)
h = fspecial ( 'motion' , , )
h = fspecial ( 'prewitt' )
h = fspecial ( 'sobel' )

ftrans2()

colormap(jet(64))
b = remez(10,[0 0.05 0.15 0.55 0.65 1],[0 0 1 1 0 0]);
[H,w] = freqz(b,1,128,'whole');
plot(w/pi-1,fftshift(abs(H)))
mx3 colormap()
blue green red
RGB
m remez()

H b freqz()
H
128x1

2 Whole
fftshift()

plot(w/pi-1,fftshift(abs(H)))
hold on
plot(w,(abs(H)), r )

h = ftrans2(b);
freqz2(h)
b ftrans2()
f2 f1 64x1 H 64x64 freqz2()

[H,f1,f2] = freqz2 ( h , [n2 n1] )

f2 f1 H

hd=zeros(11,11);
hd(4:8,4:8)=1;
[f1,f2]=freqspace(11,'meshgrid');
f2 f1 11x11 freqspace()
f2 f1

[f1,f2]=freqspace(11);
f2 f1

F1=f2=[-n:2:n-2]/n

f1=f2=[-n+1:2:n-1]/n
meshgrid
mesh(f1,f2,hd)
f2 f1 hd z hd y f2 x f1 mesh()

hd

hd
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
h=fsamp2(hd);
h hd FIR fsamp2()
mesh() hd

FIR filter

FIR h
32x32
freqz2(h,[32 32])

FIR filter

FIR

h=fwind1(hd,hamming(11))
n hamming(n)
hd FIR fwind1()
hd
freqz2(h,[32 32])
FIR

11x11

FIR filter

0 0 0 0 0 0.0025 0 0 0 0 0
0 0 0.0005 -0.0003 -0.0027 -0.0041 -0.0027 -0.0003 0.0005 0 0
0 0.0005 0.0018 -0.0013 -0.0126 -0.0198 -0.0126 -0.0013 0.0018 0.0005 0
0 -0.0003 -0.0013 0.0010 0.0093 0.0147 0.0093 0.0010 -0.0013 -0.0003 0
0 -0.0027 -0.0126 0.0093 0.0833 0.1324 0.0833 0.0093 -0.0126 -0.0027 0
0.0025 -0.0041 -0.0198 0.0147 0.1324 0.2066 0.1324 0.0147 -0.0198 -0.0041 0.0025
0 -0.0027 -0.0126 0.0093 0.0833 0.1324 0.0833 0.0093 -0.0126 -0.0027 0
0 -0.0003 -0.0013 0.0010 0.0093 0.0147 0.0093 0.0010 -0.0013 -0.0003 0
0 0.0005 0.0018 -0.0013 -0.0126 -0.0198 -0.0126 -0.0013 0.0018 0.0005 0
0 0 0.0005 -0.0003 -0.0027 -0.0041 -0.0027 -0.0003 0.0005 0 0
0 0 0 0 0 0.0025 0 0 0 0 0
h=[ ];
Freqz2(h)

1x3
RGB

y x R,G,B
z
Help

+12 -12

Max232

1200 COM1

ser=serial('COM1','BaudRate',1200)

1
fopen(ser);

fwrite(ser,'B')

fclose(ser);

delete(ser);

clear ser;

usb
IMAQHWINFO(2)

vid=videoinput('winvideo',2,'RGB24_160x120');
160x120 R,G,B RGB24

preview(vid)

for i=1:200
I=getsnapshot(vid);
End

vid=videoinput('winvideo',2,'RGB24_160x120');
preview(vid)
I=getsnapshot(vid);
a=I(1:15,5:20);

fa=mean(mean(a));

Object Detection By White Filter

clear;
colorimg=imread('white lines.bmp'); %load color image.
imshow(colorimg);
title('original image');
pause;

%dissect image
for a=1:1:170 %ys
for b=1:1:228 %x s
%must cast intensities from uint8 to uint16 since they will
%overflow 2^8 limit when added together
red=cast(colorimg(a,b,1),'uint16'); %get red intensity
green=cast(colorimg(a,b,2),'uint16'); %get green intensity
blue=cast(colorimg(a,b,3),'uint16'); %get blue intensity

%cast back to uint8 for standard intensity image format


gsimage(a,b)=cast((red+green+blue)/3,'uint8');
end
end
%whites-only filter
thresholdw=input('Whites-only intensity threshold: ');
for a=1:1:170
for b=1:1:228
%all three color intensities must be higher than thresholdw
if (colorimg(a,b,1) > thresholdw) && (colorimg(a,b,2) >
thresholdw) && (colorimg(a,b,3) > thresholdw)
%if white detected, copy original pixel
woimage(a,b,1)=colorimg(a,b,1);
woimage(a,b,2)=colorimg(a,b,2);
woimage(a,b,3)=colorimg(a,b,3);
else
%otherwise make pixel black
woimage(a,b,1)=0;
woimage(a,b,2)=0;
woimage(a,b,3)=0;
end
end
end
imshow(woimage);
title('whites-only image');

%detect white pixels.


for a=1:2:170 %only scan every other line.
for b=1:1:228
if woimage(a,b) ~= 0
%calculate position as percentage of entire width or height
ypos = [num2str(floor(a/170*1000)/10) '%'];
xpos = [num2str(floor(b/228*1000)/10) '%'];
stroutput=['Object detected at x=' ypos ' y=' xpos]
break; %skip to next line after first positive pixel found
end
end
end
clc
p=input('enter your path : ')
path=char(p);
i=imread(path);
[l,c,d]=size(i);
for a=1:l
for b=1:c
red(a,b)=i(a,b,1);
green(a,b)=i(a,b,2);
blue(a,b)=i(a,b,3);
end
end

%redPHOTO
for a=1:l
for b=1:c
rphoto(a,b,1)=i(a,b,1);
rphoto(a,b,2)=0;
rphoto(a,b,3)=0;
end
end

%greenPHOTO
for a=1:l
for b=1:c
gphoto(a,b,1)=0;
gphoto(a,b,2)=i(a,b,2)/255;
gphoto(a,b,3)=0;
end
end

%bluePHOTO
for a=1:l
for b=1:c
bphoto(a,b,1)=0;
bphoto(a,b,2)=0;
bphoto(a,b,3)=i(a,b,3)/255;
end
end
imshow(rphoto)
figure
imshow(gphoto)
figure
imshow(bphoto)

You might also like