A FAST ALGORITHM FOR MORPHOLOGICAL AND DILATION

C. Jeremy Pye J. A. Bangham School of Information Systems, University of East Anglia, Norwich, NR4 7TJ, UK. Tel: +44 0 1603 456161; Fax: +44 0 1603 453345 Email:[email protected],[email protected]

ABSTRACT maximum and minimum values as the histogram adapts it

k l

This paper describes a new algorithm for performing erosion is possible to reduce the computation required by a

l and dilation which is suitable for flat line-segment structur- structuring from kl down to computations per el- ing functions, and which has a computational complexity ement. A drawback of this type of method is that the data that is independent of the structuring function size. Unlike must be represented by a fixed number of bits in order to a other proposed algorithms, the computation time required by build a histogram. Astola [4] presents a similar method based this method is directly proportional to the number of extrema upon a double heap structure which elevates this constraint. within the signal being processed. This makes it particularly Recently several algorithms designed to perform the fast cal- suitable for signals and images that have large and slowly culation of the running maximum or minimum on a one varying segments. dimensional signal have been presented. Pitas [5, 6] de- velops an algorithm based upon the ”divide and conquer” 1 INTRODUCTION principle designed to exploit the redundancy in the running calculations, resulting in a reduced number of comparisons

Since its evolution in the late 1970's, mathematical morphol- k per element log . Herk [7] proposes an alternative algo- ogy [1] has been regarded as a powerful discipline suited  rithm based upon a restricted distance transform. Using this to image processing and analysis. Underlying mathemati- method the number of comparisons per element is reduced to cal morphology are two primitive operations, known as ero-

just and is independent of the window size. sion and dilation. All morphological operations can be con- Whilst these methods demonstrate significant improvement

structed from a combination of erosions and dilations. A tra-

k over the general O solution under certain circumstances ditional and naive implementation of these two operations is these algorithms are rather inefficient. based upon the technique employed for order-statistic filter- ing, which involves passing a window, or structuring func- 2 FAST RUNNING MAXIMUM

tion, over the signal and at each position recording the ith

x

ranked value within the window. Erosion and dilation re- Consider a discrete input signal consisting of elements i

i Z quire finding either the maximum or minimum value within R . In this paper we will only consider the running the window. This is a special case of order-statistic filter- maximum as the running minimum can be obtained by sim- ing because it does not require a sorting of the windowed ply inverting the signal before and after processing. In order values. Instead, selection of either the maximum or mini- to simplify the explanation of the algorithm the structuring

mum requires only a single pass through the windowed val- element will consist only of delayed input elements, that is

k k

ues. This has a computational complexity of O where

 

y max x x x

i i

k  ik  is the number of samples contained within the window or i (1) structuring function. Through the wise use of structuring el-

ement decomposition [2] it is often possible to reduce the although the more usual running maximum [5, 7] can be ob-

n number of computations required by splitting the structur- tained by simply shifting the output signal by

ing element into a number of smaller structuring functions. elements.

k l For example, if an image is dilated using a flat size In order to understand the inefficiencies of the present algo-

structuring function, the same result can be achieved using rithms it is necessary to examine a number of input scenarios.

k l two separate structuring elements of size and .

This clearly reduces the number of computations per element 2.1 Monotonic

k l from kl down to . Other methods [3] involve maintain- Given a monotonic signal as input, that is a signal whose ing local histograms of the values within the window. As elements are all of a constant value,

the window moves from one position to the next many of

x x max x x x

i i i i the samples within the window remain. By monitoring the i (2)

and hence, 2.5 Local minimum 

 A local minimum occurs when two local maxima exist within

y max x x x

i i

ik  ik  x

(3) the same signal. It is formed at j when a positive ramp

x i follows a negative ramp, that is

2.2 Positive ramp 

x i j

i x

Given a signal in which the elements are ordered so that they i (12)

x ij

 form a positive ramp, i

The output due to the negative ramp component of the signal,

x x max x x x

i i i i i (4) is formulated in subsection (2.3),

and hence,

y x i j

i

k 

i (13)

 

y max x x x

i i

ik  ik 

(5) Similarly, from subsection (2.2), the component of the sig-

x i nal that forms a positive ramp is unaffected by the filtering 2.3 Negative ramp process. That is,

