Introduction to Digital Image Processing with MATLAB® Asia Edition McAndrew‧Wang‧Tseng Chapter 12: Shapes and Boundaries 1 © 2010 Cengage Learning Engineering. All Rights Reserved. 1 12.1 Introduction • How do we tell if two objects have the same shape? • How can we classify shape? • How can we describe the shape of an object? Formal means of describing shapes are called shape descriptors, which may include size, symmetry, and length of perimeter 2 Ch12-p.355 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers • The idea of a chain code is quite straightforward: We walk around the boundary of an object, taking note of the direction we take The resulting list of directions is the chain code 3 Ch12-p.355 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.2 4 Ch12-p.356 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers • Suppose we walk along the boundary in a counterclockwise direction starting at the leftmost point in the top row and list the directions as we go 333233000101112122 5 Ch12-p.356 665660001222344 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers • A simple boundary-following algorithm has been given by Sonka et al. [35] • The version for 4-connected boundaries: Start by finding the pixel in the object that has the left-most value in the topmost row; call this pixel P0 Define a variable dir (for direction), and set it equal to 3 6 Ch12-p.357 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers Traverse the 3 × 3 neighborhood of the current pixel in a counterclockwise direction, beginning the search at the pixel in direction This simply sets the current direction to the first direction counterclockwise from dir: 7 Ch12-p.357 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers The first foreground pixel will be the new boundary element. Update dir Stop when the current boundary element Pn is equal to the second element P1 and the previous boundary pixel Pn−1 is equal to the first boundary element P0 im is a binary image 8 Ch12-p.357 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.4 9 Ch12-p.358 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.5 10 Ch12-p.360 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 11 Ch12-p.360 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 12 Ch12-p.362 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 13 Ch12-p.363 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers • 12.2.1 Normalization of Chain Codes • There are two problems with the definition of the chain code as given in previous sections: The chain code is dependent on the starting pixel The chain code is dependent on the orientation of the object. 14 Ch12-p.363 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.7 15 Ch12-p.364 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 16 Ch12-p.363 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 17 Ch12-p.364 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 18 Ch12-p.365 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 19 Ch12-p.365 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.8 20 Ch12-p.366 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers • 12.2.2 Shape Numbers We now consider the problem of defining a chain code that is independent of the orientation of the object 21 Ch12-p.366 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 22 Ch12-p.366 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers Suppose we rotate the shape so that it has a different orientation (we use the rot90 function, which rotates a matrix by 90°) 23 Ch12-p.367 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 24 Ch12-p.367 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.9 25 Ch12-p.368 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.10 (normalized) 26 Ch12-p.369 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 27 Ch12-p.369 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers This is already normalized 28 Ch12-p.369 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.2 Chain Codes and Shape Numbers 29 Ch12-p.370 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors • The idea is this: suppose we walk around an object, but instead of writing down the directions, we write down the boundary coordinates • The final list of (x, y) coordinates can be turned into a list of complex numbers z = x + yi • The Fourier transform of this list of numbers is a Fourier descriptor of the object 30 Ch12-p.371 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors • We can easily modify our function chaincode4.m to boundary4.m by replacing the lines 31 Ch12-p.371 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors 32 Ch12-p.371 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors 33 Ch12-p.372 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.11 34 Ch12-p.372 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors 35 Ch12-p.373 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.12 36 Ch12-p.373 © 2010 Cengage Learning Engineering. All Rights Reserved. 12.3 Fourier Descriptors • The Fourier transform of c contains only three nonzero terms • Only two terms of the transform are enough to begin to get some idea of the shape, size, and symmetry of the object • Even though the shape itself has been greatly hanged—a square has become a circle—many shape descriptors are still little changed (such as size and symmetry) 37 Ch12-p.373 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.13 38 Ch12-p.374 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.14 39 Ch12-p.374 © 2010 Cengage Learning Engineering. All Rights Reserved. FIGURE 12.14 40 Ch12-p.375 © 2010 Cengage Learning Engineering. All Rights Reserved.