Skip to content

Distance Measures on a Pixel Grid

Images are built on a grid of pixels. To describe how close two pixels are, we need a definition of distance. Different distance measures produce different paths, neighborhoods, and ideas of connectivity.

1. Pixels as Coordinates

A pixel can be described by its position on the image grid. Let two pixels be

\[ p=(x_1,y_1), \qquad q=(x_2,y_2). \]

On this page, \(x\) represents horizontal position and \(y\) represents vertical position.

Two Pixels on the Same Grid
p q horizontal change vertical change

Define the horizontal and vertical coordinate differences as

\[ \Delta x = |x_1-x_2| \] \[ \Delta y = |y_1-y_2|. \]
\(\Delta x\) measures horizontal separation and \(\Delta y\) measures vertical separation. The three distance definitions below combine these quantities differently.

2. Euclidean Distance

Euclidean distance, also called the \(L_2\) distance, measures the direct straight-line distance between two points.

\[ d_E(p,q) = \sqrt{ (\Delta x)^2 + (\Delta y)^2 }. \]
Straight-Line Distance
p q
Euclidean distance is the familiar geometric idea of measuring directly from one point to another.

3. Manhattan / City-Block Distance

Manhattan distance, also called the \(L_1\) distance, measures distance using horizontal and vertical movement.

\[ d_M(p,q) = \Delta x + \Delta y. \]
One Shortest City-Block Path
p q
Think of traveling through streets arranged as a rectangular grid. You can move horizontally or vertically, but a diagonal shortcut is not counted as one step.

4. Chebyshev / Chessboard Distance

Chebyshev distance, also called the \(L_\infty\) distance, allows horizontal, vertical, and diagonal movement. A diagonal step costs the same as a horizontal or vertical step.

\[ d_C(p,q) = \max \left( \Delta x, \Delta y \right). \]
One Shortest Chessboard Path
p q
This resembles the movement of a king on a chessboard. A diagonal move changes both coordinates during one step.

5. Same Pixels, Different Distances

Suppose the two pixels differ by

\[ \Delta x=4, \qquad \Delta y=3. \]
Euclidean
5
\[ \sqrt{4^2+3^2}=5 \] Straight-line distance
Manhattan
7
\[ 4+3=7 \] Horizontal and vertical steps
Chebyshev
4
\[ \max(4,3)=4 \] Diagonal movement is allowed
The positions of \(p\) and \(q\) have not changed. Only the definition of distance changed.

6. Neighborhoods Defined by Distance

A neighborhood around a center pixel \(p\) can be defined by selecting every pixel \(q\) whose distance from \(p\) is no greater than radius \(r\):

\[ N_r(p) = \{ q \mid d(p,q) \le r \}. \]

The examples below all use the same radius, \(r=3\).

Euclidean
Approximately circular on the discrete pixel grid
Manhattan
Diamond-shaped neighborhood
Chebyshev
Square neighborhood
With the same radius \(r=3\), the distance definition changes the neighborhood shape: approximately circular for Euclidean, diamond-shaped for Manhattan, and square for Chebyshev.
The square neighborhoods commonly used by image filters can be interpreted as Chebyshev-distance neighborhoods.

7. 4-Neighborhood and 8-Neighborhood

Two especially common local neighborhoods use only the pixels immediately surrounding a center pixel.

4-Neighborhood

Horizontal and vertical neighbors only.

8-Neighborhood

Horizontal, vertical, and diagonal neighbors.

A 4-neighborhood corresponds naturally to pixels at Manhattan distance 1. An 8-neighborhood corresponds naturally to pixels at Chebyshev distance 1.
Distance determines which positions are nearby. Connectivity determines how neighboring foreground pixels may form paths and connected objects. Connectivity will be developed separately.

8. Why Does the Distance Definition Matter?

Distance is not only used to say how far apart two pixels are. It also helps define several other image-processing concepts.

N
Neighborhoods
Distance determines which pixels are considered nearby around a chosen center pixel.
C
Connectivity
It helps define which neighboring pixels can connect to form paths, regions, and objects.
M
Morphology
Different structuring-element shapes reflect different spatial neighborhood ideas.
S
Shape Analysis
Distance is used to describe spacing, boundaries, object size, and distance-transform based measurements.
Choose a distance definition
→
Determine which pixels are nearby
→
Build neighborhoods and paths
→
Use them in image-processing operations

9. Interactive Example

The interactive visualizer below uses the same pixel grid in two different ways.

Compare Two Pixels

Select pixels \(p\) and \(q\).

The visualizer calculates \(\Delta x\), \(\Delta y\), Euclidean distance, Manhattan distance, and Chebyshev distance.

You can then display a representative shortest path for each distance rule.

Build a Neighborhood

Select a center pixel, a distance measure, and radius \(r\).

Every pixel satisfying \(d(p,q)\le r\) is highlighted.

Switching the distance rule changes the neighborhood shape immediately.

The pixel grid stays the same. Only the mathematical rule used to interpret spatial separation changes.

Summary

Concept Meaning
\(\Delta x\) Absolute horizontal coordinate difference.
\(\Delta y\) Absolute vertical coordinate difference.
Euclidean / \(L_2\) \[ d_E = \sqrt{ (\Delta x)^2 + (\Delta y)^2 } \]
Manhattan / City-Block / \(L_1\) \[ d_M = \Delta x+\Delta y \]
Chebyshev / Chessboard / \(L_\infty\) \[ d_C = \max( \Delta x, \Delta y ) \]
\(N_r(p)\) Pixels whose selected distance from \(p\) is no greater than radius \(r\).
4-Neighborhood Immediate horizontal and vertical neighbors.
8-Neighborhood Immediate horizontal, vertical, and diagonal neighbors.

Pixel Grid
Select pixel p, then select pixel q.
Pixel p Pixel q
Center Euclidean Manhattan Chebyshev
Selected Pixels
p = —
q = —
Coordinate Differences
Δx = —
Δy = —
Distances
Euclidean —
Manhattan —
Chebyshev —
Show a Path
Center
—
Neighborhood Shape
Approximately circular
Included Pixels
0
Includes the center pixel.
Select pixel p, then select pixel q.

Published inDigital Image Processing

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *