Docstoc

Introduction to Matlab

Document Sample
Introduction to Matlab Powered By Docstoc
					       Holon Academic Institute of Technology
       Department of Industrial Mathematics



Digital Image Processing in
MATLAB Environment

   Matlab Fundamentals, Programming
   Techniques, and Working with images
   using MATLAB Image Processing Toolbox
             Instructor: Eugene Kruchkov,
                        2005
Course Purpose
    This course is designed to give students and
     seniors a fundamental understanding of digital
     image processing techniques with Matlab
     Image Processing Toolbox, including
     Using different types of image representations
     Enhancing image characteristics
     Filtering images
    Reducing the effects of noise and blurring
    Extracting features and objects within an image
    Registering images
    Reconstructing images/objects


                                                   2
Course Properties
   This is a 24 hours (about 4 weeks) course, 6 hours
    weekly, and the final exam.
   Time and Place:
    18:00 – 21:00 PM, Monday, Room 104, Bld. 3.
    18:00 – 21:00 PM, Thursday, Room 104, Bld. 3.
    First Session: 01.08.2005
   Last Session: 26.08.2005
   Contact:
      E-mail: ns_tima@bezeqint.net
      Phone: 054 - 4355083



                                                         3
Course Requirements
   Basic knowledge of Linear Algebra, Probability, and
    MATLAB is required. A prior acquaintance with
    basic Signal and Image processing concepts is
    recommended, but not required.
   Students are assumed to be familiar with basic
    programming concepts.
   Student will be in his/her seat in the lecture hall prior
    to the start of every lecture. This is a common
    courtesy to the instructor and to fellow students.
    If the student wants more information on a topic or
    needs help, he/she will make an appointment with
    the instructor.

                                                                4
Course Objectives

   Give the students a general understanding of
    the fundamentals of digital image processing.
   Introduce the student to analytical tools, which
    are currently used in digital image processing as
    applied to image information for human viewing.
   Develop the students ability to apply these tools
    in image enhancement, segmentation, and
    feature extraction using MATLAB image-
    processing environment

                                                        5
Expected Results

By the end of this course, participants will:
 Learn to develop, fast prototype, and test
  image processing algorithms.
 Gain workable knowledge of the powerful
  MATLAB image-processing environment.
 Learn about the most recent advances in the
  field.



                                                6
Topics Covered
   Covers introductory topics, including working with
    matrices, data manipulation, file I/O, and
    programming.
    Image basics (I/O, color, files, objects)
   Image acquisition
   Color spaces
   Histograms – correcting contrast & illumination
    problems
   Image Quantization
   Thresholding
   Linear filtering Block processing
   Image enhancement & restoration
                                                         7
Topics Covered, Cont.

   Block processing
   Image enhancement & restoration
   Image Deblurring and Sharpening
   BW Morphology and Edge detection
   Wavelet transform
   Fourier transform
   Radon & Hough transforms
    Distance transform
                                       8
Topics Covered, Cont.

   Connected component analysis
   Grayscale Morphology
   Segmentation: shape, color, watershed
   Feature extraction
   Image registration & geometric
    transformations
   Motion detection and tracking


                                            9
Methods of assessment

   One control test (20%) and final exam (80%).
   Student will pass the course taking 60 points.
   The control test will assess the basic
    concepts of the subject and the main
    techniques used in MATLAB.
   The function of the final exam is to test the
    students ability to tackle a real problem,
    produce working MATLAB code, and present
    obtained results in a coherent way.
                                                 10
Feedback and student support during
course study
   Course Notes will be provided
   Feedback will be given on the homework
    assignment
   Instructor is available for one to one
    discussions with students in the event of
    difficulties




                                                11
Session1:
Getting Started

    MATLAB fundamentals and
    programming techniques
What is Matlab

   MATLAB® is a high-performance language
    for technical computing.
    It integrates computation, visualization, and
    programming in an easy-to-use environment
    where problems and solutions are expressed
    in familiar mathematical notation.




                                                 13
Typical uses for Matlab

   Math and computation
   Algorithm development
   Data acquisition
   Modeling, simulation, and prototyping
   Data analysis, exploration, and visualization
   Scientific and engineering graphics
   Application development, including graphical
    user interface building

                                                    14