Given a signal in which the elements are ordered so that they

y x i j k i i (14)

form a negative ramp,

ijn

The output between the limits j cannot

x x max x x x

i i i i i (6) be determined so easily as none of the previous cases apply.

and hence, From equation (1) the output between these limits is given 

 by,

y max x x x

i i

ik  ik  

(7) 

y max x x x

i i

k  ik 

i (15)

x

ik 

2.4 Local maximum This can be decomposed into two parts. Given that the signal

Consider now a signal that has a positive ramp followed by a for all ij forms a positive ramp the maximum of the sam-

i j x

ples between and is i . Also, because the signal for all

negative ramp, forming a single maximum at position j . That

j

is, i forms a negative ramp, the maximum of the samples

i j x

k 

between and is i . Therefore,



 

x i j

i

x

y max x x jijk

i (8)

i i

ik 

x ij

 i (16)

From subsection (2.2) the component of the signal that forms In conclusion given a local minimum the output is given as,

j the positive ramp is unaffected by the filtering process, i .

Also, from subsection (2.3) the component of the signal that 



x i j

ik 



 

jk

forms a negative ramp is also unaffected, i .

y

max x x jijk

i

i

ik 

However, part of the signal does not fit into either of these 

 (17)

x i j k

i

i j

cases. From equation (1), between the limits j

k the output of the filter is given by

2.6 Combinations local

 

y max x x x i

i Whilst the cases so far discussed are valid they are in gen-

k  ik  i (9) eral unlikely to occur individually. It is much more likely

When the window is positioned at the extreme left of this that a number of local maxima and minima will exist within

j

limit so that i , then the same signal. If local maxima occur in close proximity 

 to each other then the simplifications obtained by separating

y max x x x

i j 

k  j k  j (10) the signal into various component parts, as demonstrated in

subsection (2.5), do not apply. x clearly the window contains j which by definition is known

to be the maximum value within the signal. Similarly, when Figure (1) shows a signal containing three local maxima at

abc ba k cb

the window is positioned at the extreme right of these limits, positions a b c. Where and

k x x a

and the ordering of the maximum are b

j k

that is i , it is clear that the window also contains x

c . Each local maxima is separated by a corresponding local

x x

j

k 

the element i . In conclusion,

a d b e

minima at the positions d e f , where 

cf. It is clear from this diagram that the output due to the



x i j

i



x x x

a c

maximum b is affected by both and .

y

x ji j k i

j (11)

 In order to calculate the output given this interference caused



x ij k

k  i by proximity, it is necessary to re-formulate the output with

 

x

 

x

 

   

r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r

x x

x x x x x x x x x x

f f

a d a d c c b e b e

    

y

         

A

y

  

r r r r r r r r r r r r r r r r r r r r r b b b b

y y y y

a b c a

Figure 1: (Top) Shows a signal x which contains three local

x x x

b c maxima a , and . The dotted line attempts to illustrate

the problem caused by the close proximity of the individual B

y

   

maxima. (Bottom) Shows the result after applying a running 

maximum of width k .

r r r r b b b b y respect to each individual local maximum. Only the samples b

that constitute the individual local maxima are considered in

    each case. 

Recall from the definition of a local maximum, equation (8), C

y x

a local maxima at i results from samples that form a positive

 

x x

i

ramp i followed by samples that form a negative

r r r r b b b b

x x

i

ramp i . Therefore the collection of samples that

y

c

A

x x

form the local maximum a are denoted as

A

x fx x x g

    

a d

 (18)

y

     A

From equation (11) the output given the input collection x ,

is



 

r r r r r r r r r r r r r r r r

x i a



i





y y y

a b c

x ai a k

A

a

y

i



a k i 

 (19)

k

x Figure 2: Illustration of processing the individual local max-

a

d k imum and recombining these intermediate results to form the The output given the collection of samples that constitute the output. The dotted lines (often occluded) represent the out-

x put, and the solid lines represent the input.

local maximum b can also be calculated,

B

x fx x x g

b e d (20) This procedure is illustrated graphically in figure (2). In or- and therefore, der to calculate the output for the complete signal it is neces-

 sary to combine these intermediate results using a maximum

x d i b

 i

 operator. That is,



x bi b k

B

