MASTERCLASS

# Spatial operators

### Lessons

#### Share

#### Transcript

Let’s look now at what we call the edge problem. If I try and compute the pixel value at this particular coordinate the window of pixels in the input image has to look like this. And the problem is that some of the input window has fallen off the edge of the image. So there are pixel values that are undefined: the window requires pixel values that do not exist in the input image. There’s a couple of solutions to this: one solution is we simply don’t compute the output value whenever the window falls off the edge. That would be all the pixels that are indicated, hashed out in the right-hand image. We just wouldn’t be able to compute those.

Another option is to assume that the image is surrounded by pixels that are all set to zero. If we put a layer of zero pixels all around the edge of the image then we can determine a valid window and from the valid window we can compute our function. The problem is that the zeros are somewhat artificial and they will, of course, influence the result returned by the function. Another technique is to replicate the edge pixels. Make copies of those pixels around the edge; stack them around the image so that we always have valid pixel values going into the window.

All of these options have different advantages and disadvantages; just be aware of the problem when you are performing spatial operations on images and in some environments there may be options you can pass into the software to tell it how to handle this boundary condition.

We can see the effect of the window falling off the edge of the input image as a dark border around the edge of our output image. Let’s have a look at this image we computed a short time ago. If we zoom in on the edge here, we can see quite a rapid drop off in grey level as we approach the edge of the image, and this is due to the neighbourhood window falling off the edge of the input image.

Another thing to be aware of is that the window size is always odd—it is a square window and the width and the height are always odd numbers. The reason for this is the following: the window is square and it is always centred on the input pixel. Let’s say that the edge most pixel is H pixels away from the centre. In this particular case, H would be equal to 2. Move from the middle pixel, we step two pixels to the right and we are at a pixel which is on the edge of the window. So that means, then, that the window width is 2H + 1. H is an integer; 2H + 1 is always an odd number.

#### Code

We run into problems when we take all of the pixels in a box around an input pixel and that pixel is close to one of the edges of the image. Let’s look at some strategies to deal with edge pixels.

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