MASTERCLASS

# Image processing

### Lessons

#### Transcript

Imagine now that we want to try and implement some of these monadic operations, like adding a constant to every pixel, or to multiply every pixel by a constant.

One way to do this would be to write some code with some nested loops that step through every element in the matrix and performs the monadic operation on them.

In MATLAB there is an easy way to do it and it is a technique in MATLAB that is referred to as vectorization.

Consider that we have a matrix with n rows and m columns and we have a scalar. And in in the middle we have a number of standard arithmetic operators; plus, minus, times, divide and so on.
If, for instance, we use the addition operator—plus symbol—and we add the matrix to a scalar, the result will be a matrix. And what MATLAB is going to do is to add the scalar to every individual element of the input matrix and assign it to the corresponding element in the output matrix.

So we do not have to write loops to go through and add the scalar to every element of the input matrix. MATLAB will do it for us, and we use the short hand notation of a matrix operator and a scalar. So we can add a constant to every element matrix, subtract the constant; multiply; divide; raise every element to a power; and so on.

We are going to demonstrate this now with a real example. We will start by creating a matrix and call it m, and its elements will all be integers, integers between 1 and 10. And the matrix will be 4 by 4.

We are going to use MATLAB’s built in randi function to create this matrix with random integer elements in it.

Now I am going to add 1 to that matrix, or adding a matrix—four by four matrix—to a scalar, and what MATLAB is going to do is add 1 to every element of the matrix.

Similarly, if I multiply the matrix by 2, we will see that every individual element in the matrix m has been multiplied by 2.

We can perform more complex mathematical functions in a similar way. We can compute the square root of m, and what MATLAB is going to do is to compute the square root of every element within the matrix.

Finally, we can raise each element of the matrix to the power of two, we use the operator .^ and what we see here is that every element in the matrix m has been squared.

Important to note that this is very different to writing this, and what this has done is to multiply the matrix m by itself; it is the same as writing that. This is multiplying a matrix by a matrix, using matrix multiplication rules—very different to raising each element of the matrix to the power of 2.

#### Code

There is no code in this lesson.

Images contain many pixels and the normal way to process them is with nested for loops that index each pixel in turn. This is slow and somewhat cumbersome to write. MATLAB has a facility called vectorization that allows us to perform complex matrix operations without any loops.

### Professor Peter Corke

Professor of Robotic Vision at QUT and Director of the Australian Centre for Robotic Vision (ACRV). Peter is also a Fellow of the IEEE, a senior Fellow of the Higher Education Academy, and on the editorial board of several robotics research journals.

### Skill level

This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.