|
Image Processing : Extracting and Analyzing Shapes |
|
The MORPH_THIN function performs a thinning operation on binary images. After designating "hit" and "miss" structures, the thinning operation applies the hit-or-miss operator to the original image and then subtracts the result from the original image.
The thinning operation is typically applied repeatedly, leaving only pixel-wide linear representations of the image objects. The thinning operation halts when no more pixels can be removed from the image. This occurs when the thinning operation (applying the hit and miss structures and subtracting the result) produces no change in the input image. At this point, the thinned image is identical to the input image.
When repeatedly applying the thinning operation, each successive iteration uses hit and miss structures that have had the individual elements of the structures rotated one position clockwise. For example, the following 3-by-3 arrays show the initial structure (left) and the structure after rotating the elements one position clockwise around the central value (right).
h0 = [[0,0,0], h1 = [[0,0,0], [0,1,0], [1,1,0], [1,1,1]] [1,1,0]]
The following example uses eight rotations of each of the original hit and miss structuring elements. The repeated application of the thinning operation results in an image containing only pixel-wide lines indicating the original grains of pollen. This example displays the results of each successive thinning operation. Complete the following steps for a detailed description of the process.
| Example Code See morphthinexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. |
| Note This example uses a file from the examples/demo/demodata directory of your installation. If you have not already done so, you will need to install "IDL Demos" from your product CD-ROM to install the demo data file needed for this example. |
DEVICE, DECOMPOSED = 0, RETAIN = 2 LOADCT, 0
file = FILEPATH('pollens.jpg', $
SUBDIRECTORY = ['examples','demo','demodata'])
READ_JPEG, file, img, /GRAYSCALE
dims = SIZE(img, /DIMENSIONS) WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $ TITLE='Original, Binary and Thinned Images' TVSCL, img, 0
binaryImg = img GE 140 TVSCL, binaryImg, 1
| Note The following lines were used to determine the threshold value: WINDOW, 2, XSIZE = 400, YSIZE = 300See Determining Intensity Values for Threshold and Stretch for details about using a histogram to determine intensity values. |
| Note For a version of these structures that is easy to copy and paste into an Editor window, see MorphThinExample.pro in the examples/doc/image subdirectory of the IDL installation directory. This code displays the eight pairs of hit and miss structuring elements on individual lines so that the code can be easily copied into an Editor window. Although it is less visible, the elements of each successive structure are rotated as shown below and as described in the beginning of this section, Thinning Image Objects. |
h0 = [[0b,0,0], $ [0,1,0], $ [1,1,1]] m0 = [[1b,1,1], $ [0,0,0], $ [0,0,0]] h1 = [[0b,0,0], $ [1,1,0], $ [1,1,0]] m1 = [[0b,1,1], $ [0,0,1], $ [0,0,0]] h2 = [[1b,0,0], $ [1,1,0], $ [1,0,0]] m2 = [[0b,0,1], $ [0,0,1], $ [0,0,1]] h3 = [[1b,1,0], $ [1,1,0], $ [0,0,0]] m3 = [[0b,0,0], $ [0,0,1], $ [0,1,1]] h4 = [[1b,1,1], $ [0,1,0], $ [0,0,0]] m4 = [[0b,0,0], $ [0,0,0], $ [1,1,1]] h5 = [[0b,1,1], $ [0,1,1], $ [0,0,0]] m5 = [[0b,0,0], $ [1,0,0], $ [1,1,0]] h6 = [[0b,0,1], $ [0,1,1], $ [0,0,1]] m6 = [[1b,0,0], $ [1,0,0], $ [1,0,0]] h7 = [[0b,0,0], $ [0,1,1], $ [0,1,1]] m7 = [[1b,1,0], $ [1,0,0], $ [0,0,0]]
bCont = 1b iIter = 1 thinImg = binaryImg
bCont eq 1 fails and the loop is exited.WHILE bCont EQ 1b DO BEGIN & $ PRINT,'Iteration: ', iIter & $ inputImg = thinImg & $ thinImg = MORPH_THIN(inputImg, h0, m0) & $ thinImg = MORPH_THIN(thinImg, h1, m1) & $ thinImg = MORPH_THIN(thinImg, h2, m2) & $ thinImg = MORPH_THIN(thinImg, h3, m3) & $ thinImg = MORPH_THIN(thinImg, h4, m4) & $ thinImg = MORPH_THIN(thinImg, h5, m5) & $ thinImg = MORPH_THIN(thinImg, h6, m6) & $ thinImg = MORPH_THIN(thinImg, h7, m7) & $ TVSCL, thinImg, 2 & $ WAIT, 1 & $ bCont = MAX(inputImg - thinImg) & $ iIter = iIter + 1 & $ ENDWHILE
| Note The & after BEGIN and the $ allow you to use the WHILE/DO loop at the IDL command line. These & and $ symbols are not required when the WHILE/DO loop in placed in an IDL program as shown in MorphThinExample.pro in the examples/doc/image subdirectory of the IDL installation directory. |
TVSCL, 1 - thinImg, 3
The following figure displays the results of the thinning operation, reducing the original objects to a single pixel wide lines.
![]() |
Each successive thinning iteration removed pixels marked by the results of the hit-or-miss operation as long as the removal of the pixels would not destroy the connectivity of the line.
IDL Online Help (March 06, 2007)