|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.
This example uses a file from the
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, YSIZE = 2*dims, $ TITLE='Original, Binary and Thinned Images' TVSCL, img, 0
binaryImg = img GE 140 TVSCL, binaryImg, 1
The following lines were used to determine the threshold value:
For a version of these structures that is easy to copy and paste into an Editor window, see
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 1fails 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
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
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)