You are on page 1of 4

# Fast Algorithms for Binary Dilation and Erosion

Using Run-Length Encoding

Wook-Joong Kim, Seong-Dae Kim, and Kyuheon Kim

ABSTRACT⎯Fast binary dilation and erosion algorithms using run-length encoding (RLE) [1] are proposed. A run is a
using run-length encoding (RLE) are proposed. RLE is an sequence of ‘1’ (or ‘0’) pixels horizontally (or vertically), and
alternative way of representing a binary image using a run, RLE is a representation of a binary image using a combination
which is a sequence of ‘1’ pixels. First, we derive the run-based
of runs. Handling binary images in runs, rather than in pixels,
representation of dilation and erosion and then present the full
steps of the proposed algorithms in detail. can bring a considerable reduction in computation because the
number of runs is usually much smaller than the number of
Keywords⎯Binary image processing, morphological pixels. Previously, several ideas for run-based morphological
operator, run-length encoding. operations were presented in [2]-[4]. Piper and Tang [2]
introduced the concept of using RLE for binary morphological
I. Introduction operations, and Cardoner and Thomas [3], [4] applied the
concept for skeletoning and line drawing. However, a detailed
Morphological operators are some of the most fundamental theoretical background and an effective algorithm with full
and popular nonlinear tools for various binary image numerical analysis have yet to be presented.
processing. Morphological operators are constructed by In section II, we provide a mathematical investigation on
combining two basic operators: dilation and erosion. Assuming dilation and erosion in terms of runs, and in section III, fast
that Z is a region in binary image I, the dilation and erosion of Z algorithms for dilation and erosion are proposed based on the
by structuring element S are defined as investigation. The proposed algorithms have inputs and outputs

{
Dilation: Z ⊕ S = p Sˆ p ∩ Z ≠ 0 } (1)
as run-length encoded forms. In section IV, simulation results
are presented, and we conclude in section V.

{
Erosion: Z Θ S = p S p ⊆ Z , } (2)
II. Dilation and Erosion Using RLE
where Ŝ is the reflection about the origin and Sp is the For the sake of simplicity, a run is considered as a sequence
translation by position p. All other morphological operators are of ‘1’ pixels in the horizontal direction throughout this paper.
defined by concatenating the two operators. For example, the Assume that binary image I has region Z of ‘1’ pixels, and the
opening operator is defined by dilation after erosion, and the RLE representation of Z is defined as Z = ∪ Nn=1 Rn , where N is
closing operator is defined by erosion after dilation. the number of runs, and Rn= <xLn, xRn, yn> is the n-th run of
In this paper, fast binary dilation and erosion algorithms Z. The xLn, xRn, and yn of Rn respectively indicate the x-
coordinate of the left-most (starting) pixel, the x-coordinate of
Manuscript received Feb. 16, 2005; revised Aug. 26, 2005.
Wook-Joong Kim (phone: +82 42 860 6123, email: wjk@etri.re.kr) and Kyuheon Kim
the right-most (ending) pixel, and the y-coordinate of Rn.
(email: kkim@etri.re.kr) are with Digital Broadcasting Research Division, ETRI, Daejeon, Regarding RLE, we define a ‘compact representation of
Korea.
RLE’ when concatenated ‘1’ pixels are defined as a single run,
Seong-Dae Kim (email: sdkim@sdvision.kaist.ac.kr) is with the Department of Electrical
Engineering, KAIST, Daejeon, Korea. not divided into several runs with or without overlapping.

814 Wook-Joong Kim et al. ETRI Journal, Volume 27, Number 6, December 2005