More about Matlab

   MatLab is an interactive system whose basic data
    element is an array that does not require
    dimensioning.
   The name MatLab stands for MATrix LABoratory
   MatLab features a family of add-on application-
    specific solutions called toolboxes
   Toolboxes are comprehensive collections of MatLab
    functions (M-files) that extend the MatLab
    environment to solve particular classes of problems


                                                      15
How to start and exit Matlab

   On a Microsoft Windows platform, to start MATLAB,
    double-click the MATLAB shortcut         icon on
        your Windows desktop.
   After starting MATLAB, the MATLAB desktop opens

   Note the >> is the matlab command prompt
   To end your MATLAB session, select Exit MATLAB
    from the File menu in the desktop, or type quit in the
    Command Window.
   Note that nothing is saved when you exit, you will
    not be prompted to save
                                                         16
MATLAB Desktop

   When you start MATLAB, the MATLAB
    desktop appears, containing tools (graphical
    user interfaces) for managing files, variables,
    and applications associated with MATLAB.
   The first time MATLAB starts, the desktop
    appears as shown in the following illustration,
    although your Launch Pad may contain
    different entries.


                                                  17
MATLAB Desktop




                 18
MATLAB Desktop, Cont.

    When launching Matlab, version 6 brings up a desktop with pull-
    down menus and various windows. These windows are:
   Command Window: This is the main window for issuing commands
    and seeing results, and is what has been used in this class up to
    now.
   Command History: An ordered list of all commands issued in the
    Command Window.
   Current Directory: The files in the user directory currently available
    for use in the Command Window.
   Workspace: a list of variables that have been used in the Command
    Window.
   Launch Pad: a variety of packages that may be available with
    Matlab. We won't consider this window further.

                                                                        19
Using Matlab help

   Information about Matlab commands can be
    found either by issuing the help command, or
    by using the Help Window.
   Just typing help brings up a list of packages
    containing functions and symbols:
   >> help
   HELP topics:
   matlab/general     - General purpose commands.
   matlab/lang      - Programming language constructs.
   matlab/elmat      - Elementary matrices and matrix manipulation.
   matlab/elfun     - Elementary math functions.
   matlab/specfun      - Specialized math functions, etc.


                                                                       20
Using Matlab help, Cont.

   We could then look at the elfun package,
    which contains some elementary functions.
   >> help elfun
     Elementary math functions.
     Trigonometric.
          sin     - Sine.
         sinh     - Hyperbolic sine.
         asin     - Inverse sine.
         asinh     - Inverse hyperbolic sine.
         cos      - Cosine.
         cosh      - Hyperbolic cosine.
         acos      - Inverse cosine, etc.


                                                21
Help Window

   You can also use the Help Window, activated
    from the desktop. For example, let's look up
    information on the Matlab’s functions by
    category. (see next slide)




                                               22
23
Help Window, Cont.

   From the tabs in the Help Navigator, you can use an
    index, or search for keywords or explicit function names
    (e.g. atan) or concepts.
    The description of the arctangent function is much more
    detailed than typing help atan, and includes graphs and
    examples in a nice layout.
   You can also use the helpwin command to display the
    above help text inside the deskptop Help Window:
         helpwin atan
   The command doc goes directly to the help text above,
    without the extra step involved in helpwin to click on a
    link: doc atan

                                                           24
Running demos


   Click Help -> Demos
   Then in new window, select Matlab, then
    Desktop Environment
   Then select Desktop overview
   Run the demo (it will run in a browser)



                                              25
Matlab Basics. What Matlab operates on?

   MATLAB works with scalars, vectors and
    matrices.
   A scalar is just a number, a 1x1 matrix.
   A vector is a list of numbers, effectively a
    matrix, given as either a row or a column.
   In this sense, everything that MATLAB
    operates on is a matrix.
   The best way to get started with MATLAB is
    to learn how to handle matrices.

                                                   26
Entering Matrices

        We can enter matrices into MATLAB in several
         different ways:
    1.    Enter an explicit list of elements.
    2.    Load matrices from external data files.
    3.    Generate matrices using built-in functions.
    4.    Create matrices with your own functions in M-files.
         We have only to follow a few basic
         conventions:
    1.    Separate the elements of a row with blanks or commas.
    2.    Use a semicolon, ; , to indicate the end of each row.
    3.    Surround the entire list of elements with square brackets, [ ].


                                                                            27
