300 likes | 461 Views
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;
E N D
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’) ;
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’);
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
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’) ;
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’) ;
x=-3:0.2:3 ; y=exp (-x .*x) ; subplot(1,2,1) ; bar(x,y) ; subplot(1,2,2) ; bar3(x,y) ;
x=-3:0.2:3 ; y=exp(-x .* x) ; subplot (1,2,1) ; barh (x,y) ; subplot (1,2,2) ; stairs (x,y);
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
axis ([Xmin Xmax Ymin Ymax Zmin Zmax]) يحدد الإحداثيات حسب الأبعاد المدخلة من قبل المستخدم axis ([-2 2 -0.6 0.6]);
axis auto يحدد الإحداثيات تلقائيا من طرف MATLAB بقيم تكون مناسبة للشكل المرسوم.
axis tight يحدد الإحداثيات بمدى المعلومات المرسومة
axis equal يجعل الإحداثيات في جميع الاتجاهات محدودة بنفس القيم
axis square يجعل الإحداثيات على شكر مربع
في حالة وجود فراغ مقابل للرمز معنى ذلك أن الناتج مشابه للرمز.
example x=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;
Reading a Graphics Image [RGB, map] = imread('ngc6543a.jpg'); Writing Image imwrite(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) Image Processing Toolbox
Querying a Graphics File info = 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
Converting Image Storage Classes • im2double, • im2uint8 • im2uint16
Image Types in the Toolbox • Indexed Images • size(X) = [m,n] • imshow(X,map)
Intensity Images • size(X) = [m,n] • values 0:255 • I = rgb2gray(RGB) • imshow(I)
Binary image • size(X) = [m,n] • values 0,1 • imshow(BW)
RGB image (True color) • size(RGB) = [m,n,c] • c=3 • values 0:255 • imshow(RGB)
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)
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)
example I = imread('rice.tif'); J = imread('cameraman.tif'); K = imadd(I,J); imshow(K)
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
Hints • True color images