xRmS : y mS and RnZ = xLZn . Z n N ⎡M = ∪ ⎢ ∪ Rˆ mS n =1⎣ m =1 ( ) p ⎤ N M ∩ RnZ ⎥ = ∪ ∪ Rˆ mS ⎦ n =1 m =1 [( ) p ] ∩ RnZ . the erosion operation is identifying all possible ( ) p n ( = ∪ Nn=1 ∪ Mm=1 RnZ ⊕ RmS . it is the same as finding all ( ) Hence. we have [ ( Z Θ S = ∩ Mm=1 ∪ Nn=1 RnZ Θ RmS . the erosion of Z by a single run R is defined as and Z is expressed as { } { Z Θ R = p R p ⊆ Z = p R p ⊆ ∪ Nn=1 RnZ ( )} ⎡N ⎤ N Sˆ p ∩ Z = Sˆ p ∩ ⎢ ∪ RnZ ⎥ = ∪ Sˆ p ∩ RnZ [ ] { } (8) ⎣n =1 ⎦ n =1 (3) = ∪ Nn=1 p R p ⊆ RnZ = ∪ Nn=1 [R Θ R]. xRnZ : y nZ . yd). and (iii) when the left. Dilation Using RLE = {p (R ) ⊆ Z }∩ ∩ {p (R ) S 1 p S M p ⊆Z } Here. unnecessary { } { ZΘS = p S p ⊆ Z = p ∪ Mm=1 RmS ( ) p ⊆Z } computation is required due to redundancy among the runs. 815 . the dilation of Z by S is expressed as III. (6) n =1 m =1 In this section. ⎫⎤ ⎢ ⎪ n m n m n m ⎪⎥ M ⎢ ⎪ = ∩ ⎢∪⎨ N S ( if xRm − xLm ≤ xRn − xLZn S Z ) ( ) ⎪⎥ . Therefore. Let [ = ∩ Mm=1 Z Θ RmS . Therefore. S Z RmS = xLSm . Run R1 includes R2 in the following situations: (i) when the length of R1 is equal Therefore. December 2005 Wook-Joong Kim et al. we have compactly represented. Regarding dilation RnZ ⊕ RmS . { [( ) ∩ R ] ≠ 0} = ∪ Nn=1 ∪ Mm=1 p Rˆ mS Z (4) From (2). ) positions p where RmS p is included in RnZ . erosion. a run-based expression of dilation is presented. respectively. the two runs RnZ and ( Rˆ mS ) p overlap when obtain − y mS + y d = y nZ − y mS + y d = y nZ Z ΘS or (5) xL ≤ − xL + xd ≤ xR Z n S m Z n xL ≤ − xR + xd ≤ xR . where p= (xd . Number 6.and right-most pixels of R2 are within R1. Proposed Algorithms N M Z ⊕ S = ∪ ∪ xLZn + xLSm . To have further operation with the ETRI Journal. the dilation of Z by S is the same as the union of the to or longer than R2. modifying (10) and applying it into (9). Combining (7) and (8).Unless a compact run-length encoding is made. )] (9) { }{ Z ⊕ S = p Sˆ p ∩ Z ≠ 0 = p ∪ Nn=1 ∪ Mm=1 Rˆ mS [( ) p ] } ∩ RnZ ≠ 0 Next. translated reflection of RmS can be represented as ( Rˆ mS ) p = − xRmS + xd . xR Z − xR S : y Z − y S . a run-based representation of erosion is presented. However. possible positions where RnZ and ( Rˆ mS ) p overlap. Z n S m Z n ⎡ ⎧ xLZ − xLS . the proposed dilation and erosion algorithms are presented. The outcomes from (6) and (11) are the results for dilation or Now. ) ( ) can have an overlapping relation when they are located at the From ( ii) ⇒ y mS + y d = y nZ . (10) same y position and share more than one pixel. For a given xLm + x d ≥ xLn . Before presenting the algorithms. xRnZ + xRmS : y nZ + y mS . Two runs ( From ( i ) ⇒ xRmS − xLSm ≤ xRnZ − xLZn . erosion of Z by S is expressed as Applying (3) into the dilation definition (1). Volume 27. The intersection of Ŝp Meanwhile. (11) ⎬⎥ Modifying (5). Erosion Using RLE primary runs as the runs generated from (6) or (11). we finally Hence. we define 2. { [( ) ∪ ∪ (R ) ] ⊆ Z } = p R1S p S M p (7) 1.− xLSm + xd : − y mS + y d . ⎢⎣ ⎪⎩φ . RnZ includes RnS p when the following are true. ] Z = ∪ Nn=1 RnZ and S = ∪ Mm=1 RmS be the compact RLE representation of Z and S. let’s consider the erosion of single runs: RnZ Θ RmS . we obtain m =1 n =1 ⎢ ⎪ ⎪⎥ y d = y nZ + y mS . (ii) when they are at the same y position. otherwise ⎪⎥ ⎭⎦ xLZn + xLSm ≤ xd ≤ xRnZ + xRmS . dilation of each run of Z by each run of S. the From (iii) ⇒ xRmS + x d ≤ xRnZ . it is not guaranteed that primary runs are From the definition of erosion (2).

