286 A Computational Introduction to Digital Image Processing, Second Edition
by dilating twice:
((A B) B) B.
The first dilation returns the holes to their original size; the second dilation removes them.
But this will enlarge the objects in the image. To reduce them to their correct size, perform
a final erosion:
(((A B) B) B) B.
The inner two operations constitute an opening; the outer two operations a closing. Thus,
this noise removal method is in fact an opening followed by a closing:
(A B) B).
This is called morphological filtering.
Suppose we take an image and apply 10% shot noise to it. This can be done in MATLAB
or Octave by:
MATLAB/Octave
>> c = imread(’circles.png’);
>> x = rand(size(c));
>> c(find(x>0.95)) = 0;
>> c(find(x<0.05) = 1;
>> imshow(c)
and in Python using the function from the module of numpy:
Python
In : c = io.imread(’circles.png’).astype(’bool’)
*
1
In : x = np.random.random
_
sample(c.shape)
In : c[np.nonzero(x>0.95)]=0
In : c[np.nonzero(x<=0.05)]=1
The result is shown as Figure 10.17(a). The filtering process can be implemented with
MATLAB/Octave
>> cf1 = imclose(imopen(c,sq),sq);
>> figure, imshow(cf1)
>> cf2 = imclose(imopen(c,cr),cr);
>> figure, imshow(cf2)
and the same in Python, with imclose and imopen replaced with our aliases bwclose and
bwopen. The results are shown as Figures 10.17(b) and (c). The results are rather “blocky”;
although less so with the cross-shaped structuring element.
Relationship between Opening and Closing
Opening and closing share a relationship very similar to that of erosion and dilation:
the complement of an opening is equal to the closing of a complement, and the complement
of a closing is equal to the opening of a complement. Specifically:
A B = A
ˆ
B
and
A B = A
ˆ
B.
Again, see Haralick and Shapiro [14] for a formal proof.
Mathematical Morphology 287
(a) (b) (c)
FIGURE 10.17: A noisy binary image and results after morphological filtering with
different structuring elements.
10.5 The Hit-or-Miss Transform
This is a powerful method for finding shapes in images. As with all other morphological
algorithms, it can be defined entirely in terms of dilation and erosion; in this case, erosion
only.
Suppose we wish to locate 3 × 3 square shapes, such as is in the center of the image A
in Figure 10.18.
FIGURE 10.18: An image A containing a shape to be found
If we performed an erosion A B with B being the square structuring element, we would
obtain the result given in Figure 10.19.
FIGURE 10.19: The erosion A B
288 A Computational Introduction to Digital Image Processing, Second Edition
The result contains two pixels, as there are exactly two places in A where B will fit. Now
suppose we also erode the complement of A with a structuring element C, which fits exactly
around the 3 × 3 square;
A and C are shown in Figure 10.20. (We assume that (0, 0) is at
the center of C.)
FIGURE 10.20: The complement A and the second structuring element
If we now perform the erosion A C, we would obtain the result shown in Figure 10.21.
FIGURE 10.21: The erosion A C
The intersection of the two erosion operations would produce jus t one pixel at the
position of the center of the 3×3 square in A, which is just what we want. If A had contained
more than one square, the final result would have b een single pixels at the p ositions of the
centers of each. This combination of erosions forms the hit-or-miss transform.
In general, if we are looking for a particular shape in an image, we design two structuring
elements: B
1
, which is the same shape, and B
2
, which fits around the shape. We then write
B = (B
1
, B
2
) and
A B = (A B
1
) (A B
2
)
for the hit-or-miss transform.
As an example, we shall attempt to find the dot at the bottom of the question mark in
the
morph
_
text.png image. This is in fact a 4 × 4 square with missing corners. The two
structuring elements then will be defined in MATLAB/Octave as
MATLAB/Octave
>> b1 = [0 1 1 0;1 1 1 1;1 1 1 1;0 1 1 0]
>> b2 = ones(6); b2(2:5,2:5) = ~b1
>> tb1 = imerode(t,b1);
>> tb2 = imerode(~t,b2);
>> hit
_
or
_
miss = tb1 & tb2;
>> [x,y] = find(hit
_
or
_
miss==1)
or in Python with
Mathematical Morphology 289
Python
In : b1 = np.array([[0,1,1,0],[1,1,1,1],[1,1,1,1],[0,1,1,0]])
In : b2 = ones((6,6)); b2[1:5,1:5] = 1-b1
In : tb1 = bwerode(t,b1);
In : tb2 = bwerode(1-t,b2);
In : np.where((tb1 & tb2)==1)
and this returns a coordinate of (281, 296) in MATLAB/Octave and (280, 295) in Python,
which is right in the middle of the dot. Note that eroding the text by
b1 alone is not
sufficient, as there are many places in the images where
b1 can fit. This can be seen by
viewing the image
tb1
, which is given in Figure 10.22.
FIGURE 10.22: Text eroded by a dot-shaped structuring element
10.6 Some Morphological Algorithms
In this section we shall investigate some simple algorithms that use some of the mor-
phological techniques we have discussed in previous sections.
Region Filling
Suppose in an image we have a region bounded by an 8-connected boundary, as shown
in Figure 10.23.
Given a pixel p within the region, we wish to fill up th e entire region. To do this, we start
with p, and dilate as many times as necessary with the cross-shaped structuring element
B (as used in Figure 10.6), each time taking an intersection with
A bef ore continuing. We
thus create a sequence of sets:
{p} = X
0
, X
1
, X
2
, . . . , X
k
= X
k+1
for which
X
n
= (X
n1
B) A.
Finally, X
k
A is the filled region. Figure 10.24 shows how this is done.
290 A Computational Introduction to Digital Image Processing, Second Edition
FIGURE 10.23: An 8-connected boundary of a region to be filled
1
p
1
2 1 2
2
3
5 4
6 5
FIGURE 10.24: The process of filling the region
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.184.21