Morphological
Morphological
SECOND PART
MORPHOLOGICAL IMAGE PROCESSING
Each of these approaches was found to have advantages and disadvantages. In this
section we discuss an approach based on the concept of so-called morphological watersheds.
These often produces more stable segmentation results, including connected segmentation
boundaries. This approach also provides a simple framework for incorporating knowledge-
based constraints in the segmentation process.
Background:
For a particular regional minimum, the set of points satisfying condition (b) is called the
catchment basin or watershed of that minimum.
Binary images may contain numerous imperfections. In particular, the binary regions
produced by simple thresholding are distorted by noise and texture. Morphological image
processing pursues the goals of removing these imperfections by accounting for the form and
structure of the image. These techniques can be extended to grey scale images.
Morphology is a broad set of image processing operations that process images based on
shapes. Morphological operations apply a structuring element to an input image, creating an
output image of the same size. In a morphological operation, the value of each pixel in the output
image is based on a comparison of the corresponding pixel in the input image with its neighbors.
By choosing the size and shape of the neighborhood, you can construct a morphological
96
operation that is sensitive to specific shapes in the input image.
Morphological techniques probe an image with a small shape or template called
a structuring element.
The structuring element is positioned at all possible locations in the image and it is
compared with the corresponding neighborhood of pixels. Some operations test whether the
97
element "fits" within the neighborhood, while others test whether it "hits" or intersects the
neighborhood:
The structuring element is a small binary image, i.e. a small matrix of pixels, each with a
value of zero or one:
A common practice is to have odd dimensions of the structuring matrix and the origin
defined as the centre of the matrix. Stucturing elements play in moprphological image
processing the same role as convolution kernels in linear image filtering .
When a structuring element is placed in a binary image, each of its pixels is associated with
the corresponding pixel of the neighbourhood under the structuring element. The structuring
element is said to fit the image if, for each of its pixels set to 1, the corresponding image pixel is
98
also 1. Similarly, a structuring element is said to hit, or intersect, an image if, at least for one of
its pixels set to 1 the corresponding image pixel is also 1.
Fitting and hitting of a binary image with structuring elements s1 and s2.
Zero-valued pixels of the structuring element are ignored, i.e. indicate points where the
corresponding image value is irrelevant.
Fundamental operations:
The most basic morphological operations are dilation and erosion
In the morphological dilation and erosion operations, the state of any given pixel in the
output image is determined by applying a rule to the corresponding pixel and its neighbors in the
input image. The rule used to process the pixels defines the operation as a dilation or an erosion.
This table lists the rules for both dilation and erosion.
Rules for Dilation and Erosion
Operation Rule
Dilation The value of the output pixel is the maximum value of all the pixels in the input pixel's
neighborhood. In a binary image, if any of the pixels is set to the value 1, the output pixel
is set to 1.
Erosion The value of the output pixel is the minimum value of all the pixels in the input pixel's
neighborhood. In a binary image, if any of the pixels is set to 0, the output pixel is set to 0.
The following figure illustrates the dilation of a binary image. Note how the structuring
element defines the neighborhood of the pixel of interest, which is circled. The dilation function
applies the appropriate rule to the pixels in the neighborhood and assigns a value to the
corresponding pixel in the output image. In the figure, the morphological dilation function sets
the value of the output pixel to 1 because one of the elements in the neighborhood defined by the
structuring element is on.
Morphological Dilation of a Binary Image
99
The following figure illustrates this processing for a grayscale image. The figure shows
the processing of a particular pixel in the input image. Note how the function applies the rule to
the input pixel's neighborhood and uses the highest value of all the pixels in the neighborhood as
the value of the corresponding pixel in the output image.
Morphological Dilation of a Grayscale Image
The erosion of a binary image f by a structuring element s (denoted f s) produces a new binary
image g = f s with ones in all locations (x,y) of a structuring element's origin at which that
structuring element s fits the input image f, i.e. g(x,y) = 1 is s fits f and 0 otherwise, repeating for
all pixel coordinates (x,y).
100
Erosion with small (e.g. 2×2 - 5×5) square structuring elements shrinks an image by
stripping away a layer of pixels from both the inner and outer boundaries of regions. The holes
and gaps between different regions become larger, and small details are eliminated:
Larger structuring elements have a more pronounced effect, the result of erosion with a
large structuring element being similar to the result obtained by iterated erosion using a smaller
structuring element of the same shape. If s1 and s2 are a pair of structuring elements identical in
shape, with s2twice the size of s1, then
101
Binary image Dilation: a 2×2 square structuring element
http://documents.wolfram.com/applications/digitalimage/UsersGuide/Morphology/ImageProcessing6.3.html
Results of dilation or erosion are influenced both by the size and shape of a structuring element.
Dilation and erosion are dual operations in that they have opposite effects. Let f c denote the
complement of an image f, i.e., the image produced by replacing 1 with 0 and vice versa.
Formally, the duality is written as
f s = fc srot
where srot is the structuring element s rotated by 1800. If a structuring element is symmetrical
with respect to rotation, then srot does not differ from s. If a binary image is considered to be a
collection of connected regions of pixels set to 1 on a background of pixels set to 0, then erosion
is the fitting of a structuring element to these regions and dilation is the fitting of a structuring
element (rotated if necessary) into the background, followed by inversion of the result.
Operation Rule
Dilation Pixels beyond the image border are assigned the minimum value afforded by the data type.
For binary images, these pixels are assumed to be set to 0. For grayscale images, the
minimum value for uint8 images is 0.
Erosion Pixels beyond the image border are assigned the maximum value afforded by the data type.
For binary images, these pixels are assumed to be set to 1. For grayscale images, the
maximum value for uint8 images is 255.
102
Image Processing Toolbox
Perform image processing, analysis, and algorithm development
Examples
Functions and Other Reference
Release Notes
PDF Documentation
Image Processing Toolbox™ provides a comprehensive set of reference-standard
algorithms and workflow apps for image processing, analysis, visualization, and algorithm
development. You can perform image segmentation, image enhancement, noise reduction,
geometric transformations, image registration, and 3D image processing.
Image Processing Toolbox apps let you automate common image processing workflows.
You can interactively segment image data, compare image registration techniques, and batch-
process large datasets. Visualization functions and apps let you explore images, 3D volumes, and
videos; adjust contrast; create histograms; and manipulate regions of interest (ROIs).
You can accelerate your algorithms by running them on multi core processors and GPUs.
Many toolbox functions support C/C++ code generation for desktop prototyping and embedded
vision system deployment.
Getting Started
Learn the basics of Image Processing Toolbox
Compound operations
Many morphological operations are represented as combinations of erosion, dilation, and simple
set-theoretic operations such as the complement of a binary image:
f c(x,y) = 1 if f(x,y) = 0, and f c(x,y) = 0 if f(x,y) = 1,
103
Set operations on binary images: from left to right: a binary image f,
a binary image g, the complement f c of f, the intersection f ∩ g, and the union f ∪ g.
f s=(f s) s
Opening is so called because it can open up a gap between objects connected by a thin bridge of
pixels. Any regions that have survived the erosion are restored to their original size by the
dilation:
104
Opening is an idempotent operation: once an image has been opened, subsequent openings with
the same structuring element have no further effect on that image:
(f s) s) = f s.
Closing is so called because it can fill holes in the regions while keeping the initial region sizes.
Like opening, closing is idempotent: (f • s) • s = f • s, and it is dual operation of opening (just as
opening is the dual operation of closing):
105
f • s = (f c s)c; f s = (f c • s)c.
In other words, closing (opening) of a binary image can be performed by taking the complement
of that image, opening (closing) with the structuring element, and taking the complement of the
result.
The hit and miss transform (see also HIPS2 web page) allows to derive information on
how objects in a binary image are related to their surroundings. The operation requires a matched
pair of structuring elements, {s1, s2}, that probe the inside and outside, respectively, of objects in
the image:
A pixel belonging to an object is preserved by the hit and miss transform if and only
if s1 translated to that pixel fits inside the object AND s2translated to that pixel fits outside the
object. It is assumed that s1 and s2 do not intersect, otherwise it would be impossible for both fits
to occur simultaneously.
The hit and miss transform can be used for detecting specific shapes (spatial
arrangements of object and background pixel values) if the two structuring elements present the
desired shape, as well as for thinning or thickening of object linear elements.
106
Morphological filtering of a binary image is conducted by considering compound
operations like opening and closing as filters. They may act as filters of shape. For example,
opening with a disc structuring element smooths corners from the inside, and closing with a disc
smooths corners from the outside. But also these operations can filter out from an image any
details that are smaller in size than the structuring element, e.g. opening is filtering the binary
image at a scale defined by the size of the structuring element.
Only those portions of the image that fit the structuring element are passed by the filter;
smaller structures are blocked and excluded from the output image. The size of the structuring
element is most important to eliminate noisy details but not to damage objects of interest.
Opening (morphology)
In mathematical morphology, opening is the dilation of the erosion of a set A by a structuring
element B:
Together with closing, the opening serves in computer vision and image processing as a
basic workhorse of morphological noise removal. Opening removes small objects from
the foreground (usually taken as the bright pixels) of an image, placing them in the
background, while closing removes small holes in the foreground, changing small islands
of background into foreground. These techniques can also be used to find specific shapes
in an image. Opening can be used to find things into which a specific structuring element
Properties
107
Closing (morphology)
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.
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.
108
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 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
109
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 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
110
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.
The image
111
is an image containing pepper noise.
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
112
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))))
113
Proper Closing
Max(f, O(C(O(f))))
Automedian Filter
Interactive Experimentation
Exercises
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.
114
THE HIT OR MISS TRANSFORMATION: It is a morphological operation where we
mainly focus on finding patterns of foreground and background object.
(1) Hit condition: If the foreground and background pixels in the structuring elements
exactly matches with the foreground and background pixels in the image then it is hit
condition, and the pixels below the origin of structuring element is bet to the
foreground color.
(2) Miss condition: If foreground and background pixels in element does not match
exactly then it is miss condition.
This transform is useful in locating all pixel configurations that match the B1 structure
(i.e. a hit) but do not match that B2 (i.e. a miss). Thus, the hit-or-miss transform is used for
shape detection.
115
Example : Use the hit-or-miss transform to identify the locations of the following shape
pixel configuration in the image below using the two structuring elements B1 and B2.
Solution:
The figure below shows an example of applying the hit-or-miss transform on the image in the
previous example.
116
Fig. (a) Binary image. (b) Result of applying hit-or-miss transform.
A B = (A Ө B) ∩ ( Ac Ө B2)
Foreground Background
1 1
= (A Ө B) ∩ (Ac Ө (W-B)) where B2 is W-B, let W is 1 1
Let A as 1 1 1 0
1 1 0 1
1 1 0 0
And B as 1 0
0 1
0 0 1 0
0 0 0 0
AӨB = 0 0 0 0
117
Let W = 1 1
1 1
0 1
W–B= 1 0
Ac = 0 0 0 1
0 0 1 0
0 0 1 1
0 0 1 0
Then Ac Ө (W-B) =
0 0 0 0
0 0 0 0
0 0 1 0
THEN A B = 0 0 0 0 =(A Ө B) ∩ (Ac Ө (W-B))
0 0 0 0
118
119
120
121
122
123
124
125