Lab 2 (Week 3) - Introduction to Matrices & MATLAB PDF

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

The British University in Egypt

null

Tags

matrices matrix operations image processing MATLAB

Summary

This document details matrix operations such as addition, subtraction, and scalar multiplication. It also introduces the concept of matrix multiplication and how to perform it. The document is part of a course on image processing and is likely part of an undergraduate program.

Full Transcript

Introduction to Image Processing (24CSCI23H) Week 3 Lab 2: Introduction to matrices & MATLAB Matrices operations Recap: Describing Matrices A matrix is often referred to by its size or dimensions. m × n indicating m ro...

Introduction to Image Processing (24CSCI23H) Week 3 Lab 2: Introduction to matrices & MATLAB Matrices operations Recap: Describing Matrices A matrix is often referred to by its size or dimensions. m × n indicating m rows and n columns. Matrix entries are defined first by row and then by column. For example, to locate the entry in matrix A identified as aij we look for the entry in 𝑟𝑜𝑤i , columnj. In the matrix shown below, the entry in row 2, column 3 is a23. 𝑎11 𝑎12 𝑎13 𝐴 = [𝑎21 𝑎22 𝑎23 ] 𝑎31 𝑎32 𝑎33 A matrix is a rectangular array of numbers that is usually named by a capital letter: 𝐴, 𝐵, 𝐶, and so on. Each entry in a matrix is referred to as 𝑎𝑖𝑗 , such that represents the row and represents the column. Matrices are often referred to by their dimensions: 𝑚 × 𝑛 indicating 𝑚 rows and 𝑛 columns. Example 1: Given matrix find the dimensions of the given matrix and locating entries: 2 1 0 𝐴 = [2 4 7 ] 3 1 −2 1. What are the dimensions of matrix 𝐴 2. What are the entries at and 𝑎31 and 𝑎22 Solution: 1. The dimensions are 3 × 3 because there are 3 rows and 3 columns. 2. Entry a31 is the number at row 3, column 1, which is 3. The entry a22 is the number at row 2, column 2, which is 4. Remember, the row comes first, then the column. N.B.: o A square matrix is a matrix with dimensions meaning that it has the same number of rows as columns. The matrix above is an example of a square matrix. o A row matrix is a matrix consisting of one row with dimensions 1 × 𝑛 o A column matrix is a matrix consisting of one column with dimensions 𝑚 × 1 1 Introduction to Image Processing (24CSCI23H) A. Addition & Subtraction We add or subtract matrices by adding or subtracting corresponding entries. In order to do this, the entries must correspond. Therefore, addition and subtraction of matrices is only possible when the matrices have the same dimensions. We can add or subtract a 3 × 3 matrix and another 3 × 3 matrix, but we cannot add or subtract a 2 × 3 matrix and a 3 × 3 matrix because some entries in one matrix will not have a corresponding entry in the other matrix. Example 2: Find 𝐴 + 𝐵 𝑎𝑛𝑑 𝐴 − 𝐵, given 4 1 5 9 𝐴=[ ] 𝐵=[ ] 3 2 0 7 Solution: 9 10 𝐴+𝐵 =[ ] 3 9 −1 −8 𝐴−𝐵 =[ ] 3 −5 N.B.: o 𝑨−𝑩≠ 𝑩−𝑨 B. Scalar Multiplication Example 3 : Multiply matrix 𝐴 by the scalar 3. 8 1 A=[ ] 5 4 Solution: 3×8 3×1 24 3 3𝐴 = 3 ∗ 𝐴 = [ ]=[ ] 3×5 3×4 15 12 C. Matrix multiplication In addition to multiplying a matrix by a scalar, we can multiply two matrices. Finding the product of two matrices is only possible when the inner dimensions are the same, meaning that the number of columns of the first matrix is equal to the number of rows of the second matrix. If A is an m × r matrix and B is an r × n matrix, then the product matrix AB is an m × n matrix. For example, the product AB is possible because the number of columns in A is the same as the number of rows in B. If the inner dimensions do not match, the product is not defined. We multiply entries of A with entries of B according to a specific pattern as outlined below. The process of matrix multiplication becomes clearer when working a problem with real numbers. 2 Introduction to Image Processing (24CSCI23H) Question: Is it possible for AB to be defined but not BA? Answer: Yes, consider a matrix A with dimension 3 × 4 and matrix B with dimension 4 × 2. For the product AB the inner dimensions are 4 and the product is defined, but for the product 𝐵𝐴 the inner dimensions are 2 and 3 so the product is undefined. Example 4: Multiply matrix A and matrix B. 1 2 5 6 A=[ ] 𝐵=[ ] 3 4 7 8 Solution: (1 ∗ 5) + (2 ∗ 7) (1 ∗ 6) + (2 ∗ 8) 19 22 A∗B=[ ]=[ ] (3 ∗ 5) + (4 ∗ 7) (3 ∗ 6) + (4 ∗ 8) 43 50 D. Using MATLAB: o First, apply all the above operations to get the same results o Then find the commands for: ▪ Transpose ▪ Determinant ▪ Inverse of matrix 3 Introduction to Image Processing (24CSCI23H) An image may be defined as a two-dimensional function 𝑓 (𝑥, 𝑦), where x and y are spatial (plane) coordinates, and the amplitude of 𝑓 at any pair of coordinates is called the intensity of the image at that point A digital image can be represented as a MATLAB matrix and Each element of this Matrix is called an image element, picture element or a pixel The terms image and pixel are used throughout the rest of our tutorials to denote a digital image and its elements. A. Matrices in MATLAB 1. Creating Matrices: o To create an array with four elements in a single row, separate the elements with either a comma (,) or a space. A=[1 2 3 4] A = 1 2 3 4 o To create a matrix that has multiple rows, separate the rows with semicolons. A=[1 2 ;3 4;5 6] A = 1 2 3 4 5 6 o Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1 column vector of zeros. Z=zeros(5,1) Z = 0 0 0 0 0 4 Introduction to Image Processing (24CSCI23H) Try at Home ones(), eye(), and rand() functions. 2. Matrix Operations: o To add a constant to matrix entries A+10 ans = 11 12 13 14 15 16 o To transpose a matrix, use a single quote ('): A' ans = 1 3 5 2 4 6 o You can perform standard matrix multiplication, which computes the inner products between rows and columns, using the * operator. For example, confirm that a matrix times its inverse returns the identity matrix: Case 1: P=A*inv(A) error: inverse: A must be a square matrix Case 2: A=[1 2 3; 4 5 6; 8 9 10]; P=A*inv(A) warning: matrix singular to machine precision P = Inf Inf Inf Inf Inf Inf Inf Inf Inf Case 3: A=[1 2 3; 4 5 6; 7 8 10]; P=A*inv(A) P = 1.0000 0 0.0000 0 1.0000 -0.0000 0.0000 0 1.0000 5 Introduction to Image Processing (24CSCI23H) o To perform element-wise multiplication rather than matrix multiplication, use the.* operator: P=A.*A P = 1 4 9 16 25 36 49 64 100 o The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power: A.^3 ans = 1 8 27 64 125 216 343 512 1000 Note the difference A^3 ans = 489 600 756 1104 1353 1704 1828 2240 2821 o Concatenation is the process of joining arrays to make larger ones. In fact, you made your first array by concatenating its individual elements. The pair of square brackets [] is the concatenation operator. [A A] ans = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 10 7 8 10 6 Introduction to Image Processing (24CSCI23H) o Concatenating arrays next to one another using commas is called horizontal concatenation. Each array must have the same number of rows. Similarly, when the arrays have the same number of columns, you can concatenate vertically using semicolons. [A;A] ans = 1 2 3 4 5 6 7 8 10 1 2 3 4 5 6 o 7 8 10 You can rotate the matrix by 90 degrees by using rot90(A) o Concatenate arrays along specified dimension C = cat(dim, A, B)concatenates the arrays A and B along array the dimension specified by dim. The dim argument must be a real, positive, integer value. 3. Matrix Indexing: o When you want to access selected elements of an array, use indexing. For example, consider Matrix A: A=[ 16 2 3 13; 5 11 10 8;9 7 6 12;4 14 15 1] A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 o There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as A(4,2) ans = 14 o Less common, but sometimes useful, is to use a single subscript that traverses down each column in order: A(8) ans = 14 7 Introduction to Image Processing (24CSCI23H) o You can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers. A(4,5)=17 A = 16 2 3 13 0 5 11 10 8 0 9 7 6 12 0 4 14 15 1 17 o To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of A: A(1:3,2) ans = 2 11 7 o The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A: A(3,:) ans = 9 7 6 12 0 For The Next Lab B. Images in MATLAB: o RGB images: An RGB image, sometimes referred to as a truecolor image, is stored as an m-by-n- by-3 data array that defines red, green, and blue color components for each individual pixel. 8 Introduction to Image Processing (24CSCI23H) o Indexed image consists of a data matrix, X, and a colormap matrix, map. map is an m-by-3 array of class double containing floating-point values in the range [0, 1]. Each row of map specifies the red, green, and blue components of a single color. An indexed image uses "direct mapping" of pixel values to colormap values o Grayscale Image: an image in which the value of each pixel is a single sample, that is, it carries only intensity information. o Read a sample image A = imread('ngc6543a.jpg'); o Display an image imshow('ngc6543a.jpg'); o Write image to graphics file imwrite( A , filename ); o Get image information from graphics file imfinfo('ngc6543a.jpg'); C. Exercise Problems: Problem 1: Extract Red, Green and Blue planes from an RGB Image Problem 2: Convert an RGB image to Grayscale image using averaging technique. Problem 3: Create a function that resizes an image. 9

Use Quizgecko on...
Browser
Browser