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.
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.
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.
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.
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
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.
It is common for opening to be used in conjunction with closing to achieve more subtle effects, as described in the section on closing.
You can interactively experiment with this operator by clicking here.
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.
while removing the large cells Use the closing operator with structuring elements at different sizes in combination with some logical operator.
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?
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.
Specific information about this operator may be found here.
More general advice about the local HIPR installation is available in the Local Information introductory section.
©2003 R. Fisher, S. Perkins,
A. Walker and E. Wolfart.