# Opening

Common Names: Opening

## Brief Description

Opening and closing are two important operators from mathematical morphology. They are both derived from the fundamental operations of erosion and dilation. Like those operators they are normally applied to binary images, although there are also graylevel versions. The basic effect of an opening is somewhat like erosion in that it tends to remove some of the foreground (bright) pixels from the edges of regions of foreground pixels. However it is less destructive than erosion in general. As with other morphological operators, the exact operation is determined by a structuring element. The effect of the operator is to preserve foreground regions that have a similar shape to this structuring element, or that can completely contain the structuring element, while eliminating all other regions of foreground pixels.

## How It Works

Very simply, an opening is defined as an erosion followed by a dilation using the same structuring element for both operations. See the sections on erosion and dilation for details of the individual steps. The opening operator therefore requires two inputs: an image to be opened, and a structuring element.
Graylevel opening consists simply of a graylevel erosion followed by a graylevel dilation.
Opening is the dual of closing, i.e. opening the foreground pixels with a particular structuring element is equivalent to closing the background pixels with the same element.

## Guidelines for Use

While erosion can be used to eliminate small clumps of undesirable foreground pixels, e.g. `salt noise', quite effectively, it has the big disadvantage that it will affect all regions of foreground pixels indiscriminately. Opening gets around this by performing both an erosion and a dilation on the image. The effect of opening can be quite easily visualized. Imagine taking the structuring element and sliding it around inside each foreground region, without changing its orientation. All pixels which can be covered by the structuring element with the structuring element being entirely within the foreground region will be preserved. However, all foreground pixels which cannot be reached by the structuring element without parts of it moving out of the foreground region will be eroded away. After the opening has been carried out, the new boundaries of foreground regions will all be such that the structuring element fits inside them, and so further openings with the same element have no effect. The property is known as idempotence. The effect of an opening on a binary image using a 3×3 square structuring element is illustrated in Figure 1.

Figure 1 Effect of opening using a 3×3 square structuring element

As with erosion and dilation, it is very common to use this 3×3 structuring element. The effect in the above figure is rather subtle since the structuring element is quite compact and so it fits into the foreground boundaries quite well even before the opening operation. To increase the effect, multiple erosions are often performed with this element followed by the same number of dilations. This effectively performs an opening with a larger square structuring element.
Consider

which is a binary image containing a mixture of circles and lines. Suppose that we want to separate out the circles from the lines, so that they can be counted. Opening with a disk shaped structuring element 11 pixels in diameter gives

Some of the circles are slightly distorted, but in general, the lines have been almost completely removed while the circles remain almost completely unaffected.
The image

shows another binary image. Suppose that this time we wish to separately extract the horizontal and vertical lines. The result of an opening with a 3×9 vertically oriented structuring element is shown in

The image

shows what happens if we use a 9×3 horizontally oriented structuring element instead. Note that there are a few glitches in this last image where the diagonal lines cross vertical lines. These could easily be eliminated, however, using a slightly longer structuring element.
Unlike erosion and dilation, the position of the origin of the structuring element does not really matter for opening and closing the result is independent of it.
Graylevel opening can similarly be used to select and preserve particular intensity patterns while attenuating others. As a simple example we start with

and then perform graylevel opening with a flat 5×5 square structuring element to produce

The important thing to notice here is the way in which bright features smaller than the structuring element have been greatly reduced in intensity, while larger features have remained more or less unchanged in intensity. Thus the fine grained hair and whiskers in the image have been much reduced in intensity, while the nose region is still at much the same intensity as before. Note that the image does have a more matt appearance than before since the opening has eliminated small specularities and texture fluctuations.
Similarly, opening can be used to remove `salt noise' in images. The image

shows an image containing salt noise, and

