LESSON

# Creating an image from code

#### Share

#### Transcript

We have talked quite a bit about how an image is represented as a matrix inside a MATLAB. Conversely, a matrix that we create using MATLAB tools can be displayed as an image. So what we are going to do now is use some of MATLAB’s fundamental image creation tools to produce some test images. So we are going to create a new image purely through code.

So let’s start off by assigning to a workspace variable im, my standard name for an image. I am going to use the function zeros. And what that does is create a matrix with dimensions 200 by 200 and all the elements in that matrix are zeroes. And I am going to set one element of that to the value of 1. Now I can display this matrix that I just created as an image and what we can see is this largely black, because all the pixels are zero, which represents black and up in the top left-most corner you can see a tiny little patch of white. That is the one pixel that I set to the value of 1—1 representing white.

Now, I can create a slightly bigger object in this and what I am going to do is, within this matrix, I am going to set all the pixels from row 150 to row 160, and all the columns from column 60 to column 70, and I am going to set those to a value of 0.5. So that is a grey, half way between black and white. And I am going to set all those elements of the matrix to the value of 0.5, now I can display that matrix again and we see we have created a small grey square in the image.

So we are creating an image simply by writing code.

The toolbox contains a function to create circles. I am going to do that, I am going to call the circle function, it is called kcircle. And I am going to say I want a circle of radius 30—we see that it has created a matrix of dimensions 61 by 61. Let’s have a look at this thing that we just created. We can see here is the image representation of that matrix. We can explore it using idisp, and we see that all these pixels in here, these matrix elements, have got a value of 1. And all these ones which don’t lie in the circle have got a value of 0.

So this function has returned a matrix which is square and the elements in that are either 0 if they don’t belong inside the circle and white if they do belong inside the circle.

Now what we can do now is actually paste this circle into the image that we had before, so let’s just go back to the image that we had a moment ago.

And I am going to paste the circle into that image and I do it like this. I am going to make the circle, not bright white, I am going to make it a slightly brighter grey then the square was. So I am going to multiply all of the elements in the circle by the value of 0.7, so it will have a 70% grey value. And I am going to place the circle with its centre at 100 by 30. So that is the u coordinate of a 100 and the v coordinate of 30.

All I have done now is updated the elements in the matrix im. The value that I had before and now it has got a circle pasted into it. So let’s have a look at that.

And we see that the image now contains a square and a circle. Toolbox has got some other functions for creating graphical entities within a matrix. Another function is iline and that draws a line into a matrix. So I am going to draw a line into the matix im.

The line is going to start at coordinate 30 by 40. And it is going to go to 150 by 190, and I’m going to make it have a value of 90% grey.

Let’s display that. And there we have a matrix which now contains a square, a line, a circle and a single bright pixel.

Toolbox has got some functions to create slightly more elaborate test patterns, accessed through the test pattern function. And here I am going to create a ramp function and I am going to create an image of dimensions 200 by 200 pixels.

And let’s have a look at that.

And here we have what is called a grey scale ramp. Let’s have a look at the grey scale values in here. They are quite low, about 0.5 and as we move horizontally it gets brighter and brighter all the way up to 1. So if it is a grey scale ramp pattern we can create more elaborate test patterns such as grid of squares or circles.

Here is an example. We assign it to an output variable im, use the test pattern function. Ask for squares. We want the output image to be 200 pixels by 200 pixels. We want the squares to be on a 50 pixel spacing and we want the squares to be 25 pixels square. Let’s display that image. And there we have a grid of 16 squares.

Since an image in MATLAB is just a matrix of numbers, we could write code to fill in the elements of the matrix. Let’s look at some simple examples such as squares, circles and lines and more complex images formed by pasting these shapes together.

### Skill level

MATLAB experience

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.