MASTERCLASS

# Spatial operators

### Lessons

#### Share

#### Transcript

In this section we are going to consider another spatial image processing operation, goes by the rather confronting title of mathematical morphology. Now the word morphology means shape, so this is a type of spatial image processing operation that is sensitive to shape. The output image contains shapes that are compatible with a structuring element S. Now the structuring element is a pattern which represents a subset of pixels within the input window that we wish to consider.

An example will make that clear: we have a contrived example here, simple binary scene, black pixels and white pixels that contain squares and rectangles of different sizes. As in previous animations, we see the input window shown here in red marching across the image left to right and top to bottom. What is different in this case is that the output is only true if all of the pixels underneath the red window are true. So far none have been. In a moment we will see… this one particular case now… where the output image contains one pixel. That is where all of the pixels underneath the red marching window were all set. You will notice that most of the shapes that were in the input image have disappeared, the rectangles have all disappeared, the little square has disappeared; the only shape that was retained was the big square. Note that the big square was the same shape as the red marching window. We say that only shapes that are compatible with the structuring element — a 3 x 3 square —are retained. This operation is also referred to as erosion, because what has happened here is that our 3 x 3 square, even though it is compatible, has been eroded away … pixels have been chewed off its edges until only the centre pixel is left. This operation is often called erosion and this function can be computed using the tool box function ierode; the first argument is the image and the second argument is the structuring element. In this case it is a 3 x 3 matrix containing ones.

So we can perform the converse operation, which is called dilation, and again we show this in animation. Here is the 3 x 3 marching window, but now for dilation, the output is true if any pixel underneath the red square is true. So far none have been. Now we see that there are a bunch of pixels are going to be set as the red window marches across them and encounters a one pixel. This operation is called dilation for the obvious reason that it has taken a single pixel, dilated it, grown it into a 3 x 3 shape; in fact, it is back to the shape that it was originally. This function, this dilation function, can be computed using tool box function idilate; the first argument is the input image and the second argument is the structuring element.

So this was the original image that we had and we eroded it to a single pixel and then we dilated it so that the single pixel became a block of 3 x 3 pixels. So you can see what has happened here, we have taken an original image, we have eroded it, dilated it and the only shape that made it all the way through was the 3 x 3 square. The rectangles have disappeared and the little square has disappeared and that is because only the shapes that are compatible with the structuring element have been maintained. Now this operation, this sequence of erosion followed by dilation, is often called opening. Only the compatible shapes have remained and we can do this in the tool box using the function iopen.

Let’s try doing things in the opposite order. Let’s dilate first and then erode, so in the dilation step we can animate this, and for dilation output pixels are true if any pixel underneath the marching window is true and as we would expect for dilation the shape is going to get bigger. But another interesting benefit of this dilation step is that holes within the shape are going to be filled in and it has got bigger and holes within it have been closed up.

Now we have the dilated image, now we can erode it. So erosion is going to chew off a strip of one pixels all around the outside of this particular object; it is going to restore it to something like its original size and shape.

Now let’s look at the original image. Then its dilated version. And then its eroded version. And we can see we have something that looks like the original shape, except the hole within it has been filled in. This operation is called a closing operation. It tends to close those holes within an object. So any hole that is smaller than the size of the structuring element — which is a 3 x 3 pixel block — will be filled in.

This closing operation can be computed using the tool box function iclose. So this brings us to the notion of compatibility between a structuring element and a shape. So here a structuring element is a red square and I am going to test it against a large square and a thin rectangle.

So first of all we are going to test the large square and basically it is saying that if I can fit the structuring element within that shape it will survive in an erosion operation or an opening operation, but for the tall thin rectangle there is nowhere within that shape that the structuring element will fit. So this shape will not survive, it will be eliminated in the erosion step.

Conversely, if my structuring element is a tall thin rectangle we will find that there is actually nowhere that this tall thin rectangle can fit inside the large square. So if my structuring element is a tall thin rectangle, this square will disappear. However, that tall thin rectangle will survive, so the shape of the structuring element controls the shapes that will be retained in the output image; it is a filter that is sensitive to shape.

Another non-linear operation on the pixels in the box around the input pixel is to test whether they match a reference shape. This is a very powerful and useful approach to cleaning up noisy binary images known as mathematical morphology and objects in the image are treated according to their compatibility with a structuring element. We can perform operations on binary images such as erosion or dilation which make objects smaller or bigger respectively, or opening and closing which separate or merge objects respectively.

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