Matlab Statements and Variables

   MATLAB is an expression language. It
    interprets and evaluates expressions typed in
    the command window at the keyboard.
   You are allowed to assign a name to an
    expression.
   Statements are usually in the form of
        variable = expression,
        e.g. A = magic(4)

                                                28
  Syntax - symbols and punctuation

       Try these examples
Input         Output       Comments

2+3           ans = 5
7-5           ans = 2
                           Arithmetic works as expected.
34*212        ans = 7208
1234/5786     ans = 0.2173 Note that the result is given the name "ans" each time.
2^5           ans = 32
a = sqrt(2)   a = 1.4142   You can choose your own names for things.




                                                                                 29
                    b = 1.4142
                                           You can use commas to put more
                    ans = 3.1416
b = a, pi, 2 + 3i                          than one command on a line. Pi, i,
                    ans = 2.0000 +
                                           and j are contants.
                    3.0000i
                                           "eps" is the current limit of
                                           precision. Anything smaller than
c = sin(pi)         c = 1.2246e-016        eps is probably zero. Note that
eps                 ans = 2.2204e-016      Matlab understands (and expects
                                           you to understand!) scientific
                                           notation.
d=                                         "d", "e", and "f" are all vectors.
[1 2 3 4 5 6 7 8 9 d = 1 2 3 4 5 6 7 8 9   They are equal. Note the use of
]                  e=123456789             the ":" operator - it counts (by
e = [1:9]          f=123456789             ones) from one number to the
f = 1:9                                    next.
g = 0:2:10          g = 0 2 4 6 8 10       More uses of the colon. Note that
f(3)                ans = 3                you can use it to get slices of a
f(2:7)              ans = 2 3 4 5 6 7      vector (or matrix, or cube, etc), or
f(:)                123456789              get the whole thing.


                                                                                  30
                              A semi-colon ";" will prevent the output
h = [1 2 3];    (nothing)
                              from being displayed. A single quote " '
h'              ans = 1
                              " computes the transpose of a matrix,
                      2
                              or in this case, switches between row
                      3
                              and column vectors.

                              Operations on vectors. * is matrix
h * h'          ans = 14
                              multiplication, and so the dimensions
h .* h          ans = 1 4 9
                              must line up correctly.
h + h           ans = 2 6 8
                              " .* " is entry-by-entry multiplication.
                g = 1 2 3
g = [ 1 2 3;
                    4 5 6     Entering a matrix.
4 5 6; 7 8 9]
                    7 8 9
g(2,3)          ans = 6
g(3,:)          ans = 7 8 9
                              Accessing matrix elements.
g(2,3) = 4      g = 1 2 3
                              Note use of ":" to access an entire row.
                    4 5 4
                    7 8 9


                                                                         31
Input    Output                 Comments

         ans = 30 36 42
g^2
               66 81 96
              102 126 150 The first multiplies the matrix by itself.
         ans = 1 4 9      The second squares each entry in the
g .^ 2
              16 25 36    matrix.
              49 64 81




                                                                       32
Control of output

   The main command to know is format
       To control linespacing, use format compact
       To see all 15 digits that were used in calculation,
        use format long
       To see just 5 digits, use format short
       To suppress output completely, use a semi-colon
        at the end of the command.
   For more information, type help format


                                                              33
Working with Scalars

   A scalar is just a number
   Matlab stores them as 1x1 matrices
   All operations involving a scalar and a matrix
    are entry-by-entry, with on exception:
       The power (“^”) operator




                                                     34
Working with Scalars, Cont.

   Try these examples
    Input    Output        Comments

    b=2      b=2           Define b to be a scalar.
             ans = 3 4
    a + b                  Addition works entry-by-entry.
                   5 6
             ans = 2 4
    a * b                  So does multiplication.
                   6 8
             ans = 7 10
    a ^ b                  This is matrix power - a*a
                   15 22
             ans = 1 4
    a .^ b                 Entry-by-entry power.
                   9 16


                                                            35
