30
Introduction to Matlab Section 2

Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Embed Size (px)

DESCRIPTION

x=0:pi/100:2*pi ; y=sin(x); z=cos(x) ; subplot (2,1,1) ; plot (x,y) ; grid on; title (‘Sine function’) ; subplot (2,1,2) ; plot (x,z) ; grid off; title (‘Cosine function’) ;

Citation preview

Page 1: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Introduction to MatlabSection 2

Page 2: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

2D – Graphics

x=0:pi/100:2*pi ;y=sin(x); plot (x,y) ;hold on ;z=cos(x) ;plot (x,z) ;

Page 3: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=0:pi/100:2*pi ;y=sin(x);z=cos(x) ;subplot (2,1,1) ; plot (x,y) ;grid on;title (‘Sine function’) ;subplot (2,1,2) ;plot (x,z) ;grid off;title (‘Cosine function’) ;

Page 4: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=-2*pi:pi/100:2*pi ;y=sin(x);z=2*cos(x) ;subplot (2,1,1) ;plot (x,y,x,z) ;title (‘plot in same y-axis scale’);subplot (2,1,2) ; plotyy(x,y,x,z);title (‘plot in different y-axis

scales’);

Page 5: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

subplot(3,1,1) ;fplot('abs(exp(-j*x*(0:9))*…

ones(10,1))',[0 2*pi]);subplot(3,1,2) ;fplot('[tan(x),sin(x),cos(x)]',…

2*pi*[-1 1 -1 1]) ;subplot(3,1,3) ;fplot('sin(1 ./ x)', [0.01 0.1 ],…1e-3) ;

Plot a function between specified limits

Page 6: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

M=[0.5 , 1 , 1.6 , 1.2 , 0.8 , 2.1] ;N=[2 3 4 5];subplot (1,3,1) ;pie (M) ;title (‘case 1’) ; subplot (1,3,2) ;pie (M , M==max(M));title (‘case 2’) ; subplot (1,3,3) ;pie (N,{'North','South',…

'East','West'}) ;title (‘case 3’) ;

Page 7: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=[1 5 8];y=[4 9 2];z=[3 6 7];a=[2 3 9 7] ;b=[5 8 6 1] ;c=[2 4.7 3 9];subplot (2,2,1) ;fill (x,y,’r’); subplot (2,2,2) ;fill (a,b,’b’) ;subplot (2,2,3) ;fill3 (x,y,z,’r’); subplot (2,2,4) ;fill3 (a,b,c,’b’) ;

Page 8: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=-3:0.2:3 ;y=exp (-x .*x) ;subplot(1,2,1) ;bar(x,y) ;subplot(1,2,2) ;bar3(x,y) ;

Page 9: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=-3:0.2:3 ;y=exp(-x .* x) ;subplot (1,2,1) ;barh (x,y) ; subplot (1,2,2) ;stairs (x,y);

Page 10: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

x=-pi:pi/100:pi ;y=sin(x) ;plot (x,y) ;title (‘Graph of the Sine

function’) ;ylabel (‘ y=sin(x)’);xlabel (‘ -\pi \leq {\itx} \leq \

pi’) ; text (1,-1/3,‘ text1’) ;

text1

Page 11: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

axis ([Xmin Xmax Ymin Ymax Zmin Zmax])

األبعاد حسب اإلحداثيات يحددالمستخدم قبل من المدخلة

axis ([-2 2 -0.6 0.6]);

Page 12: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

axis autoمن تلقائيا اإلحداثيات يحدد

بقيم MATLAB طرفللشكل مناسبة تكون.المرسوم

Page 13: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

axis tightبمدى اإلحداثيات يحدد

المرسومة المعلومات

Page 14: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

axis equalجميع في اإلحداثيات يجعل

محدودة االتجاهاتالقيم بنفس

Page 15: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

axis square شكر على اإلحداثيات يجعل

مربع

Page 16: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

* الرموز المستخدمة في التحكم بشكل الرسمالرمز اللون الرمز المؤشر الرمز الخط نوع

b . األزرق -g األخضر o :r األحمر x -.c األخضر

المزرق+ --

m البنفسجي الفاتح

*

y األصفر s مربعk األسود d معينw األبيض v إلى مثلث

األسفل^ إلى مثلث

األعلى> إلى مثلث

اليمين< إلى مثلث

اليسارp ثماني شكلh ست شكل

عشري

للرمز مشابه الناتج أن ذلك معنى للرمز مقابل فراغ وجود حالة .في

Page 17: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

examplex=1:9;a=rand(3);b=rand(3);c=rand(3);figure;hold on; plot (x,a(:),'bs') ;plot (x,b(:),'gd') ;plot (x,c(:),'r^') ;plot (x,a(:).*c(:),'m*') ;hold off;

Page 18: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Image Processing Toolbox Reading a Graphics Image[RGB, map] =

imread('ngc6543a.jpg');

Writing Imageimwrite(A,map,’filename’,fmt);imwrite(RGB,map, 'myfile.jpg',

'Quality', 100);

Image types BMP (Microsoft Windows Bitmap) CUR (Microsoft Windows Cursor

resource) GIF (Graphics Interchange Format) HDF (Hierarchical Data Format) ICO (Windows Icon resource JPEG (Joint Photographic Experts

Group) PBM (Portable Bitmap) PCX (Windows Paintbrush) PGM (Portable Graymap) PNG (Portable Network Graphics) PPM (Portable Pixmap) RAS (Sun

Raster image) TIFF (Tagged Image File Format) XWD (X Window Dump)

Page 19: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Querying a Graphics Fileinfo = imfinfo(filename)

Name of the file File format Version number of the file

format File modification date File size in bytes Image width in pixels Image height in pixels Number of bits per pixel Image type:

RGB (truecolor), intensity (grayscale), or indexed

Page 20: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Converting Image Storage Classes im2double, im2uint8 im2uint16

Page 21: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Image Types in the Toolbox

Indexed Images size(X) = [m,n] imshow(X,map)

Page 22: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Intensity Images size(X) = [m,n] values 0:255 I = rgb2gray(RGB) imshow(I)

Page 23: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Binary image size(X) = [m,n] values 0,1 imshow(BW)

Page 24: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

RGB image (True color) size(RGB) = [m,n,c] c=3 values 0:255 imshow(RGB)

Page 25: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Display multi images[X1,map1]=imread('forest.tif');[X2,map2]=imread('trees.tif');subplot(1,2,1), imshow(X1,map2)subplot(1,2,2), imshow(X2,map2)

Page 26: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Display multi images with different color map

[X1,map1]=imread('forest.tif');[X2,map2]=imread('trees.tif');subplot(1,2,1),

subimage(X1,map1)subplot(1,2,2),

subimage(X2,map2)

Page 27: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Image Arithmetic Functions

Function Description

imabsdiff Absolute difference of two images

imadd Add two images

imcomplement Complement an image

imdivide Divide two images

imlincomb Compute linear combination of two images

immultiply Multiply two images

imsubtract Subtract two images

Page 28: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

example

I = imread('rice.tif');J = imread('cameraman.tif');K = imadd(I,J);imshow(K)

Page 29: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Exercise

Read a color image and display it Convert it to gray image and display it Calculate its histogram and display it as bar and

line figures Read another gray image Display the average image Display all images in one figure

Page 30: Introduction to Matlab Section 2. 2D – Graphics x=0:pi/100:2*pi ; y=sin(x); plot (x,y) ; hold on ; z=cos(x) ; plot (x,z) ;

Hints

True color images