pink26 0.1.1
Image processing and digital topology for teaching
Loading...
Searching...
No Matches
distance.h File Reference

Distance maps of binary images. More...

#include <pink26/image.h>
#include <pink26/status.h>

Go to the source code of this file.

Enumerations

enum  p26_distance_kind {
  P26_DISTANCE_SQUARED_EUCLIDEAN = 0 , P26_DISTANCE_CHAMFER = 1 , P26_DISTANCE_D4 = 4 , P26_DISTANCE_D8 = 8 ,
  P26_DISTANCE_D6 = 6 , P26_DISTANCE_D18 = 18 , P26_DISTANCE_D26 = 26
}
 Kind of distance. More...

Functions

p26_status p26_distance_map (const p26_image *binary, p26_distance_kind kind, p26_image **out)
 Computes, for every object point, the distance to the nearest background point; the outside of the image counts as background.

Detailed Description

Distance maps of binary images.

The distance of an object point to the background is the length of the shortest path, or the straight-line distance, to the nearest background point. Every image is surrounded by an infinite background, so the points of the image frame are at distance 1 from the outside (as for p26_simple_points()).

The distances are those of Pink's ldist.c (Couprie, ESIEE): see p26_distance_kind for the exact definition of each one.

Enumeration Type Documentation

◆ p26_distance_kind

Kind of distance.

The value of the D4 ... D26 constants is the number of the neighbors used (Pink's connectivity argument); SQUARED_EUCLIDEAN and CHAMFER are 0 and 1.

Enumerator
P26_DISTANCE_SQUARED_EUCLIDEAN 

Squared Euclidean distance dx^2 + dy^2 (+ dz^2) to the nearest background point, exact (Meijster, Roerdink and Hesselink 2000; Pink's lsedt_meijster).

2D and 3D. Squared, so the values are integers.

P26_DISTANCE_CHAMFER 

5-7 chamfer distance (Pink's lchamfrein): a step to a 4-neighbor costs 5 and a step to a diagonal neighbor costs 7, so that the distance is about 5 times the Euclidean distance.

For a background point at offset (dx, dy) it is 5 * max(|dx|, |dy|) + 2 * min(|dx|, |dy|). 2D only (Pink's chamfer also exists in 3D, with weights 4, 5 and 6; pink26 does not offer it).

P26_DISTANCE_D4 

City-block distance |dx| + |dy|: steps between 4-neighbors.

2D.

P26_DISTANCE_D8 

Chessboard distance max(|dx|, |dy|): steps between 8-neighbors.

2D.

P26_DISTANCE_D6 

City-block distance |dx| + |dy| + |dz|: steps between 6-neighbors.

3D.

P26_DISTANCE_D18 

Steps between 18-neighbors (distance between D6 and D26).

3D.

P26_DISTANCE_D26 

Chessboard distance max(|dx|, |dy|, |dz|): 26-neighbors.

3D.

Function Documentation

◆ p26_distance_map()

p26_status p26_distance_map ( const p26_image * binary,
p26_distance_kind kind,
p26_image ** out )

Computes, for every object point, the distance to the nearest background point; the outside of the image counts as background.

Pink computes the D4 ... D26 and chamfer distances on an image whose object and background are exchanged (its "external distance"); the squared Euclidean distance is computed on the image as it is. In every case pink26 works on a copy of the image with a frame of background and gives back the interior.

Range: the squared Euclidean distance is computed in 32 bits; it is exact when the largest possible value, (rs + 2)^2 + (cs + 2)^2 [+ (ds + 2)^2 in 3D], is at most INT32_MAX: about 32000 pixels along each side in 2D, and 26000 in a cubic volume. Beyond that the call may return P26_E_OVERFLOW if a value is negative as int32, but a wrapped value that stays positive is not detected: do not rely on larger images. The other distances are at most 7 times the image diameter and never wrap.

Complexity: O(N) time and memory for the squared Euclidean distance and the D4 ... D26 distances (N is the number of pixels); the chamfer distance repeats two passes over the image until nothing changes (a few passes, at most about the image diameter).

Parameters
binaryP26_U8 image; any nonzero value is an object point.
kindKind of distance, see p26_distance_kind.
outSet to NULL on entry; receives a new P26_I32 image of the shape of binary (released with p26_image_free()): 0 on the background, the distance (at least 1) on the object.
Returns
P26_OK; P26_E_INVALID_ARG if out is NULL, binary is not a valid image, kind is unknown, or kind does not match the dimension (CHAMFER, D4 and D8 are 2D only; D6, D18 and D26 are 3D only); P26_E_TYPE if binary is not P26_U8; P26_E_OVERFLOW if the image with its frame is too large, or a squared Euclidean distance does not fit in int32 (see Range); P26_E_NOMEM if an allocation fails (including inside Pink); P26_E_INTERNAL if the admitted Pink code aborted. On error, *out stays NULL.