b

y

 

i

A B C



b k i



y max x y y y i

i (24)

i i i

 (21)

x k

b

e k Indeed, the output due to every local maxima can be exam- where all of the elements outside of the collections are as-

ined in this way, sumed to be .

C

x fx x x g

c f e (22) 3 RESULTS AND DISCUSSION and

It is clear from subsections (2.1-2.3) that the number of com-



x e i c 

i putations required to calculate the running maximum for a





x ci c k C

c number of signals is low because many of the input samples

y

i



k i

c remain unaffected by the filtering process. Subsection (2.6) 

 (23)

x k

c

f k describes a method of calculating the running maximum which is suitable for all types of inputs whilst exploiting re- 500 dundancy within the signal samples. Consider a monotonic 400 Computation time vs. SE signal as input, because the method depends upon finding size for a signal containing and processing only the local maxima, the only calculation 300 a small number of required is the initial search along the signal for the maxima, extrema. of which there are none. Alternatively, given a signal that has 200 a local maxima at every other sample and therefore a maxi- mum number of local maxima, it is clear that the amount of 100 intermediate storage and computation required by this basic 0 method is high, as it is dependent upon both the number of maxima and also the distance between them. 500 for a signal containing a The underlying principle of this method is extending or maximum number of stretching parts of the signal. This suggests that a suitable 400 extrema. Computation Time (ms) architecture for the data streams is a type of run-length- 300 rle fast encoding (RLE). In fact whilst there is necessarily some classic computation overhead in encoding and decoding the input 200 signal into RLE form, this type of architecture does allow herk the intermediate operations described in subsection (2.6) to 100 be performed in place, elevating any storage problems. The algorithm is described below. 0 5 3 5 9 33 65 17 02 EXT = RLE.start; 129 257 513 1 SE size k 2049 while (EXT) if (EXT == local_maximum) then Figure 3: (TOP) Shows the computation times given a sig- EXT.width = EXT.width + k; nal containing a minimum number of extrema. (BOTTOM) endif shows the computation times given a signal containing a EXT = EXT.next; maximum number of extrema. In each case the signal con- end tains 8096 samples. EXT = RLE.start; while (EXT) if (EXT == local_minimum) then References remain = k; while (remain > 0) [1] J. Serra, Image analysis and if (EXT.width > remain) then Vol 1. London: Academic Press, 1982. EXT.width = EXT.width - remain; remain = 0; [2] X. Zhuang and R. M. Haralick, “Morphological structur- else ing element decomposition,” Computer Vision, Graphics remain -= EXT.width; and Image Processing, vol. 35, pp. 370Ð382, 1986. remove EXT; [3] T. S. Huang, G. J. Yang, and G. Y. Tang, “A fast two- EXT = MIN(EXT.prev, EXT.next); endif dimensional median filtering algorithm,” IEEE Trans- end actions on Acoustics, Speech, and Signal Processing, endif vol. 27, pp. 13Ð18, 1979. EXT = EXT.next; [4] J. Astola and T. G. Campbell, “On computation of end the running median,” IEEE Transactions on Acoustics, The algorithm is two stage, firstly expanding all the local Speech, and Signal Processing, vol. 37, pp. 572Ð574, maxima and then removing the smallest samples from the 1989. minima in order to allow for the expansion. [5] I. Pitas, “Fast algorithms for running ordering and For comparison the computational times for the classical run- max/min calculation,” IEEE Transactions on Circuits ning maximum selection algorithm, Herks [7] algorithm and and Systems, vol. 36, pp. 795Ð804, 1989. the proposed extrema processing algorithm are given in fig- ure (3). All of these algorithms were implemented on a SUN [6] D. Coltuc and I. Pitas, “Fast running max/min filters,” SPARCstation IPC and were written using the C++ language. in IEEE Workshop on Non-linear Signal and Image Pro- cessing, pp. 871Ð874, June 1995. 4 CONCLUSIONS [7] M. van Herk, “A fast algorithm for local minimum and This algorithm seems appropriate for signals that have large maximum filters on rectangular and octogonal kernels,” slowly varying sections, such as images. Even in its worst Pattern Recognition Letters, vol. 13, pp. 517Ð521, July case this algorithm appears to be comparable in performance 1992. to other methods.