shows the result of opening with a 3×3 square structuring element. The noise has been entirely removed with relatively little degradation of the underlying image. However, if the noise consists of dark points (i.e. `pepper noise') as it can be seen in

graylevel opening yields

Here, no noise has been removed. At some places where two nearby noise pixels have merged into one larger point, the noise level has even been increased. In this case of `pepper noise', graylevel closing is a more appropriate operator.
As we have seen, opening can be very useful for separating out particularly shaped objects from the background, but it is far from being a universal 2-D object recognizer/segmenter. For instance if we try and use a long thin structuring element to locate, say, pencils in our image, any one such element will only find pencils at a particular orientation. If it is necessary to find pencils at other orientations then differently oriented elements must be used to look for each desired orientation. It is also necessary to be very careful that the structuring element chosen does not eliminate too many desirable objects, or retain too many undesirable ones, and sometimes this can be a delicate or even impossible balance.
Consider, for example,

which contains two kinds of cell: small, black ones and larger, gray ones. Thresholding the image at a value of 210 yields

in which both kinds of cell are separated from the background. We want to retain only the large cells in the image, while removing the small ones. This can be done with straightforward opening. Using a 11 pixel circular structuring element yields

Most of the desired cells are in the image, whereas none of the black cells remained. However, we cannot find any structuring element which allows us to detect the small cells and remove the large ones. Every structuring element that is small enough to allow the dark cells remain in the image would not remove the large cells, either. This is illustrated in

which is the result of applying a 7 pixel wide circular structuring element to the thresholded image.

## Common Variants

It is common for opening to be used in conjunction with closing to achieve more subtle effects, as described in the section on closing.

## Exercises

1. Apply opening to

using square structuring elements of increasing size. Compare the results obtained with the different sizes. If your implementation of the operator does not support graylevel opening, threshold the input image.
2. How can you detect the small cells in the above example

while removing the large cells Use the closing operator with structuring elements at different sizes in combination with some logical operator.
3. Describe two 2-D object shapes (different from the ones shown in the image below)

between which simple opening could distinguish, when the two are mixed together in a loose flat pile. What would be the appropriate structuring elements to use?
4. Now describe two 2-D shapes that opening couldn't distinguish between.
5. Can you explain why the position of the origin within the structuring element does not affect the result of the opening, when it does make a difference for both erosion and dilation?

## References

R. Haralick and L. Shapiro Computer and Robot Vision, Vol. 1, Addison-Wesley Publishing Company, 1992, Chap. 5, pp 174 - 185.
D. Vernon Machine Vision, Prentice-Hall, 1991, pp 78 - 79.

# Closing

Common Names: Closing

## Brief Description

Closing is an important operator from the field of mathematical morphology. Like its dual operator opening, it can be derived from the fundamental operations of erosion and dilation. Like those operators it is normally applied to binary images, although there are graylevel versions. Closing is similar in some ways to dilation in that it tends to enlarge the boundaries of foreground (bright) regions in an image (and shrink background color holes in such regions), but it is less destructive of the original boundary shape. As with other morphological operators, the exact operation is determined by a structuring element. The effect of the operator is to preserve background regions that have a similar shape to this structuring element, or that can completely contain the structuring element, while eliminating all other regions of background pixels.

## How It Works

Closing is opening performed in reverse. It is defined simply as a dilation followed by an erosion using the same structuring element for both operations. See the sections on erosion and dilation for details of the individual steps. The closing operator therefore requires two inputs: an image to be closed and a structuring element.
Graylevel closing consists straightforwardly of a graylevel dilation followed by a graylevel erosion.
Closing is the dual of opening, i.e. closing the foreground pixels with a particular structuring element, is equivalent to closing the background with the same element.

## Guidelines for Use

One of the uses of dilation is to fill in small background color holes in images, e.g. `pepper noise'. One of the problems with doing this, however, is that the dilation will also distort all regions of pixels indiscriminately. By performing an erosion on the image after the dilation, i.e. a closing, we reduce some of this effect. The effect of closing can be quite easily visualized. Imagine taking the structuring element and sliding it around outside each foreground region, without changing its orientation. For any background boundary point, if the structuring element can be made to touch that point, without any part of the element being inside a foreground region, then that point remains background. If this is not possible, then the pixel is set to foreground. After the closing has been carried out the background region will be such that the structuring element can be made to cover any point in the background without any part of it also covering a foreground point, and so further closings will have no effect. This property is known as idempotence. The effect of a closing on a binary image using a 3×3 square structuring element is illustrated in Figure 1.

Figure 1 Effect of closing using a 3×3 square structuring element

As with erosion and dilation, this particular 3×3 structuring element is the most commonly used, and in fact many implementations will have it hardwired into their code, in which case it is obviously not necessary to specify a separate structuring element. To achieve the effect of a closing with a larger structuring element, it is possible to perform multiple dilations followed by the same number of erosions.
Closing can sometimes be used to selectively fill in particular background regions of an image. Whether or not this can be done depends upon whether a suitable structuring element can be found that fits well inside regions that are to be preserved, but doesn't fit inside regions that are to be removed.
The image

is an image containing large holes and small holes. If it is desired to remove the small holes while retaining the large holes, then we can simply perform a closing with a disk-shaped structuring element with a diameter larger than the smaller holes, but smaller than the large holes.
The image

is the result of a closing with a 22 pixel diameter disk. Note that the thin black ring has also been filled in as a result of the closing operation.
In real world applications, closing can, for example, be used to enhance binary images of objects obtained from thresholding. Consider that we want compute the skeleton of

To do this we first need to transform the graylevel image into a binary image. Simply thresholding the image at a value of 100 yields

We can see that the threshold classified some parts of the receiver as background. The image

is the result of closing the thresholded, image with a circular structuring element of size 20. The merit of this operator becomes obvious when we compare the skeletons of the two binary images. The image

is the skeleton of the image which was only thresholded and

is the skeleton of the image produced by the closing operator. We can see that the latter skeleton is less complex and it better represents the shape of the object.
Unlike erosion and dilation, the position of the origin of the structuring element does not really matter for opening and closing. The result is independent of it.
Graylevel closing can similarly be used to select and preserve particular intensity patterns while attenuating others.
The image

is our starting point.
The result of graylevel closing with a flat 5×5 square structuring element is shown in

Notice how the dark specks in between the bright spots in the hair have been largely filled in to the same color as the bright spots, while the more uniformly colored nose area is largely the same intensity as before. Similarly the gaps between the white whiskers have been filled in.
Closing can also be used to remove `pepper noise' in images.
The image

is an image containing pepper noise.
The result of a closing with a 3×3 square structuring element is shown in

The noise has been completely removed with only a little degradation to the underlying image. If, on the other hand, the noise consists of bright spots (i.e. `salt noise'), as can be seen in

closing yields

Here, no noise has been removed. The noise has even been increased at locations where two nearby noise pixels have merged together into one larger spot. Compare these results with the ones achieved on the same image using opening.
Although closing can sometimes be used to preserve particular intensity patterns in an image while attenuating others, this is not always the case. Some aspects of this problem are discussed under opening.

## Common Variants

Opening and closing are themselves often used in combination to achieve more subtle results. If we represent the closing of an image f by C(f), and its opening by O(f), then some common combinations include:
Proper Opening

Min(f, /em{C}(O(C(f))))
Proper Closing

Max(f, O(C(O(f))))
Automedian Filter

Max(O(C(O(f))), Min(f, C(O(C(f)))))

These operators are commonly known as morphological filters.

## Exercises

1. Use closing to remove the lines from

, whereas the circles should remain. Do you manage to remove all the lines?
Now use closing to remove the circles while keeping the lines. Is it possible to achieve this with only one structuring element?
2. Can you use closing to remove certain features (e.g. the diagonal lines) from

Try it out.
3. Combine closing and opening to remove the `salt'n'pepper' noise from

## References

R. Gonzalez and R. Woods Digital Image Processing, Addison-Wesley Publishing Company, 1992, pp 524, 552.
R. Haralick and L. Shapiro Computer and Robot Vision, Vol. 1, Addison-Wesley Publishing Company, 1992, pp 174 - 185.
A. Jain Fundamentals of Digital Image Processing, Prentice-Hall, 1986, p 387.
D. Vernon Machine Vision, Prentice-Hall, 1991, pp 78 - 79.