Creating Distance Maps
The MORPH_DISTANCE function computes a grayscale, N-dimensional distance map from a binary image. The map shows, for each foreground pixel, the distance to the nearest background pixel using a given norm. The norm simply defines how neighboring pixels are sampled. See the MORPH_DISTANCE description in the IDL Reference Guide for full details. The resulting values in the grayscale image denote the distance from the surveyed pixel to the nearest background pixel. The brighter the pixel, the farther it is from the background.
The following example applies the distance transformation to a grayscale image of a cultured sample of Neocosmospora vasinfecta, a common fungal plant pathogen. Complete the following steps for a detailed description of the process.
Example Code
See morphdistanceexample.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering morphdistanceexample at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT morphdistanceexample.pro.
- Prepare the display device and load a grayscale color table:
- Select and load an image:
- Get the size of the image and create a border for display purposes:
- Get the dimensions of the padded image, create a window and display the original image:
- Use an intensity histogram as a guide for creating a binary image:
- Before using the distance transform, the grayscale image must be translated into a binary image. Create and display a binary image containing the dark tubules. Threshold the image, masking out pixels with values greater than 120:
- Compute the distance map using MORPH_DISTANCE, specifying "chessboard" neighbor sampling, which surveys each horizontal, vertical and diagonal pixel touching the pixel being surveyed, and display the result:
- Display a combined image of the distance map and the binary image. Black areas within the binary image (having a value of 0) are assigned the maximum pixel value occurring in the distance image:
file = FILEPATH('n_vasinfecta.jpg', $
SUBDIRECTORY = ['examples', 'data'])
READ_JPEG, file, img, /GRAYSCALE
dims = SIZE(padImg, /DIMENSIONS) WINDOW, 0, XSIZE = 2*dims[0], YSIZE = 2*dims[1], $ TITLE='Distance Map and Overlay of Binary Image' TVSCL, padImg, 0
WINDOW, 2, XSIZE = 400, YSIZE = 300 PLOT, HISTOGRAM(padImg)Note
Using an intensity histogram as a guide for determining intensity values is described in the section, Determining Intensity Values for Threshold and Stretch.
binaryImg = padImg LT 120 WSET, 0 TVSCL, binaryImg, 1The original image (left) and binary image (right) appear in the following figure.

