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

Homotopic operators on grayscale images: thinning, thickening, homotopic kernel and lambda-kernel ([BEC97], [CBB01]). More...

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

Go to the source code of this file.

Functions

p26_status p26_gray_homotopic_thinning (const p26_image *image, int connectivity, const p26_image *condition, int max_steps, p26_image **out)
 Grayscale homotopic thinning ([BEC97], [CBB01]; Pink's lhthindelta, lhthindelta3d, command hthin).
p26_status p26_gray_homotopic_thickening (const p26_image *image, int connectivity, const p26_image *condition, int max_steps, p26_image **out)
 Grayscale homotopic thickening ([BEC97], [CBB01]; Pink's lhthickdelta, lhthickdelta3d, command hthick).
p26_status p26_gray_homotopic_kernel (const p26_image *image, int connectivity, const p26_image *condition, p26_image **out)
 Lower homotopic kernel ([BEC97], [CBB01] section 4.1; Pink's lhtkern, mctopo3d_lhtkern3d, command htkern).
p26_status p26_gray_lambda_kernel (const p26_image *image, int connectivity, int lambda, const p26_image *condition, p26_image **out)
 Lambda-kernel, or filtered leveling kernel ([CBB01] section 5; Pink's llambdakern, command lambdaskel), corrected by pink26 (see "What pink26 changes" at the top of this file).

Detailed Description

Homotopic operators on grayscale images: thinning, thickening, homotopic kernel and lambda-kernel ([BEC97], [CBB01]).

Cross-section topology. A grayscale image F is seen as a stack of binary images, its sections: for each level k, the section F_k is the set of the points p with F(p) >= k. An operator is homotopic when every section of the result has the topology of the same section of the input (same components and holes in 2D; also cavities and tunnels in 3D). On a 0/1 image there is one nonempty section, and these operators become binary thinning and thickening.

The points that may change, in plain words:

  • a point p is destructible when p is a simple point of the section F_k with k = F(p): it can leave that section without changing its topology. Lowering p to alpha-(p), the largest value of its neighbours below F(p), changes only the sections F_k with alpha-(p) < k <= F(p), and they all look the same around p, so it keeps the topology of every section;
  • a point p is constructible when p is simple for the complement of the section {q : F(q) > F(p)}: raising p to alpha+(p), the smallest value of its neighbours above F(p), adds p to that section without changing its topology;
  • a point p is lambda-destructible (lambda >= 0) when it is destructible, or a peak (no neighbour at its level or above) at most lambda above alpha-(p), or a pass between k >= 2 lower regions of its neighbourhood of which at least k - 1 lie entirely at most lambda below F(p) ([CBB01] section 5.1; Pink's lambdadestr4). Lowering it may merge minima, or erase a peak, of contrast at most lambda: the topology changes, but only by details of small contrast.

The operators:

  • homotopic thinning (Pink's command hthin, [CBB01] appendix A): each step lowers, one after the other, every point that was destructible at the start of the step, each to the lowest value that keeps the topology (delta-), but never below the value it could reach at the start of the step: a step shrinks the bright regions by about one point. Until stability, the result is a homotopic kernel;
  • homotopic thickening (hthick): the dual, it raises constructible points;
  • homotopic kernel (htkern, "ultimate homotopic thinning"): lowers destructible points until none is left, always taking a point of LOWEST level first (a priority queue). It is not the same as thinning until stability: the thinning lowers the points of a step in the order of its work list (a stack: the first step takes the points in reverse raster order, the next ones the points just lowered and their neighbours), whatever their level; the kernel lowers first the points next to the deepest minima, which therefore spread first. Both results have no destructible point and the topology of the input, but they may differ;
  • lambda-kernel (lambdaskel, [CBB01] section 5.2): the same with the lambda-destructible points; it removes the minima and the peaks of contrast at most lambda, then thins. It is a filter, not a homotopic operator (for lambda = 0 it is homotopic).

Connectivity. In grayscale, the connectivity parameter is the connectivity of the MINIMA (of the dark regions), as in [CBB01] and Pink: the sections (the bright regions) use the complementary one. On a 0/1 image, connectivity 4 therefore thins like a binary 8-connected object on a 4-connected background, and connectivity 8 like a 4-connected object on an 8-connected background. Accepted values: 4 or 8 in 2D, 6 or 26 in 3D; 18 is refused (P26_E_UNSUPPORTED): Pink's 3D functions would silently treat it as 26.

Border. The points on the border of the image (its first and last rows and columns, and its first and last planes in 3D) are never modified: pink26 behaves like Pink and its commands htkern, hthin, hthick and lambdaskel. Hence:

  • two minima that touch the border are never merged through the outside of the image, and a crest that runs to the border is kept;
  • a point next to the border is judged with its full 3 x 3 (3 x 3 x 3 in 3D) neighbourhood, which lies inside the image. To have the border processed too, pad the image yourself with one point on each side (for instance by repeating the border values, or with a level of your choice), apply the operator, and crop the result.

Condition image (optional, P26_U8, shape of the image):

  • thinning and kernels: a lower bound, NULL meaning 0 everywhere. A point where condition >= image is never modified, no point is ever raised, and the thinning and the homotopic kernel never lower a point below its condition value. The lambda-kernel follows Pink's llambdakern: it selects only points above their condition value, but lowers a selected point to alpha-(p) or further, even below its condition value (the algorithm of Pink's lambdaskel: "F(p) = alpha-(p,F)" for a point with F(p) > G(p));
  • thickening: an upper bound, NULL meaning 255 everywhere. A point where condition <= image is never modified, no point is lowered, and no point is raised above its condition value.

What pink26 changes in Pink's lambda-kernel (read this before comparing with Pink's command lambdaskel). [CBB01] appendix B (Algorithm lambda-Leveling, which has no condition image) lowers lambda-destructible points until stability. Pink's documentation of lambdaskel adds the condition G: "Repeat until stability: select a point p which is lambda-destructible ... such that F(p) > G(p)". Pink's llambdakern missed points of two kinds, which stayed lambda-destructible (even destructible) above the condition:

  • it started its priority queue only from the neighbours of the regional minima strictly above the condition (F > G). A minimum at the level of the condition, which includes every minimum at level 0 when there is no condition, started nothing: on a bump of height 40 on a background at 0, Pink's llambdakern changes nothing at all;
  • the queue then grew only through the points it lowered. With a condition image, the points that the condition freezes (G >= F) could cut a region off the minima, and nothing reached it. Smallest case found (4 x 3, lambda = 0): the point at 200 in the second row and second column of F = 200 100 100 100 G = 100 0 100 100 200 200 100 200 0 0 100 0 200 200 100 0 200 200 200 0 is destructible, but the only minimum (the 0) reaches it only through the frozen point at 100 next to it (the border points are never lowered); Pink leaves it at 200, pink26 lowers it to 100. One fix covers both (a marked edit of src/legacy/llambdakern.c): pink26 also starts the queue from every lambda-destructible point above its condition, so every such point is processed until stability; the test F(p) > G(p) is still made on each point before lowering it. Its results therefore differ from those of Pink's lambdaskel command. Two behaviours of Pink are kept, in plain words:
  • a peak (a point with no neighbour at its level or above) is lowered only when it is at most lambda above its highest lower neighbour: a peak of larger contrast stays (Pink's lambdadestr4; [CBB01] appendix B and lambdaskel's documentation say "or a peak", which would lower every peak);
  • the condition image only chooses which points may be lowered (those above it): a chosen point is lowered to alpha-(p), and again while it stays lambda-destructible, even below its condition value. So the result of the lambda-kernel is NOT always above the condition; only the points where condition >= image are guaranteed unchanged. Pink implements the lambda-kernel for 4-connected minima in 2D only.

Size limit. Pink's thinning and thickening store a point index and a value in one 32-bit word, with 24 bits for the index: the image may have at most 2^24 = 16777216 points (4096 x 4096, or 256 x 256 x 256). pink26 applies this limit to the four operators (P26_E_UNSUPPORTED), before any call to Pink.

Pink functions used (lhtkern.c, lhtkern3d.c, llambdakern.c; Couprie, ESIEE):

pink26 function                  2D            3D                  command
p26_gray_homotopic_thinning      lhthindelta   lhthindelta3d       hthin
p26_gray_homotopic_thickening    lhthickdelta  lhthickdelta3d      hthick
p26_gray_homotopic_kernel        lhtkern       mctopo3d_lhtkern3d  htkern
p26_gray_lambda_kernel           llambdakern   (none)              lambdaskel

References: [BEC97] G. Bertrand, J.-C. Everat, M. Couprie, "Image segmentation through operators based upon topology", Journal of Electronic Imaging 6(4), 1997, pp. 395-405 (no local copy; checked against reference 4 of [CBB01]). [CBB01] M. Couprie, F. N. Bezerra, G. Bertrand, "Topological operators for grayscale image processing", Journal of Electronic Imaging 10(4), 2001, pp. 1003-1015.

Function Documentation

◆ p26_gray_homotopic_thinning()

p26_status p26_gray_homotopic_thinning ( const p26_image * image,
int connectivity,
const p26_image * condition,
int max_steps,
p26_image ** out )

Grayscale homotopic thinning ([BEC97], [CBB01]; Pink's lhthindelta, lhthindelta3d, command hthin).

Lowers destructible points step by step (see the top of this file). The result is at most the input at every point and every section keeps its topology.

Parameters
imageP26_U8 image, 2D or 3D.
connectivityConnectivity of the minima: 4 or 8 in 2D, 6 or 26 in 3D (the bright regions use the complementary one).
conditionNULL (0 everywhere), or a P26_U8 image of the shape of image: a lower bound (see the top of this file).
max_stepsNumber of steps: any negative value runs until no point changes (the result is then a homotopic kernel); 0 returns a copy of image without calling Pink; k > 0 runs at most k steps. The result after k + 1 steps is at most the result after k steps at every point.
outSet to NULL on entry; receives a new P26_U8 image of the shape of image (released with p26_image_free()).
Returns
P26_OK; P26_E_INVALID_ARG if out is NULL, image or condition is not a valid image, connectivity is not 4 or 8 (2D) or 6, 18 or 26 (3D), or condition has another shape than image; P26_E_UNSUPPORTED if connectivity is 18 in 3D, or if the image has more than 16777216 points; P26_E_TYPE if image or condition is not P26_U8; P26_E_OVERFLOW if a pixel count exceeds PTRDIFF_MAX (p26_image_validate()); P26_E_NOMEM if an allocation of pink26 fails; P26_E_INTERNAL if the admitted Pink code aborted (an allocation failure inside Pink: the memory Pink allocated before is leaked). On error, *out stays NULL.

Complexity: O(N) time per step for N points, and about the size of the bright regions in steps; memory: the copies of image and condition (1 byte per point each), Pink's two point lists (4 bytes per point each) and its marks (1 byte per point).

◆ p26_gray_homotopic_thickening()

p26_status p26_gray_homotopic_thickening ( const p26_image * image,
int connectivity,
const p26_image * condition,
int max_steps,
p26_image ** out )

Grayscale homotopic thickening ([BEC97], [CBB01]; Pink's lhthickdelta, lhthickdelta3d, command hthick).

The dual of p26_gray_homotopic_thinning(): raises constructible points step by step. The result is at least the input at every point and every section keeps its topology.

Parameters
imageAs p26_gray_homotopic_thinning().
connectivityAs p26_gray_homotopic_thinning().
conditionNULL (255 everywhere), or a P26_U8 image of the shape of image: an upper bound (see the top of this file).
max_stepsAs p26_gray_homotopic_thinning(); the result after k + 1 steps is at least the result after k steps.
outAs p26_gray_homotopic_thinning().
Returns
The statuses of p26_gray_homotopic_thinning(). On error, *out stays NULL.

Complexity: as p26_gray_homotopic_thinning().

◆ p26_gray_homotopic_kernel()

p26_status p26_gray_homotopic_kernel ( const p26_image * image,
int connectivity,
const p26_image * condition,
p26_image ** out )

Lower homotopic kernel ([BEC97], [CBB01] section 4.1; Pink's lhtkern, mctopo3d_lhtkern3d, command htkern).

Lowers destructible points, lowest level first, until no point above its condition value is destructible (see the top of this file for the difference with thinning until stability). The result is at most the input, every section keeps its topology, and its minima are spread as much as possible: they are separated by thin crest lines.

Parameters
imageAs p26_gray_homotopic_thinning().
connectivityAs p26_gray_homotopic_thinning().
conditionAs p26_gray_homotopic_thinning() (a lower bound).
outAs p26_gray_homotopic_thinning().
Returns
The statuses of p26_gray_homotopic_thinning(). On error, *out stays NULL.

Complexity: about O(N) time for N points (each point enters Pink's hierarchical queue on 256 levels a bounded number of times); memory: the copies of image and condition, Pink's queue (a few bytes per point) and its marks (1 byte per point).

◆ p26_gray_lambda_kernel()

p26_status p26_gray_lambda_kernel ( const p26_image * image,
int connectivity,
int lambda,
const p26_image * condition,
p26_image ** out )

Lambda-kernel, or filtered leveling kernel ([CBB01] section 5; Pink's llambdakern, command lambdaskel), corrected by pink26 (see "What pink26 changes" at the top of this file).

Lowers lambda-destructible points above their condition value, lowest level first, until none is left. The minima and the peaks of contrast at most lambda disappear; the result is at most the input, and no lambda-destructible point above its condition is left off the border. For lambda = 0 every section keeps its topology. The result may go below the condition (see the top of this file).

Parameters
imageP26_U8 2D image.
connectivityConnectivity of the minima: 4 (Pink implements no other).
lambdaContrast, in [0, 255].
conditionNULL (0 everywhere), or a P26_U8 image of the shape of image: only points above it are selected (see the top of this file).
outAs p26_gray_homotopic_thinning().
Returns
P26_OK; P26_E_INVALID_ARG if out is NULL, image or condition is not a valid image, lambda is outside [0, 255], connectivity is not 4 or 8 in 2D, or condition has another shape than image; P26_E_UNSUPPORTED if connectivity is 8 (Pink's llambdakern implements 4-connected minima only), if image is 3D (Pink provides no 3D lambda-kernel), or if the image has more than 16777216 points; P26_E_TYPE, P26_E_OVERFLOW, P26_E_NOMEM, P26_E_INTERNAL as p26_gray_homotopic_thinning(). On error, *out stays NULL.

Complexity: as p26_gray_homotopic_kernel(), plus a labelling of the minima (O(N), 4 bytes per point).