1 DIP Lab: Introduction to MATLAB Goal: In this lab we will become familiar with the MATLAB programming environment. You will learn the basics of MATLAB syntax and functionality. You will also write your own program to do simple manipulations to an image in MATLAB. Introduction: MATLAB is a mathematical computer package from The MathWorks. "MATLAB" stands for "Matrix Laboratory". MATLAB’s functions operate on matrices and vectors, as well as scalar numbers, so it is very useful for linear algebra applications. With it, you can perform any number of simple or complicated mathematical operations, including image processing. As a general-purpose programming language, it is easy to learn and use, but powerful enough for a wide range of science and engineering applications. The best way to learn MATLAB is by using it, which is what we will be doing in this lab and throughout the semester. The built-in and online documentation is excellent and there is also a complete set of manuals for you to use. Please keep the manuals in the lab! To access MATLAB, log in to one of the PCs and double-click on the MATLAB icon on the desktop (or the Windows Start Menu). The MATLAB environment will open. It is a window with several panes. The largest one is the command line. The others allow you to view the current directory, see what variables are in memory, etc. The menu bar at the top and the “Start” menu at the bottom of the window allow you to access many of MATLAB’s functions as well as help and tutorials. For our purposes, MATLAB is ideal since an image is basically a matrix. Remember that digital images have been discretized and are composed of pixels that can be thought of as rectangular, 2-D arrays of small dots on the screen. Each dot has a particular location associated with it, its coordinates (expressed as a pair of numbers) in the rectangular array. Each of the dots also has a value associated with it (a number “stored” at that location) that specifies the brightness (or color) of that dot. These values (DN, or digital numbers) are the elements of the matrix. The matrix has a size, m x n, which represents the number of rows (m) and the number of columns (n). Each pixel also has a size related to the quantization, e.g. how many binary digits (0’s and 1’s) it takes to record the number stored. (Note that this is different than a physical or spatial size!) For example, to record grayscale between 0 and 255 (256 total possible values, in this case, shades of gray), you need to quantize the image at 8-bit (28 = 256). Thus, if you multiply the number of bits/pixel by the total number of pixels (m x n), you can figure out how much memory or disk space your image will fill.2 Since we can treat our image as a matrix, a mathematical object, we can do all sorts of things to it. Essentially any matrix and scalar operation can be done. We can multiply, add, divide, subtract, etc. images. We can apply transformations (in shape, size, quantization, etc.). Digital images can be mathematically manipulated as we see fit. Instructions: 1. Log in and start MATLAB. The first thing you should do when starting MATLAB is to navigate to your home directory on /geobase in order to set the correct working directory. In the following, commands to type into the MATLAB command line are in this font. Terminology you should understand the specific meaning are given in italics (see below and above) A note on help: If you type help followed by the name of a command, you will get a detailed description of the command, what it does, and how to use it. Note also that MATLAB is case-sensitive. 2. Create a matrix with the following line: A = [ 1 , 2 ; 3, 4 ] Note that commas (,) separate elements and semicolons (;) separate rows. 3. Calculate the square of your matrix: Asquare = A .* A Note that using .* (as opposed to simply *) forces MATLAB to perform the operation element-by-element. Try the command with * and see what happens. You will have performed a conventional matrix multiplication. I make a habit of always using .* unless I specifically want matrix multiplication. The same holds for division (./ vs. /) and for raising to a power (.^ vs. ^). Addition (+) and subtraction (-) are always element-by-element operations. 4. As you can see, MATLAB prints the result of everything you do. This can slow things down, however, especially in a long program. You can suppress the output by placing a semicolon at the end of the line. If you type: B = [ 1 , 2 ; 3 , 4 ] ; the matrix B will not be printed. If you now type: B3 the matrix will be printed. 5. Large matrices can be easily constructed without typing in every element. Below are some examples. Try them and see what they do. (Before doing so though, you will need to define the matrix dimensions, m and n, which denote the number of rows and columns, respectively) C = ones( m , n ) D = 0.5 .* ones( m , n ) E = zeros( m , n ) 6. Diagonal matrices are also easy to create: F = diag( v , k ) Before using this command, you need to define the vector v that you want on the diagonal of F. Vector v should have at least 2 elements, and you may want to play with this value as you vary k. You also need to specify k which tells MATLAB which diagonal to use: k = 0 is the main diagonal, k = 1 is 1 spot above, k = -1 is one spot below, etc. Note that MATLAB automatically adjusts the size of F size depending on how you specify k. 7. To create vectors, you can type all the elements in manually, or use the tricks you learned in step 5. The following example will create a vector of 1’s, of m x 1 dimensions: g = 0.9 .* ones( m , 1 ) 8. The vector, g, you just made is a column vector. You can make it into a row vector by transposing it. Transposition can be done using the ' operator or with transpose command: h = g' or h = transpose( g ) This trick also works with matrices. Try it. (Note that we have been using lower case variable names for vectors and upper case for matrices.) If you use your matrix F (which you will note is still in MATLAB’s memory) you get a transposed
View Full Document