Previous Image Processing : Extracting and Analyzing Shapes Next

Combining Morphological Operations

The following example uses a variety of morphological operations to remove bridges from a satellite image of New York waterways. Complete the following steps for a detailed description of the process.


Example Code
See removebridges.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example.

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

     

  3. Specify the known dimensions and use READ_BINARY to load the image:
  4. xsize = 768   
    ysize = 512   
    img = READ_BINARY(FILEPATH('nyny.dat', $  
       SUBDIRECTORY = ['examples', 'data']), $  
        DATA_DIMS = [xsize, ysize])  
    

     

  5. Increase the image's contrast and display the image:
  6. img = BYTSCL(img)  
    WINDOW, 1, TITLE = 'Original Image'  
    TVSCL, img  
    

     

    Figure 9-24: Original Image

    Figure 9-24: Original Image

     

  7. Prepare to threshold the image, using an intensity histogram as a guide for determining the intensity value:
  8. WINDOW, 4, XSIZE = 400, YSIZE = 300  
    PLOT, HISTOGRAM(img)  
    


Note
Using an intensity histogram as a guide for determining threshold values is described in the section, Determining Intensity Values for Threshold and Stretch.

  1. Create a mask of the darker pixels that have values less than 70:
  2. maskImg = img LT 70  
    

     

  3. Define and create a small square structuring element, which has a shape similar to the bridges which will be masked out:
  4. side = 3  
    strucElem = DIST(side) LE side  
    

     

  5. Remove details in the binary mask's shape by applying the opening operation:
  6. maskImg = MORPH_OPEN(maskImg, strucElem)  
    

     

  7. Fuse gaps in the mask's shape by applying the closing operation and display the image:
  8. maskImg = MORPH_CLOSE(maskImg, strucElem)  
    WINDOW, 1, title='Mask After Opening and Closing'  
    TVSCL, maskImg  
    

     

    This results in the following figure:

     

    Figure 9-25: Image Mask After Opening and Closing Operations

    Figure 9-25: Image Mask After Opening and Closing Operations

     

  9. Prepare to remove all but the largest region in the mask by labeling the regions:
  10. labelImg = LABEL_REGION(maskImg)  
    

     

  11. Discard the black background by keeping only the white areas of the previous figure:
  12. regions = labelImg[WHERE(labelImg NE 0)]  
    

     

  13. Define mainRegion as the area where the population of the labelImg region matches the region with the largest population:
  14. mainRegion = WHERE(HISTOGRAM(labelImg) EQ $  
       MAX(HISTOGRAM(regions)))  
    

     

  15. Define maskImg as the area of labelImg equal to the largest region of mainRegion, having an index number of 0 and display the image:
  16. maskImg = labelImg EQ mainRegion[0]  
    Window, 3, TITLE = 'Final Masked Image'  
    TVSCL, maskImg  
    

     

    This results in a mask of the largest region, the waterways, as shown in the following figure.

     

    Figure 9-26: Final Image Mask

    Figure 9-26: Final Image Mask

     

  17. Remove noise and smooth contours in the original image:
  18. newImg = MORPH_OPEN(img, strucElem, /GRAY)  
    

     

  19. Replace the new image with the original image, where it's not masked:
  20. newImg[WHERE(maskImg EQ 0)] = img[WHERE(maskImg EQ 0)]  
    

     

  21. View the results using FLICK to alternate the display between the original image and the new image containing the masked areas:
  22. WINDOW, 0, XSIZE = xsize, YSIZE = ysize  
    FLICK, img, newImg  
    

     

    Hit any key to stop the image from flickering. Details of the two images are shown in the following figure.

     

    Figure 9-27: Details of Original (left) and Resulting Image of New York (right)

    Figure 9-27: Details of Original (left) and Resulting Image of New York (right)

  IDL Online Help (March 06, 2007)