1 / 76

Lecture 2: Matlab tutorial

Lecture 2: Matlab tutorial. Overview. MATLAB GUI and Help Vectors and Matrices MATLAB Built-in Functions Plotting Data Classes Basic image operations. Why Matlab?. Matrix Laboratory Everything is matrix in Matlab Scalar, vector, matrix (of course), cell... Good at

yahto
Download Presentation

Lecture 2: Matlab tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2: Matlab tutorial

  2. Overview • MATLAB GUI and Help • Vectors and Matrices • MATLAB Built-in Functions • Plotting • Data Classes • Basic image operations

  3. Why Matlab? • Matrix Laboratory • Everything is matrix in Matlab • Scalar, vector, matrix (of course), cell... • Good at • numerical computing • image processing • visualization • Powerful toolboxes • Image processing

  4. Working Directory Work- space Command Window

  5. M-files

  6. To begin with: Matlab Help • helpkeyword • Help for MATLAB functions in Command Window • doc keyword • Displays the help text for the specified TOPIC inside the Help window • lookfor keyword • Search for keyword in all help entries • type keyword • List the ascii file called ‘keyword.m’

  7. Some basic commands • clc % clear command window • clear % clear all variables in workspace • close all % close all figures • exit/quit % quit Matlab

  8. Variables • Naming Rules: • Must start with a letter, which can be followed by letters, digits, and underscores. • Case sensitive: A and a are two different variables • Checking if variable names are valid >> isvarname 8a • Do not use Function names for variables: >> which var_name • Checking for reserved keywords >> iskeyword • Avoid using i and j (imaginary units) for variables • Avoid overwriting existing variables in workspace

  9. >> iskeyword ans = 'break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'parfor' 'persistent' 'return' 'switch' 'try' 'while'

  10. Constants pi            3.1415926…….   i , j           Imaginary unit of a complex number Inf           Infinity (1/0) eps        Floating-point relative accuracy: 2^-52           NaN        Not a Number (0/0)

  11. Data types • Scalar: Single number • Vector: Ordered list of numbers • a column or a row • Matrix: Rectangle array of numbers • 2 dimensional: Rows and Columns • Multi-dimensional: Rows, Columns and Planes Example: ColorImage(r, c, 3) 3 planes, each plane is a matrix of size R x C

  12. MATLAB and images • An image in MATLAB is treated as a matrix • Every pixel is a matrix element • All the operators in MATLAB defined on matrices can be used on images: +, -, *, /, ^, sqrt, sin, cos etc.

  13. Matrices • Create a matrix: • A= [1 2 3; 4 5 6; 7 8 9]; • B= first_number : increment : last_number; • C= linspace(first, last, n); • D= [B C]; % or D = cat(1, B, C); • E= [B; C]; % or D = cat(2, B, C); • F= A(1:2, 2:end); • G = A(:, end:-1:1)

  14. Example 1 >> A1 = 1: 2: 8 A1 = 1 3 5 7 >> A2 = linspace(1, 8, 4) A2 = 1.0000 3.3333 5.6667 8.0000

  15. Example 2 >> A3 = [1 2 3; 4 5 6; 7 8 9] A3 = 1 2 3 4 5 6 7 8 9 >> F= A(1:2, 2:end) F = 2 3 5 6

  16. Specialized Matrix Functions • zeros(m,n); % mxn matrix of 0’s • zeros(n); % nxn square matrix of 0’s • ones(m,n); ones(n); % matrix of 1’s • eye(m,n); eye(n); % identity matrix • rand(m,n); rand(n); % pseudo-random values drawn from a uniform distribution within [0, 1]. • randn(m,n); randn(n) % pseudo-random values drawn from a normal distribution with mean 0 and standard deviation 1.

  17. Scaling • Scale uniform distribution into range [a, b]: A = (b-a)*rand(m,n) + a • Scale normal distribution with standard deviation sigma and mean mu A = sigma*randn(m,n) + mu

  18. Example 3 >>a=3*randn(10000,1)-2; >> sa= std(a) sa = 3.0141 >> ma = mean(a) ma = -1.9909 >> a=3*randn(100,1)-2; >> sa= std(a) sa = 2.7799 >> ma = mean(a) ma = -1.7264

  19. Matrix-Constant Operations if A= [ a1 , a2 ,…, an], c = constant, we have: A + c = [ a1+c, a2+c,…, an+c ] A .* c = [ a1*c, a2*c,…, an*c ] A ./ c = [ a1 /c, a2 /c,…, an /c ] A .^c = [ a1^c, a2^c,…, an^c ] c .^A = [ c^a1 , c^a2 ,…, c^an ]

  20. Matrix-Matrix Operations if A= [ a1 , a2 ,…, an], B= [ b1 , b2 ,…, bn], we have: A + B = [ a1+b1 , a2+b2 ,…, an+bn ] A .* B = [ a1*b1 , a2*b2 ,…, an*bn ] A . / B = [ a1 /b1 , a2 /b2 ,…, an /bn ] A .^ B = [ a1^b1 ,a2^b2 ,…, an^bn ]

  21. Matrix Operations in Matlab • Addition A+B • Multiplication A*B • Element-wise multiplication A.*B • Matrix right division A/B= A* inv(B) • Element-wise right division A./B • Element-wise power A.^B • Matrix transpose A.' • Matrix concatenation [A B] [A; B] • Matrix index A(r, c)

  22. Matrix Multiplications Matrix Multiplication 2*5 + 3*6 = * 5*5 + 6*6 Element by Element multiplication 2*5 .* = 5*6

  23. MATLAB Built-in Functions round: rounds towards nearest integer, either up or down floor: Rounds towards minus infinity ceil: Rounds towards positive infinity fix: Rounds towards zero >> round(7.8) ans = 8 >> round (7.2) ans = 7 >> floor(7.8) ans = 7 >> ceil(7.8) ans = 8 >> fix(-1.2) ans = -1

  24. Descriptive Statistics • cov(A) Covariance matrix of A • max(A) Largest elements in A along each column • min(A) Smallest elements in A along each column • mean(A) Average or mean value of A along each column • std(A) Standard deviation of A along each column • var(A) Variance of A along each column • abs(A) Absolute value and complex magnitude • Calculate range along each row: • min(A, [], 2), max(A, [], 2) • mean(A, 2)

  25. A = [1 2 3 4 5 6 7 8 9]; Example 4 >> m1 = max(A) m1 = 7 8 9 >> m2 = min(A) m2 = 1 2 3 >> m3 = mean(A) m1 = 4 5 6 >> s1 = std(A) s1 = 3 3 3

  26. A = [1 2 3 4 5 6 7 8 9]; Example 5 >> m1 = max(max(A)) m1 = 9 >> m2 = max(A(:)) m2 = 9 >> A1 = A(:) A1 = 1 4 7 2 5 8 3 6 9

  27. Logic Operations

  28. Graph Functions (summary) • 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

  29. Matlab plot close all; x=linspace(0, 2*pi, 100); y=sin(x); figure, plot( x,y ); xlabel('x(rad)'); ylabel('y'); title('Sine wave');

  30. figure, plot( x, sin(x), 'rx', x, cos(x), 'b*' ); xlabel('Input Value'); ylabel('Function Value'); title('Sine and Cosine wave'); legend('y = sin(x)','y = cos(x)'); grid on;

  31. x=linspace(-2*pi, 2*pi, 200); subplot(2,2,1); plot(x, sin(x)), title('Sine wave'); subplot(2,2,2); plot(x, cos(x)), title('Cosine wave'); subplot(2,2,3); plot(x, sinh(x)), title('Sinh wave'); subplot(2,2,4); plot(x, cosh(x)), title('Cosh wave');

  32. [x,y] = meshgrid(-8:0.5:8); r = sqrt(x.^2+y.^2)+eps; z = sin(r)./r; mesh(z);

  33. IMAGES:Sampling and Quantization • In the real world images are represented by continuous functions • Images are continuous in both in their spatial coordinates and amplitude • To create digital images must convert both the coordinates and the amplitude • Sampling: digitizing the coordinate values • Quantization: digitizing amplitude values

  34. Images types • Binary images : {0,1} • Intensity images : [0,1] or uint8, double etc. • RGB images : m x n x 3 Red plane, Green plane and Blue plane

  35. Chapter 2 – Fundamentals www.prenhall.com/gonzalezwoodseddins JPEG images

  36. Reading Images in MATLAB • imread(‘filename’) • At Command line: >> f = imread(‘chestxray.jpg’); • The semicolon suppresses the output. Without the semicolon the output is displayed • Without path information imread reads the image from the current directory, if not found, then searches the MATLAB search path • To include path: >> f = imread(‘D:\images\chestxray.jpg’);

  37. Digital Image >> Im = imread('D:\Images\PinkHotel.jpg'); >> figure, imshow(Im); >> size_Im = size(Im) size_Im = 1600 1200 3 Digital image: 1600 rows x 1200 columns x 3 planes Planes: red, green and blue (RGB) >> whos Im Name Size Bytes Class Im 1600x1200x3 5760000 uint8 5760000 = 1600x1200x3 bytes

  38. Chapter 2 – Fundamentals www.prenhall.com/gonzalezwoodseddins Data type for Matlab calculations Default class in reading data from devices 8 numeric data classes

  39. Working with Image Data >> d = Im(45:50,45:50,1) d = 47 81 18 46 81 159 50 153 42 16 48 122 51 105 72 26 46 59 139163 125 60 42 110 116 179 187 116 36 125 22 96 169 188 119 151 >> d2 = d*2 d2 = 94 162 36 92 162 255 100 255 84 32 96 244 102 210 144 52 92 118 255 255 250 120 84 220 232 255 255 232 72 250 44 192 255255 238 255 Maximum value for uint8 is 255

  40. Convert to Double >> d3 = double(d) d3 = 47 81 18 46 81 159 50 153 42 16 48 122 51 105 72 26 46 59 139 163 125 60 42 110 116 179 187 116 36 125 22 96 169 188 119 151 >> d4=2*d3 d4 = 94 162 36 92 162 318 100 306 84 32 96 244 102 210 144 52 92 118 278 326 250 120 84 220 232 358 374 232 72 250 44 192 338 376 238 302

  41. Convert using im2double >> d5 = im2double(d) % d is type uint8 d5 = 0.1843 0.3176 0.0706 0.1804 0.3176 0.6235 0.1961 0.6000 0.1647 0.0627 0.1882 0.4784 0.2000 0.4118 0.2824 0.1020 0.1804 0.2314 0.5451 0.6392 0.4902 0.2353 0.1647 0.4314 0.4549 0.7020 0.7333 0.4549 0.1412 0.4902 0.0863 0.3765 0.6627 0.7373 0.4667 0.5922 im2double: If original data is of type uint8 will convert to type double in the range [0,1]

  42. If the data is already double >> d3 = double(d) % d3 is now type double d3 = 47 81 18 46 81 159 50 153 42 16 48 122 51 105 72 26 46 59 139 163 125 60 42 110 116 179 187 116 36 125 22 96 169 188 119 151 >> d6 = im2double(d3) % No change since d3 is type double d6 = 47 81 18 46 81 159 50 153 42 16 48 122 51 105 72 26 46 59 139 163 125 60 42 110 116 179 187 116 36 125 22 96 169 188 119 151

  43. mat2gray >> help mat2gray MAT2GRAY Convert matrix to intensity image. I = MAT2GRAY(A, [AMIN AMAX]) converts the matrix A to the intensity image I. The returned matrix I contains values in the range 0.0 (black) to 1.0 (full intensity or white). AMIN and AMAX are the values in A that correspond to 0.0 and 1.0 in I. Values less than AMIN become 0.0, and values greater than AMAX become 1.0. I = MAT2GRAY(A) sets the values of AMIN and AMAX to the minimum and maximum values in A. Class Support ------------- The input array A can be logical or numeric. The output image I is double.

  44. mat2gray Example d = 47 81 18 46 81 159 50 153 42 16 48 122 51 105 72 26 46 59 139 163 125 60 42 110 116 179 187 116 36 125 22 96 169 188 119 151 >> dgray1 = mat2gray(d) dgray1 = 0.1802 0.3779 0.0116 0.1744 0.3779 0.8314 0.1977 0.7965 0.1512 0 0.1860 0.6163 0.2035 0.5174 0.3256 0.0581 0.1744 0.2500 0.7151 0.8547 0.6337 0.2558 0.1512 0.5465 0.5814 0.9477 0.9942 0.5814 0.1163 0.6337 0.0349 0.4651 0.8895 1.0000 0.5988 0.7849 >> dgray = mat2gray(d, [0 255]) dgray2 = 0.1843 0.3176 0.0706 0.1804 0.3176 0.6235 0.1961 0.6000 0.1647 0.0627 0.1882 0.4784 0.2000 0.4118 0.2824 0.1020 0.1804 0.2314 0.5451 0.6392 0.4902 0.2353 0.1647 0.4314 0.4549 0.7020 0.7333 0.4549 0.1412 0.4902 0.0863 0.3765 0.6627 0.7373 0.4667 0.5922 Smallest value converted to 0, largest to 255 In this case, 0 will be mapped to 0 and 255 will be mapped to 1

  45. Im = imread('D:\Images\PinkHotel.jpg'); figure, imshow (double(Im)), title('Imdouble1 Pink Hotel'); figure, imshow (im2double(Im)), title('Imdouble2 Pink Hotel');

  46. Im_gray = rgb2gray(Im_double2); figure, imshow(Im_gray), title('Gray Image'); Im_bw = im2bw(Im_gray, 0.57); figure, imshow(Im_bw), title('Binary Image');

  47. Displaying Images • imshow( f ) • f is image array • imshow( f, [ low high ] ) • Displays as black values less than low • Displays as white all values equal or greater than high • imshow( f, [ ] ) • Sets variable low to lowest value in f • Sets variable high to highest value in f

  48. Gray Image Gray Image [ ]

More Related