Vectors

    A vector is just a matrix with only one row or
     column
    Input         Output          Comments
    v = [1 2 3]   v = 1 2 3
                                  Define a pair of vectors.
    u = [3 2 1]   u = 3 2 1

    v * u         Error           The dimensions don't agree.

    v * u'        ans = 10        Taking the transpose works.

    dot(v,u)      ans = 10        The dot product is the same thing.

                                  The cross product works only for 3-d
    cross(v,u)    ans = -4 8 -4
                                  vectors


                                                                       36
Matrix Operations Synopsis

   +, -, *, and / are defined in an intuitive
    manner for matrices
   “ ‘ “ (transposition) turns a row vector into a
    column vector
   “.*” (dot-star) will multiply entry-by-entry
   “*” will do matrix multiplication. More
    precisely,             n
                 C (i, j )   A(i, k ) * B(k , j )
                            k 1




                                                      37
Transposing Matrices

   The special character “ ‘ “ (prime or apostrophe)
    denotes the transposition of the matrix. The
    statements
   A = [1 2 3; 4 5 6; 7 8 0];    B = A';
    Result in
   A=                              B=
     1 2 3                                1 4 7
     4 5 6                                2 5 8
     7 8 0                                3 6 0



                                                        38
Matrix-Vector Product

   Matrix-Vector Product is a special case of
    general matrix-matrix product.
   Let us
    A = [1 2 3; 4 5 6; 7 8 0]; x = [-1 0 2]’;
   b = A*x results in the output
    b=
        5
        8
       -7

                                                 39
Using Powers with Matrices

   A^p raises A to p-th power and is defined if A is a
    square matrix and p is a scalar.
   If p is an integer greater than 1, the power is
    computed by repeated multiplication.
   For other values of p, the calculation involves
    eigenvalues (D) and eigenvectors (V):
    if          [V,D] = eig(A), then
     then       A^p = V*D.^p/V
   X^P, where both X and P a matrices, is an error.


                                                          40
Matrix built-in operations

   For more, type help matfun

Input           Output           Comments
k = [16 2 3;    k = 16 2 3
     5 11 10;        5 11 10     Define a matrix.
     9 7 6]          9 7 6
rank(k)         ans = 3          The rank.
det(k)          ans = -136       The determinant.




                                               41
Matrix built-in operations, Cont.
Input         Output                            Comments
              ans =     0.0294 -0.0662 0.0956
                                                Inverse of the
inv(k)                 -0.4412 -0.5074 1.0662
                                                matrix
                        0.4706 0.6912 -1.2206
                                                Eigenvectors
                                                and eigenvalues
              vec = -0.4712 -0.4975 -0.0621     of the matrix.
                    -0.6884 0.8282 -0.6379      The columns of
[vec,val] =         -0.5514 0.2581 0.7676       "vec" are the
eig(k)        val = 22.4319       0       0     eigenvectors,
                          0 11.1136       0     and the diagonal
                          0       0 -0.5455     entries of "val"
                                                are the
                                                eigenvaules




                                                                 42
Matrix built-in constructions

   For more, type help func_name
Input       Output                         Comments
rand(2)     ans = 0.9501   0.6068
                                           Generates a matrix with
                  0.2311   0.4860
rand(2,3)   ans = 0.8913   0.4565 0.8214   entries randomly distributed
                  0.7621   0.0185 0.4447   between 0 and 1
zeros(2)    ans = 0   0
                  0   0                    Generates a 2x2 matrix with
ones(2)     ans = 1   1                    all zero (or all ones) entries.
                  1   1
eye(2)      ans = 1 0
                  0 1                      Identity matrix I.
hilb(3)     ans = 1.0000 0.5000 0.3333
                  0.5000 0.3333 0.2500     3x3 Hilbert matrix.
                  0.3333 0.2500 0.2000


                                                                         43
Concatenating Matrices

   New matrices may be formed out of old ones
       Suppose we have:

    a = [1 2; 3 4]
    a = 1 2
        3 4




                                                 44
Deleting rows and columns
   You can delete rows and columns from a matrix
    using just a pair of square brackets.
   Start with       X = [1 2 3; 4 5 6; 7 8 0]
    Then, to delete the second column of X, use
                X(:,2) = [ ];
    Thus,       X= [1 3
                        4 6
                        7 0]
   If you delete a single element from a matrix, the
    result isn't a matrix anymore.
    So, expressions like
         X(1,2) = [ ], result in an error.
                                                        45