but a certain index value m among [1. a structuring element is regarded as a filter erosion algorithm needs more consideration than the dilation window and is scanned throughout all possible pixels. Output: compact representation of ZΘS . primary runs are obtained in step 1. Volume 27. Assuming z and s are the number of pixels in Z and S. but at least. find N × M primary runs by (6). dilation and erosion are proportional to O( z × s) 1). algorithms. Test image and structuring element. performance. 256 × 256 pixels. Number 6. find a primary run using (11). To show their them into one. Sort the primary runs by the value of a y-coordinate. Sort the primary runs by the values of a y-coordinate. if: (a) ‘Symbol’ image (b) Structuring element ① The number of primary runs at a certain y-coordinate is less than M. Step 2. For each primary run. erosion. shows the test image and structuring element used for our simulations. December 2005 . IV. Figure 1 algorithm due to the finding of intersections of primary runs. Hence. respectively. that is. Step 2. go to step 4. M] is missing. Repeat the following two operations until no overlapping or inclusion of primary runs exists: In this section. 1 and identify the runs that have the same y-coordinate. Procedure: -1 0 1 Step 1. it can provide the strengths primary runs using (6) and then eliminate redundancy among of the proposed algorithms from a qualitative perspective. A them. always satisfied. a compacting process. eliminating unnecessary primary runs in step 3. The vertical axis from step 3. The index is additional information that helps to identify which run of S is used for obtaining a 0 primary run. This comparison may not be able to show The flow of the dilation algorithm is straightforward: Obtain quantitative evidence. the proposed erosion algorithm is presented. ETRI Journal. the proposed dilation and erosion algorithms require to eliminate unnecessary computations. Otherwise.…. we show how much computational reduction ① If any two runs overlap as similarly to (5). can conclude that O( z × s ) > O((N × M ) ⋅ (1 + log( N × M ) )) is and identify the runs that have the same y-coordinate. terminate the process. combine can be obtained from the proposed algorithms. The image is composed of 380 runs and its size is Input: compactly run-length encoded Z and S. O( N × M + sorting ( N × M )) operations. Pixel-unit erosion In the case of erosion. 1. or Fig. modified as O((N × M ) ⋅ (1 + log( N × M ) )) . Compared that is. delete the included run. we Step 1. opening. The filtering. Using runs of Z and S. the The proposed dilation algorithm is as follows: computational complexity for fast sorting of N numbers is proportional to O( sorting ( N )) = N log( N ) . Find the common regions of all the runs that survived dilation. The results show that the proposed algorithms require only Let’s consider the computational complexity of the proposed less than 9% of computation time for pixel-unit processing. Because z × s Procedure: is always bigger than N × M without losing generality. pixel-unit processing is implemented as two dimensional Next. Using runs of Z and S. Figure 2 shows a comparison of computation times for Step 4. we compared the execution times between the ② If any a primary run is included in another primary run proposed algorithms and their pixel-unit processing under the as similarly to (10). same machine. and closing. Simulations Step 4. Eliminate all primary runs at a certain y-coordinate. represents the elapsed time after 1000 repetitions. an additional process for removing redundancy. Generally. and takes less time than pixel-unit dilation because scanning is the intersection of primary runs is calculated in step 4 after restricted to the input regions. the Input: compactly run-length encoded Z and S. If more than two runs exist at a certain y-coordinate. Step 3. ② The number is more than or equal to M. Step 3. computational complexity of the proposed algorithms can be Output: compact representation of Z ⊕ S . among primary runs is required to this. store an index that indicates the m -1 of S = ∪ Mm=1 RmS . the computational complexity of pixel-unit 1) O( y ) = Order of ( y ) 816 Wook-Joong Kim et al.primary runs. and the ratio of the execution time is shown at the bottom.

” Computer Vision. Jan. Min-Ho Park. no.Y. 1989. “Residuals + Directional Gaps = Skeletons. pp. no. 6. The computation times for 1000 repetitions of each operation using a PC with a 1. Pattern Recognition and Artificial Intelligence. 11. ETRI Journal. The proposed algorithms are developed based on the run-based expressions of dilation and erosion as described in section II. [2] L. pp. May 1997. Lee. Dec. V. Volume 27. 4. 41. “A New Chain-Coding Algorithm for Binary Images Using Run-Length Codes. and Image Proc.21.519 1 0. and Yong-Il Kim.4.874 7. “An Application of Canonical Correlation Analysis Technique to Land Cover Classification of LANDSAT Images. no. 201-209.59 GHz CPU and 1 Gbyte RAM.41-51. and J. Conclusions In this paper. 18. pp. 817 . [3] R. “Efficient Morphological Set Transformations on Line Drawings. vol. References [1] S. vol. Number 6. Thomas. 1997.01%) (2.” Int’l J. 1.316 0. Sep... vol. Cardoner and F.126 0. Graphics. fast RLE-based dilation and erosion algorithms are presented.” Pattern Recognition Letters. Tang. Thomas. 1999. In addition to the fast computing performance of the proposed algorithms.235 0 Dilation Erosion Opening Closing (1. vol. J. Piper and J. Cardoner and F. pp. 3. “Erosion and Dilation of Binary Images by Arbitrary Structuring Elements Using Interval Coding. no. Pixel-unit operation Proposed algorithms 9 7.68%) (8. 114 - 128.107 0. no. 1988. Kim. December 2005 Wook-Joong Kim et al. 9.” Pattern Recognition Letter. pp. [5] Jong-Hun Lee. we believe that the run-based investigation on morphological operators can also be used in various image processing applications [5] and as instructive information for the further development of relevant technologies.895 8 7 6. 947-960. 343-353. [4] R.98%) Fig. Kim. vol. Apr.375 6 S 2 1.” ETRI J.3%) (4. 2.