You are on page 1of 41

MATLAB

Y. Doç. Dr. Aybars U UR Ders Notlar

MATLAB Nedir?   

MATLAB, Mathworks firmas n n geli tirdi i teknik bir programlama dilidir. (www.mathworks.com) MATLAB, teknik hesaplamalar ve matematiksel problemlerin çözümü ve analizi için tasarlanm bir yaz l m geli tirme arac d r. ³MATrix LABoratory´ kelimesinin k saltmas olan MATLAB, matris yani dizi tabanl d r.
Y. Doç. Dr. Aybars U UR
2

28.12.2004

MATLAB Ortam
Komut Penceresi Çal ma Alan Etkin Klasör M-File Help Demolar

28.12.2004

Y. Doç. Dr. Aybars U UR

3

Görüntü

leme

Edge Detection Demo

28.12.2004

Y. Doç. Dr. Aybars U UR

4

2004 Y. Dr.12. Doç.Grafik 28. Aybars U UR 5 .

6 28.         Yapay Sinir A lar Genetik Algoritmalar Görüntü leme Bulan k Mant k Grafik Veritaban Web Sunucusu . Doç...2004 Y.12. Aybars U UR ..MATLAB Araç Kutular      Kontrol statistik Optimizasyon Finans .. Dr.

12. görüntü i leme karakter tan ma (OCR) gibi i levleri içeren yaz l mlar n geli tirilmesini kolayla t r p h zland r r. Örnek : Sadece. Y. Genel bir programlama dili de ildir. Aybars U UR 7 28. Dr. Doç. simülasyon. görüntü i leme araç kutusu 200 kadar fonksiyondan olu ur. veri analizi.Programlama Dillerindeki Yeri    Matlab.2004 . grafik çizimi. denklem çözümü. Geleneksel programlama dillerinde yeterli düzeyde haz r bütünle ik araçlar yoktur.

 Matlab¶ n Windows.MATLAB Programlar n n letimi MATLAB¶da yaz lan programlar.. Doç.2004 Y.  GUI arac ile formlar da olu turulabilir.  MATLAB içinden çal t r labilir  EXE veya DLL olu turulabilir  C/C++ kodlar na dönü türülebilir  ... 28.. Dr.12. Aybars U UR 8 . Linux sürümleri .