Concatenating Matrices, Cont.

  Input                         Output
  [a, a, a]                     ans = 1 2 1 2 1 2
                                      3 4 3 4 3 4
  [a; a; a]                     ans = 1   2
                                      3   4
                                      1   2
                                      3   4
                                      1   2
                                      3   4
  [a, zeros(2); zeros(2), a']   ans = 1   2   0   0
                                      3   4   0   0
                                      0   0   1   3
                                      0   0   2   4



                                                      46
M - files
   You can create your own matrices using M-files,
    which are text files containing MATLAB code.
    Use the MATLAB Editor or another text editor to
    create a file containing the same statements you
    would type at the MATLAB command line. Save the
    file under a name that ends in .m.
   For example, create a file containing these two lines.
    A = [ 16.0 3.0 2.0 13.0; 5.0 10.0 11.0 8.0; …
           9.0 6.0 7.0 12.0; 4.0 15.0 14.0 1.0 ];
   Store the file under the name magik.m. Then the
    statement magik reads the file and creates variable,
    A, containing our example matrix.

                                                         47
Solving System of Linear Equations

   One of the main uses of matrices is in representing
    systems of linear equations.
    If a is a matrix containing the coefficients of a
    system of linear equations, x is a column vector
    containing the "unknowns," and b is the column
    vector of "right-hand sides," the constant terms, then
    the matrix equation

                    a x =b

    represents the system of equations


                                                         48
Solving equations, Cont.

    MATLAB uses the division terminology
    familiar in the scalar case to describe the
    solution of a general system of simultaneous
    equations.
    The two division symbols, slash, /, and
    backslash, \, are used for the two situations
    where the unknown matrix appears on the left
    or right of the coefficient matrix.


                                                49
Solving equations, Cont.
   X = A\B denotes the solution to the matrix equation
              AX = B.
   X = B/A denotes the solution to the matrix equation
              XA = B.
   The dimension compatibility conditions for X = A\B
    require the two matrices A and B to have the same
    number of rows.
    The solution X then has the same number of
    columns as B and its row dimension is equal to the
    column dimension of A.
    For X = B/A, the roles of rows and columns are
    interchanged.

                                                          50
Solving equations, Cont.

        In practice, linear equations of the form AX = B
         occur more frequently than those of the form XA =
         B. Consequently, backslash is used far more
         frequently than slash.
        The coefficient matrix A need not be square. If A is
         m-by-n, there are three cases.
    1.    m = n, Square system. Seek an exact solution.
    2.    m > n, Overdetermined system. Find a least squares
          solution.
    3.    m < n, Underdetermined system. Find a basic solution
          with at most m nonzero components.

                                                                 51
    Solving equations, an Example

   To solve the equation a x =b in matlab simply type
       x = a \ b
       Which reads “x equals a-inverse times b”
   Try it with
       a = [1 2 3; 4 5 6; 7 8 10]; b = [1 1 1]';
   You should get
       x =
              -1
               1
               0


                                                         52
Solving equations, an Example, Cont.

   To verify this assertion, try this:
    a*x, a*x - b, eps
    The results are:
    ans = 1 1 1
    ans = 1.0e-015 *
      -0.1110
      -0.6661
      -0.2220
    ans = 2.2204e-016
   Notice that a*x - b is very close to eps - which
    means that it is as close to zero as possible.

                                                       53
Solving equations, an Example, Cont.

   If there is no solution, a "least-squares" solution is
    provided (a*x - b is as small as possible). Enter
    a(3,3) = 9; b = [1 1 0]';
    (which makes the matrix singular and changes b)
    and try to solve the equation again.
    Notice that the solution is quite inaccurate.




                                                       54
Saving and loading matrices

   When you exit matlab you will not be
    prompted to save
   You can turn on logging
       diary '~/session.txt‘
       this will save output and input together and
        therefore can not be used as a script
   You may just want to save one or more
    matrices
       save x.value x -ascii

                                                       55
Saving and loading matrices

   To save all variables in a file named
    “mysession.mat” in a reloadable format
       save mysession
   To restore the session, use
       load mysession
   The saved files are in text format and can be
    viewed using any text editor



                                                    56

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:2/1/2012
language:
pages:56