MASTERCLASS

# Spatial operators

### Lessons

#### Share

#### Transcript

In the last lecture we talked about monadic image processing, and that is where we take a pixel from the input image, apply a function to it and put the result into the corresponding pixel in the output image. And we looked at a number of functions: we looked at gamma encoding and decoding; we looked at thresholding; and so on. So the same function is applied to every single pixel in the image. What happens here is that every output pixel is a function of the pixels in a square region around the corresponding point in the input image. So the input to the function is a number of pixels, the output is a single pixel value. Such a function, which is based on a number of pixels, can then say something either about the uniformity or the variation of the pixels over that input square region. We call this a window and denote it by the symbol W.

So let’s consider an example function we might use and a common one would be to consider the average. So what we are going to do is for that square input window, take all of the pixel values, compute the average, and place the average into the corresponding pixel in the output image. So here is the original image, the Mona Lisa image that we have seen before. And the output is the average pixel value over a 7 by 7 window. So what the average can do is it can reduce noise but we also see that it reduces the resolution—it has made this image look somewhat blurry.

Let’s see how this actually works in practice; and I have constructed a toy example here where we have an input grey scale image double precision input image. So the pixel values vary between 0 and 1. So we have a grey background with values of 0.3 and a bright square in the middle where the pixel values are 0.8.

We will start the animation and what we can see here is the input pixel window, which is shown here as red and it is moving, marching across the image left to right, top to bottom. And what you can see on the right-hand side are the average values being deposited into the output image. Couple of things to note: first of all, note that we can’t compute the pixel values in the outer most row and column pixels. And we will talk about that shortly.

Also note that the quite clearly obvious bright white square in the input image has been smeared out. That value of 0.8 only exists now at a single pixel in the output image. Around that are some slightly brighter values and beyond that is the background and so we see here what we saw when we took the average of the Mona Lisa image—that the sharp, crisp, boundaries in the image have been smeared out or blurred.

Now the effect of the window size makes a real difference. So again, here is our input image and if we compute the average with a 7 by 7 window moving over the input image, this is the result, and if we increase the size of the window, now it is a 21 by 21 window. We can see that it is very blurry indeed.

So what tends to happen is that any feature in the image that is smaller than the size of the window will be very strongly attenuated. It will tend to disappear in the output image. We might say that the output image is blurry, fuzzy, has a lower resolution or whatever. We have blurred out, smoothed over fine detail in the image.

Let’s try this in a full scale image.

We are going to load our favourite image the Mona Lisa into the workspace variable im. I am going to compute a new image with the workspace variable I am going to call s7. And every output pixel is the average of a 7 by 7 neighbourhood or window of the pixels in the input image. To do that I am going to use the tool box function icon and will explain about this function a bit more as we proceed.

The first argument is the image that I am going to perform the average on which is the Mona Lisa image in the workspace variable im. The second argument defines the area over which we compute the average. Now I wanted to compute the average over a 7 by 7 window and so to specify that, I pass in a matrix with a function ones(7,7). That produces a matrix that is 7 by 7 and every element in that matrix is 1. So that second argument defines the region over which I am going to perform the average for every single input pixel.

I am going to display the original Mona Lisa image. And I am going to create another figure. I am going to display the averaged version of that and we can see that there is a lot less detail in the background. It is much smoother; you might say it is blurrier, you might say it has got lower resolution. Now let’s increase the size of the region over which we are doing the averaging. So I am going now to correlate with a window which is 21 by 21 pixels in size. Now I am going to display that image and we can see that this looks very blurry indeed. The effect of averaging over a region of pixels is to eliminate very fine detail within the image, so any detail that is of a size that is smaller than the window that we are using, in this last case, 21 by 21 pixels, any detail that is the order of 1 or 2 pixels in size is going to be eliminated.

We introduce spatial operators by a simple example of taking the average value of all pixels in a box surrounding each input pixel. The result is a blurring or smoothing of the input image.

### 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.

### Rate this lesson

## Check your understanding

## Discussion

## Leave a comment

Please Sign In to leave a comment.

Hey, just wondering whether inconv have builtin normalization?

has*

Sorry for the delay, I’ve been on holiday… Not quite sure what you mean by normalisation, but the result is ultimately computed by the MATLAB

`conv2`

function so check out its documentation. This function is rather embarrassing since it actually computes correlation not convolution, but it was written long ago when I didn’t properly understand the difference.`iconv`

is now deprecated, use`iconvolve`

instead.