2004 Y. Aybars U UR 9 .sayi] deger = 10 20 55 >> whos Name ans deger sayi Workspace. Dr.20.12.De i kenler ve Veri Tipleri >> sayi=55 sayi = 55 >> size(sayi) ans = 1 1 Matris->Dizi Grand total is 6 elements using 48 bytes clear all veya clear ans sayi deger deger=[10. Command History Size 1x2 1x3 1x1 Bytes 16 24 8 Class double array double array double array 28. Doç.

.. Aybars U UR .7:9] 1 2 3 4 5 6 7 8 9 10 d1 = -3:.2]) d=[1:3. Dr.2:3) =?. [3.2 5.3) m1 = 0.4289 m1(5).6813 0. m1(1.0196 0.8381 0. m1(2.3 6] 28.:) =? m1([2 3].8318 0.Dizi ve Matris          lemleri m1 = rand(3.3795 0.2004 Y.12.1. m1(2:3.7095 0.5:3 d1 d1(3) .6721 0.2)¶ye e ittir. Ç kt : -2 d1 = [-2:.5028 0.4:6. Doç.[3 1 2]) ile m1([2:3].5:2] size(d1) i=1 d1(i)=-7 d1(17)=1 (geni letme) d1=[1 4.

 && || mant k kontrol 11 Y. all.12..^  * / ^(matrisler için)  .~= ili kisel  & | xor bitor bitand . isreal.<=.>.3)  rand(5./ .==..2004 lemleri Operatörler :  +  ..* . Dr.3)  ones(2.¶ (devrik alma)  \ .Dizi ve Matris Dizi olu turan temel fonksiyonlar :  zeros(2. Doç.\ (sol bölme)  <. help isreal  .3)  eye(2.  28.5) Di er : any. find..>=.. Aybars U UR .

[2:3.2004 Y.12.4:5]]} m2 = [1x2 double] 'Matlab ' 28. Doç. Aybars U UR 12 .Hücre (Cell)  Elemanlar farkl türlerden dizi. hücre veya yap olabilir. >> m2={[1:2].['Matlab'. Dr.

Doç.adres ans = il: 'Izmir' ilce: 'Bornova' Y.2004 >> ogrenci.adres.Yap (Structure) >> ogrenci. Dr.adres.ilce ='Bornova' >> ogrenci.yas=25 ogrenci = ad: 'Ali' yas: 25 28.il ='Izmir' ogrenci = ad: 'Ali' yas: 25 adres: [1x1 struct] >> ogrenci. Aybars U UR 13 .12.ad='Ali' ogrenci = ad: 'Ali' >> ogrenci.

 .2004 Yükleme  Etkin klasörden  load dosyaad  File -> Import Data xls... Y. txt... Aybars U UR 14 . Doç.Dizi Kaydetme ve Yükleme Kaydetme  Workspace¶den de i kenler seçilerek veya  Save Workspace As ile veya  Komut sat r ndan save dosyaad d1 d2 eklinde yap labilir. Dr. mat . 28..12..

m dosyas na chesapla ismi >>chesapla(5) verilerek cevre = kaydedilir komut 31.0332 28. Dr.12. Aybars U UR 15 .5000 function cevre=chesapla(r) cevre=2*pi*r .2004 Y.Prosedürler ve Fonksiyonlar  New M-File  New M-File % Cevre ve Alan hesaplar % Kucuk buyuk harf duyarli r=5.4159 Ekran Ç kt s Yaz l r ve çal t r l r.5575 alan = 95.5 Ekran Ç kt s cevre=2*pi*r r= alan=pi*r*r 5. cevre = 34.4159 sat r ndan ans = çal t r labilir 31. Doç.

2004 Y. end 28.. end Ekran Ç kt s a= 3 b= 4 a b den kücüktür   Döngü for. switch. x=0. end disp(top) top=0.end. while.end a=3. else disp('a b esittir'). b=4 if a>b disp('a b den büyüktür'). x=-1. while i<100 top=top+i. Dr. Doç. x=1 elseif a<b disp('a b den kücüktür').. break. Aybars U UR 16 .Denetim Deyimleri Seçim  if. i=0. i=i+1. for i=1:2:n top=top+i.12. continue n=input('Sayiyi Verin') top=0.

2004 Y.1:pi.Grafikler . Aybars U UR 17 .Plot t=-pi:0. Dr.y) ?(t¶ye göre y) 28. y=cos(t). Doç. plot(y) %Grafik özellikleri de i tirilebilir %Birden çok grafik % ayn pencerede plot([tµ/3 y']) bar(t.12.

y) yuzey1 = surface(x. Aybars U UR 18 .12.y.y] = meshgrid(x.Mesh x=0:10:100 y=0:20:250 z=rand(13.Grafikler. Dr. Doç.z) % Haz r fonksiyon z=peaks(30) yuzey1 = surface(z) 28.11) %[x.2004 Y.

sin(0:0. -2:.2:2). Aybars U UR 19 28.2:2.12. Z = X . Dr.Y] = meshgrid(-2:.Çizim . mesh(Z)  surf: Solid version of mesh Y.* exp(-X.01:2*pi.01:2*pi)) 3-D: plot3(x.^2 . y)  plot(0:0. mesh plots it  [X. y.Y.Genel    2-D vectors: plot(x.^2). z)(space curve) Surfaces  meshgrid makes surface from axes.2004 . Doç.

. Dr. cos.end Sub Functions Set Figure Axes         Max Min Mean Std Sort Factorial Sin.. tan. log...        Return Global Try. Aybars U UR .. Exp. 20 28. . Doç..2004 Y. .catch. sqrt.Baz önemli deyimler ve fonk.12.

MATLABµda Görüntü leme (IP) Uygulamalar .

mathworks. including:  ‡Spatial image transformations ‡Morphological operations ‡Neighborhood and block operations ‡Linear filtering and filter design ‡Transforms ‡Image analysis and enhancement ‡Image registration ‡Deblurring ‡Region of interest operations http://www.Matlab Image Processing What Is the Image Processing Toolbox? The Image Processing Toolbox is a collection of functions that extend the capability of the MATLAB® numeric computing environment.2004 22 . Dr. Aybars U UR 28. Doç.12.html Y. The toolbox supports a wide range of image processing operations.com/access/helpdesk/help/toolbox/images/images.

2004 Y.12. imshow(I) 28.Read and Display an Image    clear. Aybars U UR 23 . Doç. close all I = imread('rice. Dr.tif').

Doç. Dr.5 uint8 11 28. Aybars U UR 24 .2004 Y.12.Image Arithmetic Functions The image arithmetic functions use these rules for integer arithmetic: ‡ Values that exceed the range of the integer type are truncated to that range ‡ Fractional values are rounded Result Class Truncated Value 300 uint8 255 -45 uint8 0 10.

Aritmetik levler : Toplama rice1= imread('rice. imshow(rice2). % Ba ka resim ile de % toplanabilir 28.55).2004 Y. Dr.tif'). rice2= imadd(rice. Aybars U UR 25 .12. Doç.

2004 Y.background). rice2 = imsubtract(rice.Aritmetik levler : Ç karma rice= imread('rice.figure. strel('disk'. 28.12. Dr. background = imopen(rice. Aybars U UR 26 .imshow(rice2).tif'). imshow(rice).15)). Doç.

Doç.j)=t1(i. end end t2=uint8(I1) t1 = imcrop(t2.n]=size(t1). Dr.Matris elemanlar üzerinde i lem t1=imread('rice. t1=double(t1). Aybars U UR 27 . imshow(t1) 28. for i=1:m for j=1:n I1(i.12.j)+100.tif'). [m.2004 Y.[1 1 255 255]).

-15.tif') im2 = imread('forest.Image Rotation im1 = imread('flowers. Doç. imshow(J).'crop').'bilinear'. imshow(imnoise(im1. Aybars U UR 28 .12.'gaussian')) 28. Dr.2004 Y.tif') imshow(im1) J=imrotate(im1.

12.2. subplot(1.map2]=imread('trees. use [X1. Dr.1). For example.tif').tif'). if you want to display two images side by side.I Displaying Multiple Images in the Same Figure You can display multiple images in a single figure window with some limitations.map1]=imread('forest. This discussion shows you how to do this in one of two ways: 1 By using imshow in conjunction with subplot 2 By using subimage in conjunction with subplot subplot divides a figure into multiple display regions.n.2.2).p) This syntax divides the figure into an m-by-n matrix of display regions and makes the pth display region active.Displaying Multiple Images . imshow(X1. imshow(X2.map2) 28. The syntax of subplot is subplot(m. Doç.2004 Y. [X2.map2) subplot(1. Aybars U UR 29 .

Displaying Multiple Images . subplot(1.tif'). Dr.map2) 28.map1) subplot(1. This example displays the same two images shown in Figure 3-5 with better results.map2]=imread('trees.2.2). Doç.map1]=imread('forest.2. Aybars U UR 30 . [X2. subimage(X1.tif'). subimage(X2.II        subimage converts images to RGB before displaying and therefore circumvents the colormap sharing problem. [X1.12.1).2004 Y.

h = fspecial('unsharp'). imshow(I2). title('Filtered image') imwrite(I2.tif').2004 Y. title('Original image') figure. Doç.12. Dr. Aybars U UR 31 .h).jpg').Using Predefined Filter Types I = imread('moon. 28.µmoon2. I2 = imfilter(I. imshow(I).

tif'). imshow(I) figure.Edge Detection & Filling Holes Compute a binary edge image using the edge function.tif'). imshow(blood) blood2 = imcomplement(imfill(imco mplement(blood). Aybars U UR 32 . I = imread('ic. Dr. Doç.2004 Y. imshow(BW) blood = imread('blood1. BW = edge(I).'holes')).12. imshow(blood2) 28. figure.

BW1 = edge(I.'canny'). Dr.'sobel'). imshow(BW1) figure.2004 Y.tif'). imshow(BW2) 28. Aybars U UR 33 . BW2 = edge(I. Doç.Edge Detection & Filling Holes      I = imread('rice.12.

eventdata.edit1. set(handles.'String'. if ~isempty(A) & ~isempty(B) C=num2str(str2num(A)+str2num(B)) else C='?' end.2004 34 .12. Dr. Aybars U UR 28. handles) A=get(handles.C).  Y. M-File Editor¶den dü menin Callback fonksiyonuna sadece a a daki kod yaz l r : function pushbutton1_Callback(hObject. Bir ad verilerek kaydedilir. Doç.GUI Örne i : ki Say y Toplayan Matlab Program Start  Matlab  GUIDE (GUI Builder)  Blank GUI (Default) 1 Push Button ve 3 tane Edit Text eklenir Button¶ n String¶i Property Inspector¶den Topla yap l r Edit¶lerin String¶leri silinir. B=get(handles.edit3.'String').'String').edit2.

if ~isempty(A) & ~isempty(B) C=num2str(str2num(A)+str2num(B)) else C='?' end.edit3. set(handles.2004 35 . Bir ad verilerek kaydedilir.edit2. M-File Editor¶den dü menin Callback fonksiyonuna sadece a a daki kod yaz l r : function pushbutton1_Callback(hObject.12. B=get(handles.C).edit1. Aybars U UR 28.GUI Örne i : Resim Okuma ve Yeni Pencerede Çizdirme Start  Matlab  GUIDE (GUI Builder)  Blank GUI (Default) 1 Push Button ve 3 tane Edit Text eklenir Button¶ n String¶i Property Inspector¶den Topla yap l r Edit¶lerin String¶leri silinir.  Y. eventdata.'String').'String'). Dr. Doç.'String'. handles) A=get(handles.

Doç.Ayn Pencerede Resim Görüntüleme himage = imshow(im).12. 28.2004 Y. Aybars U UR 36 . Dr.

'IntegerHandle'. Aybars U UR 37 . handles) im1 = imread('leopard. 'Name'.. % Display the image in a figure with imshow.. hfig = figure('Toolbar'.. eventdata. 'none'. 28.. Dr.GUI Örne i : Yeni pencerede resim görüntüleme function pushbutton1_Callback(hObject.'none'....jpg').2004 Y.'off').. image_util(im1). function image_util(im) % Create a figure without % toolbar and menubar.. 'Menubar'. himage = imshow(im)..12.'My Large Image Display Tool'..'off'. Doç.. 'NumberTitle'.

type is a string having one of these values: 'gaussian' for a Gaussian lowpass filter 'sobel' for a Sobel horizontal edge-emphasizing filter 'prewitt' for a Prewitt horizontal edge-emphasizing filter 'laplacian' for a filter approximating the two-dimensional Laplacian operator 'log' for a Laplacian of Gaussian filter 'average' for an averaging filter 'unsharp' for an unsharp contrast enhancement filter 28.parameters) Definition : h = fspecial(type) creates a two-dimensional filter. of the specified type. which is the appropriate form to use with imfilter. Dr. Aybars U UR 38 . h.12.FSPECIAL Creates 2-D special filters Syntax : h = fspecial(type) h = fspecial(type.2004 Y. fspecial returns h as a correlation kernel. Doç.

hsize. hsize can be a vector specifying the number of rows and columns in h. The parameter alpha controls the shape of the Laplacian and must be in the range 0.FSPECIAL The following list shows the syntax for each filter type. additional parameters are also shown. Aybars U UR 39 . The default value for hsize is [3 3]. The default radius is 5. The default value for hsize is [5 5] and 0. h = fspecial('gaussian'. The default value for hsize is [3 3].2. or it can be a scalar.5.hsize) returns an averaging filter.5 for sigma. hsize can be a vector specifying the number of rows and columns in h. the default value for sigma is 0.2004 Y. or it can be a scalar.0. of size hsize. Doç. or it can be a scalar.sigma) returns a rotationally symmetric Laplacian of Gaussian filter of size hsize with standard deviation sigma (positive).alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator. h = fspecial('disk'.sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma (positive). h = fspecial('average'. The argument hsize can be a vector specifying the number of rows and columns in h. in which case h is a square matrix.12. 28.hsize. Where applicable. h.0 to 1. h = fspecial('log'. Dr. in which case h is a square matrix.radius) returns a circular averaging filter (pillbox) within the square matrix of side 2*radius+1. in which case h is a square matrix. h = fspecial('laplacian'. The default value for alpha is 0.

the default theta is 0. h'. which corresponds to a horizontal motion of 9 pixels. h = fspecial('sobel') returns a 3-by-3 filter. Dr. If you need to emphasize vertical edges.12. [ 1 1 1 0 0 0 -1 -1 -1 ] To find vertical edges.0. use h'. Doç. The filter becomes a vector for horizontal and vertical motions. h. h. transpose the filter. 28.2. fspecial creates the unsharp filter from the negative of the Laplacian filter with parameter alpha. Aybars U UR 40 .FSPECIAL h = fspecial('motion'. alpha controls the shape of the Laplacian and must be in the range 0. with an angle of theta degrees in a counter-clockwise direction.theta) returns a filter to approximate. If you need to emphasize vertical edges. (shown below) that emphasizes horizontal edges by approximating a vertical gradient. h'.len. The default len is 9. h = fspecial('prewitt') returns a 3-by-3 filter. transpose the filter.0 to 1. the linear motion of a camera by len pixels. or for x-derivatives. (shown below) that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient.alpha) returns a 3-by-3 unsharp contrast enhancement filter.2004 Y. once convolved with an image. The default value for alpha is 0. [ 1 2 1 0 0 0 -1 -2 -1 ] h = fspecial('unsharp'.

sharpened = imfilter(I.H).title('Original Image'). 28.2. H = fspecial('unsharp').imshow(MotionBlur). Dr.45). Aybars U UR 41 .imshow(sharpened).imshow(I).title('Blurred Image').tif').H). subplot(2. blurred = imfilter(I. subplot(2.2).4). subplot(2.50.1).10).title('Sharpened Image'). H = fspecial('motion'. subplot(2.imshow(blurred). MotionBlur = imfilter(I.title('Motion Blurred Image').2004 Y.H).3).FSPECIAL I = imread('saturn.2.12.2. Doç.2. H = fspecial('disk'.