Previous Image Processing : Extracting and Analyzing Shapes Next

Thinning Image Objects

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.

  1. Prepare the display device and load a grayscale color table:
  2. DEVICE, DECOMPOSED = 0, RETAIN = 2  
    LOADCT, 0  
    

     

  3. Select and open the image file:
  4. file = FILEPATH('pollens.jpg', $  
        SUBDIRECTORY = ['examples','demo','demodata'])  
    READ_JPEG, file, img, /GRAYSCALE  
    

     

  5. Get the image dimensions, create a window and display the original image:
  6. dims = SIZE(img, /DIMENSIONS)  
    WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $  
        TITLE='Original, Binary and Thinned Images'  
    TVSCL, img, 0  
    

     

  7. The thinning operation requires a binary image. Create a binary image, retaining pixels with values greater than or equal to 140, and display the image:
  8. binaryImg = img GE 140  
    TVSCL, binaryImg, 1  
    

     

     


    Note
    The following lines were used to determine the threshold value:
    WINDOW, 2, XSIZE = 400, YSIZE = 300
    PLOT, HISTOGRAM(img)
    See Determining Intensity Values for Threshold and Stretch for details about using a histogram to determine intensity values.

     

  9. Prepare hit and miss structures for thinning. Rotate the outer elements of each successive hit and miss structure one position clockwise:
  10.  


    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]]  
    

     

  11. Define the iteration variables for the WHILE loop and prepare to pass in the binary image:
  12. bCont = 1b  
    iIter = 1  
    thinImg = binaryImg  
    

     

  13. Enter the following WHILE loop statements into the Editor window. The loop specifies that the image will continue to be thinned with MORPH_THIN until the thinned image is equal to the image input into the loop. Since thinImg equals inputImg, the loop is exited when a complete iteration produces no changes in the image. In this case, the condition, bCont eq 1 fails and the loop is exited.
  14. 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.

     

  15. Display an inverse of the final result:
  16. TVSCL, 1 - thinImg, 3  
    

The following figure displays the results of the thinning operation, reducing the original objects to a single pixel wide lines.

 

Figure 9-23: Original Image (top left), Binary Image (top right), Thinned Image (bottom left) and Inverse Thinned Image (bottom right)

Figure 9-23: Original Image (top left), Binary Image (top right), Thinned Image (bottom left) and Inverse Thinned Image (bottom right)

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)