Routines (alphabetical) > Routines: M > MORPH_DISTANCE

# MORPH_DISTANCE

The MORPH_DISTANCE function estimates N-dimensional distance maps, which contain for each foreground pixel the distance to the nearest background pixel, using a given norm. Available norms include non-diagonal neighbors, chessboard, city block, and approximate Euclidean distance.

The distance map is useful for a variety of morphological operations: thinning, erosion and dilation by discs of radius r, and granulometry.

## Examples

The following code reads a data file in the IDL Demo data directory containing a magnified image of grains of pollen. It then applies a threshold and the morphological distance operator. Thresholding the resulting distance operator with a value of “n” produces the equivalent of eroding the thresholded image with a disc of radius “n”.

;Handle TrueColor displays:
DEVICE, DECOMPOSED=0
;Read the image
path=FILEPATH('pollens.jpg',SUBDIR=['examples','demo','demodata'])
READ_JPEG, path, img
; Create window:
WINDOW, 0, XSIZE=700, YSIZE=540
; Display the original image
XYOUTS, 180, 525, 'Original Image', ALIGNMENT=.5, /DEVICE
TV, img, 20, 280
; Apply the threshold:
thresh = img GE 140B
; Display the thresholded image
XYOUTS, 520, 525, 'Thresholded Image', ALIGNMENT=.5, /DEVICE
TVSCL, thresh, 360, 280
;Create Euclidean distance function
edist = MORPH_DISTANCE(thresh, NEIGHBOR_SAMPLING = 3)
; Display the distance function
XYOUTS, 180, 265, 'Distance Function', ALIGNMENT=.5, /DEVICE
TVSCL, edist, 20, 20
; Display image after erosion with a disc of radius 5:
XYOUTS, 520, 265, 'After erosion with disc of radius 5', $
ALIGNMENT=.5, /DEVICE
TVSCL, edist GT 5, 360, 20

## Syntax

Result = MORPH_DISTANCE (Data [, /BACKGROUND] [, NEIGHBOR_SAMPLING={1 | 2 | 3 }] [, /NO_COPY] )

## Return Value

The returned variable is an array of the same dimension as the input array.

## Arguments

### Data

An input binary array. Zero-valued pixels are considered to be part of the background.

## Keywords

### BACKGROUND

By default, the distance is computed for the foreground (non-zero) features in the Data argument. Set this keyword to compute the distance of the background features instead of the foreground features. If the keyword is set, elements of Result that are on an edge are set to 0.

### NEIGHBOR_SAMPLING

Set this keyword to indicate how the distance of each neighbor from a given pixel is determined. Valid values include:

- 0 = default. No diagonal neighbors. Each neighbor is assigned a distance of 1.
- 1 = chessboard. Each neighbor is assigned a distance of 1.
- 2 = city block. Each neighbor is assigned a distance corresponding to the number of pixels to be visited when traveling from the current pixel to the neighbor. (The path can only take 90 degree turns; no diagonal paths are allowed.)
- 3 = approximate Euclidean distance. Each neighbor is assigned an approximate Euclidean distance, where the distances along the diagonals and the center row and column are correct, but for speed the off-diagonal elements are approximated by adding together the distances after the square root.

#### Default Two Dimensional Example

1

1 X 1

1

#### Chessboard Two-Dimensional Example

1 1 1

1 X 1

1 1 1

#### City Block Two-Dimensional Example:

2 1 2

1 X 1

2 1 2

#### Approximate Euclidean Distance Two-Dimensional Example

sqrt(2) 1 sqrt(2)

1 X 1

sqrt(2) 1 sqrt(2)

### NO_COPY

Set this keyword to request that the input array be reused, if possible. If this keyword is set, the input argument is undefined upon return.

## Version History

5.3 | Introduced

## See Also

DILATE, ERODE, MORPH_CLOSE, MORPH_GRADIENT, MORPH_HITORMISS, MORPH_OPEN, MORPH_THIN, MORPH_TOPHAT