Algorithmadvanced
Determinant of Hessian detector
Also called Hessian blob detector, DoH detector.
Find blobs as scale-space maxima of the scale-normalized determinant of the Hessian of the smoothed image.
- Solves
- Feature detection
- Time
- O(K W H r) worst · average · best
- Space
- Θ(K W H) extra
- Where
- H = the image height in pixels; K = the number of scales; W = the image width in pixels; r = the radius of the largest Gaussian kernel
- Model
- Real RAM
- Properties
- approximately scale invariantdeterministicnot exactheuristicrotation invariant response
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Tony Lindeberg, 1998
The determinant of Hessian detector, or DoH detector, solves feature detection for blobs. It smooths the image with Gaussians of variance over a range of scales, computes at each point and scale the Hessian matrix of second derivatives, and takes the scale-normalized determinant as the response. The points are the positive maxima of the response in space and scale, and the scale of the maximum gives the size of the blob. The determinant is large only where the image curves strongly in two directions, so it does not respond to straight edges and needs no edge test, unlike the DoG detector. It is the second of the two blob measures in Lindeberg's theory of automatic scale selection, next to the Laplacian of Gaussian. SURF computes an approximation of it with box filters. The cost is for scales and kernels of radius at most .
In plain words
Blur the image by several amounts. At each point and each blur, measure how the brightness bends: how much it curves left to right, top to bottom, and diagonally. Combine these three numbers into one, the determinant, which is large only when the brightness bends the same way in every direction, as on the top of a hill or the bottom of a bowl. A bright spot is a hill and a dark spot is a bowl, so both give a large value. An edge bends in one direction only and gives zero. Multiply by the square of the blur, so that small and large spots can be compared fairly. A spot is found where this value is larger than at all neighbours, in the image and across the blurs. The blur at that point gives the size of the spot.
The problem
The input is a gray digital image with columns and rows of real values. A pixel has coordinates : is the column and goes right, is the row and goes down, and and name the derivatives along and . The scale space is , where is the Gaussian of variance ; this page uses a sampled Gaussian cut at and the clamp border rule. The parameters are an increasing list of scales , best with a constant ratio, and a threshold .
The Hessian of is , and the scale-normalized determinant is
The factor is for each of the two derivatives, with : each derivative of order 2 is multiplied by . With this factor, a pattern and the same pattern magnified by give the same maximum value, at scales that differ by . The output is the list of samples where and is larger than at all 26 neighbours in space and scale, refined to sub-sample position and scale.
The algorithm
DOH(I, t_1, …, t_K, τ) ▷ I is W × H, t_1 < … < t_K- 1
for i ← 1 to K do L_i ← g_(t_i) ∗ I ▷ separable Gaussian, clamp border - 2
D_i(u, v) ← t_i² (L_xx L_yy - L_xy²) at each pixel, with central second differences - 3
blobs ← ∅ - 4
for each sample (u, v, i) with 2 ≤ i ≤ K - 1, off the border, D_i(u, v) > τ and larger than all 26 neighbours do - 5
refine u, v and log t by a parabola through the sample and its two neighbours in each direction - 6
add (x̂, ŷ, t̂) to blobs - 7
return blobs
Line L2 uses , the same in for , and . Line L4 keeps only positive maxima. A positive determinant means that both curvatures have the same sign: a bright blob, with both negative, and a dark blob, with both positive, are both maxima of . A negative determinant is a saddle. Line L5 fits a parabola in each of the three directions separately; in scale it uses , since the scales are spaced by a constant ratio.
Example
Take a image of a Gaussian blob with , and the scales for , from to . The smoothed blob is a Gaussian of variance , and in the continuous case the center value of is
which is largest at , with the value . The discrete values at the center are:
| 1 | 1.41 | 2 | 2.83 | 4 | 5.66 | 8 | 11.3 | 16 | |
|---|---|---|---|---|---|---|---|---|---|
| computed | 0.0232 | 0.0340 | 0.0455 | 0.0548 | 0.0590 | 0.0560 | 0.0475 | 0.0361 | 0.0251 |
| continuous | 0.0256 | 0.0372 | 0.0494 | 0.0589 | 0.0625 | 0.0589 | 0.0494 | 0.0372 | 0.0256 |
The largest computed value is at , and the parabola in gives . The detector returns one blob, at with . The computed values are about 5 percent low because the second differences of a sampled Gaussian are smaller than the derivatives. For and the selected scales are and . A dark blob on a white background gives the same point with the same value, and a straight edge gives everywhere, since across a vertical edge.
Correctness
The proof in proofs/correctness.md shows that line L4 returns exactly the strict positive maxima among the 26 neighbours, that the parabola step returns the exact vertex of a parabola, and that for a Gaussian blob of variance the continuous response at the center has its single maximum over at , so the selected scale equals the size of the blob. It also shows the reason for the normalization: under a magnification of the image by , at the corresponding point and the scale is the same, so maxima over scale move with the size of the pattern.
That the maxima are useful points on real images is measured, not proved. In the comparison of Mikolajczyk and others, the Hessian based detectors are among the most repeatable; see detector repeatability.
Proof: Correctness of the determinant of Hessian detectorfull
We prove that line L4 returns the strict positive maxima, that the parabola step is exact on parabolas, that the normalized response is unchanged by a magnification, and that for a Gaussian blob the response selects the scale .
Lemma 1 (the maxima). Line L4 returns exactly the samples with , and at least one pixel from the border, , and for each of the 26 neighbours.
Proof. The loops run over exactly these ranges of , and , so every neighbour is a valid sample. A sample with is skipped. For the others, the test "every neighbour is strictly smaller" is evaluated over the 26 offsets in , and the sample is added exactly when it holds.
Lemma 2 (the parabola step). If with , then the offset computed from , , is .
Proof. , so the step does not return for a flat case, and . The step returns .
At a sample of Lemma 1 the center value is larger than both neighbours in each direction, so the denominator is negative and the offset is in ; the fitted point stays within half a step of the sample.
Lemma 3 (magnification). Let for , in the continuous setting. Then .
Proof. A change of variables in the convolution gives , because and the area element scales by . Each second derivative in is times the one in , so . The factor cancels the .
So a maximum of at becomes a maximum of at , with the same value: the selected scale follows the size of the pattern.
Proposition 4 (the scale of a Gaussian blob). Let in the continuous plane. Then , and as a function of it has a single maximum, at , with the value .
Proof. , and Gaussians add their variances, so . For , at we have and . So , and . Let . Then , which is positive for , zero at and negative for . So is the single maximum, and .
Theorem. DOH returns, for each strict positive maximum of Lemma 1, the point refined by the three parabola steps, with the scale for the offset in scale; and no other point.
Proof. Lines L1 and L2 fill the stack . By Lemma 1, line L4 visits exactly the strict positive maxima. Line L5 computes three offsets as in Lemma 2 and maps the offset in scale to with the half spacing of the two neighbouring scales, which is the spacing when the ratio is constant. Line L6 adds one point per maximum, and nothing else adds points.
Complexity
The model is the real RAM. Let be the radius of the largest kernel, .
- Line L1 is a separable blur, per scale.
- Line L2 costs per scale.
- Line L4 makes at most 26 comparisons per sample, .
- Line L5 is per blob.
The total time is , and the stack takes space. Two things lower the cost in practice. A pyramid halves the image when grows by 4, so the larger kernels run on smaller images. SURF replaces the Gaussian second derivatives by box filters, which an integral image evaluates in per pixel for any size, so the cost is .
Properties
- No edge response. On a straight edge one curvature is zero and so is . The DoG and the Laplacian respond to edges and need a separate edge test.
- Both polarities. Bright and dark blobs both give positive maxima. The sign of the Laplacian at the point tells them apart.
- Scale. The normalization makes the selected scale proportional to the size of the pattern. For a Gaussian blob it is exactly .
- Rotation. The determinant of the Hessian does not change under a rotation of the coordinates. On the pixel grid it changes slightly; for rotations by it is exact.
- Brightness. An offset does not change . A gain multiplies by .
- Corners. is also positive near the corners of objects, where the smoothed image bends in two directions. On a square of side 12, the scales up to 16 give maxima near the four corners and none at the center, whose scale is larger.
Variants and improvements
- Fast Hessian. SURF approximates , and by box filters on an integral image, and uses , where the factor corrects for the box approximation.
- Hessian-Laplace. Choose the position by the determinant of the Hessian and the scale by the Laplacian, as in the Harris-Laplace detector with the Hessian in place of the Harris measure.
- Hessian-affine. Adapt an elliptical shape to each Hessian point by the second moment matrix, as in the Harris-affine detector.
- Laplacian. Use , the trace instead of the determinant, which is the Laplacian of Gaussian blob detector, or its DoG approximation.
- Better localization. Fit a full quadratic in , as the DoG detector does, instead of three separate parabolas.
When to use it
Use it to find blobs at all scales when straight edges must not give points, for example on images of man-made scenes. With box filters it is the detector of SURF and is among the fastest scale invariant detectors. For matching under large changes of viewpoint, add affine adaptation (Hessian-affine) or use MSER.
Pitfalls
- Missing normalization. Without the factor , decreases with for every pattern, and the largest value is always at the smallest scale.
- Taking minima. Negative values of are saddles, not blobs. Search for positive maxima only.
- Too few scales. A maximum needs a scale below and above. The smallest and largest scales cannot hold a blob, so the range must extend past the sizes of interest.
- Unequal steps in scale. The parabola in assumes a constant ratio between scales. With unequal steps, fit with the actual spacing.
- Sampling. For small , the sampled Gaussian and the differences are poor, and the selected scale is biased. Start at or upsample the image.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/determinant-of-hessian-detector/. The authors are listed in prov/698GJR.jsonl.
"""Determinant of Hessian (DoH) blob detector with scale selection.
An image is a list of H rows of W floats; pixel (u, v) is img[v][u], with u
the column (to the right) and v the row (down). The scale parameter is the
variance t = sigma^2. L(., t) is the image blurred with a sampled Gaussian of
variance t (cut at radius ceil(3 sigma), normalized, clamp border).
The response is the scale-normalized determinant of the Hessian,
D(u, v, t) = t^2 (Lxx Lyy - Lxy^2),
with central second differences. A point is a sample (u, v, i), 1 <= i <= K - 2
for K scales, off the border, where D > tau and D is larger than all 26
neighbours in space and scale. It is refined by a parabola in each of u, v
and log t.
"""
import math
from typing import List, Sequence, Tuple
Image = List[List[float]]
def gaussian_kernel(sigma: float) -> List[float]:
"""Sampled Gaussian on -R..R with R = ceil(3 sigma), normalized to sum 1."""
if sigma <= 0:
return [1.0]
rad = max(1, int(math.ceil(3 * sigma)))
k = [math.exp(-(i * i) / (2 * sigma * sigma)) for i in range(-rad, rad + 1)]
s = sum(k)
return [x / s for x in k]
def blur(img: Sequence[Sequence[float]], t: float) -> Image:
"""Separable Gaussian blur of variance t with the clamp border rule."""
h, w = len(img), len(img[0])
k = gaussian_kernel(math.sqrt(t))
r = len(k) // 2
tmp = [[sum(k[j] * row[min(max(u + j - r, 0), w - 1)] for j in range(len(k))) for u in range(w)]
for row in img]
return [[sum(k[j] * tmp[min(max(v + j - r, 0), h - 1)][u] for j in range(len(k))) for u in range(w)]
for v in range(h)]
def doh_response(img: Sequence[Sequence[float]], t: float) -> Image:
"""t^2 (Lxx Lyy - Lxy^2) at each pixel; 0 on the one-pixel border."""
L = blur(img, t)
h, w = len(L), len(L[0])
out = [[0.0] * w for _ in range(h)]
for v in range(1, h - 1):
for u in range(1, w - 1):
c = L[v][u]
lxx = L[v][u + 1] + L[v][u - 1] - 2 * c
lyy = L[v + 1][u] + L[v - 1][u] - 2 * c
lxy = (L[v + 1][u + 1] - L[v + 1][u - 1] - L[v - 1][u + 1] + L[v - 1][u - 1]) / 4
out[v][u] = t * t * (lxx * lyy - lxy * lxy)
return out
def scale_maxima(stack: List[Image], tau: float) -> List[Tuple[int, int, int]]:
"""Samples (i, u, v) off the border of space and scale with value > tau and > all 26 neighbours."""
out = []
k, h, w = len(stack), len(stack[0]), len(stack[0][0])
for i in range(1, k - 1):
for v in range(1, h - 1):
for u in range(1, w - 1):
x = stack[i][v][u]
if x <= tau:
continue
if all(stack[i + a][v + b][u + c] < x
for a in (-1, 0, 1) for b in (-1, 0, 1) for c in (-1, 0, 1) if a or b or c):
out.append((i, u, v))
return out
def parabola_offset(fm: float, f0: float, fp: float) -> float:
"""Offset of the vertex of the parabola through (-1, fm), (0, f0), (1, fp); 0 if flat."""
den = fm - 2 * f0 + fp
if den >= 0:
return 0.0
return 0.5 * (fm - fp) / den
def doh_blobs(img: Sequence[Sequence[float]], scales: Sequence[float], tau: float = 0.0):
"""Return blobs (x, y, t, D) for the scale list t_0 < t_1 < ... (best with a constant ratio).
Precondition: img is a rectangular list of rows of floats, at least 3 x 3,
and scales has at least 3 increasing positive values.
"""
stack = [doh_response(img, t) for t in scales] # @line L1 @line L2
blobs = [] # @line L3
for i, u, v in scale_maxima(stack, tau): # @line L4
S = stack[i]
du = parabola_offset(S[v][u - 1], S[v][u], S[v][u + 1]) # @line L5
dv = parabola_offset(S[v - 1][u], S[v][u], S[v + 1][u])
di = parabola_offset(stack[i - 1][v][u], S[v][u], stack[i + 1][v][u])
lo, hi = math.log(scales[i - 1]), math.log(scales[i + 1])
lt = math.log(scales[i]) + di * ((hi - lo) / 2)
blobs.append((u + du, v + dv, math.exp(lt), S[v][u])) # @line L6
return blobs # @line L7
test_determinant_of_hessian_detector.py 124 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/determinant-of-hessian-detector/. The authors are listed in prov/698GJR.jsonl.
import math
import random
from determinant_of_hessian_detector import (
doh_blobs,
doh_response,
gaussian_kernel,
parabola_offset,
scale_maxima,
)
SCALES = [2 ** (i / 2) for i in range(9)] # 1, 1.41, 2, ..., 16
def blob(n, cx, cy, t0, amp=1.0):
return [[amp * math.exp(-((u - cx) ** 2 + (v - cy) ** 2) / (2 * t0)) for u in range(n)] for v in range(n)]
def direct_response(img, t):
"""2D convolution, then the Hessian by the same differences; independent of the separable code."""
k = gaussian_kernel(math.sqrt(t))
r = len(k) // 2
h, w = len(img), len(img[0])
L = [[sum(k[i + r] * k[j + r] * img[min(max(v + j, 0), h - 1)][min(max(u + i, 0), w - 1)]
for i in range(-r, r + 1) for j in range(-r, r + 1)) for u in range(w)] for v in range(h)]
out = [[0.0] * w for _ in range(h)]
for v in range(1, h - 1):
for u in range(1, w - 1):
# A 3 x 3 Taylor fit gives the same second differences.
m = [[L[v + b][u + a] for a in (-1, 0, 1)] for b in (-1, 0, 1)]
lxx = m[1][2] - 2 * m[1][1] + m[1][0]
lyy = m[2][1] - 2 * m[1][1] + m[0][1]
lxy = (m[2][2] - m[2][0] - m[0][2] + m[0][0]) / 4
out[v][u] = t * t * (lxx * lyy - lxy ** 2)
return out
def oracle_maxima(stack, tau):
out = []
for i in range(1, len(stack) - 1):
for v in range(1, len(stack[0]) - 1):
for u in range(1, len(stack[0][0]) - 1):
x = stack[i][v][u]
nb = [stack[i + a][v + b][u + c] for a in (-1, 0, 1) for b in (-1, 0, 1) for c in (-1, 0, 1)]
nb.remove(x)
if x > tau and max(nb) < x:
out.append((i, u, v))
return out
def test_response_matches_direct_convolution():
rng = random.Random(8)
for _ in range(10):
w, h = rng.randint(3, 7), rng.randint(3, 7)
img = [[rng.random() for _ in range(w)] for _ in range(h)]
for t in (0.5, 1.5):
a, b = doh_response(img, t), direct_response(img, t)
assert all(abs(x - y) < 1e-12 for ra, rb in zip(a, b) for x, y in zip(ra, rb))
def test_scale_maxima_against_oracle():
rng = random.Random(1998)
for _ in range(600):
k, h, w = rng.randint(3, 5), rng.randint(3, 6), rng.randint(3, 6)
stack = [[[rng.randint(-3, 3) for _ in range(w)] for _ in range(h)] for _ in range(k)]
tau = rng.choice([-10, 0, 1])
assert sorted(scale_maxima(stack, tau)) == sorted(oracle_maxima(stack, tau))
def test_parabola_offset():
for x0 in (-0.5, -0.2, 0.0, 0.3, 0.5):
f = lambda x: 3 - 2 * (x - x0) ** 2
assert abs(parabola_offset(f(-1), f(0), f(1)) - x0) < 1e-12
assert parabola_offset(1.0, 1.0, 1.0) == 0.0
def test_worked_example_blob():
img = blob(32, 16, 16, 4.0)
(x, y, t, d), = doh_blobs(img, SCALES, 1e-5)
assert (x, y) == (16.0, 16.0)
assert abs(t - 4.12) < 0.01 and abs(d - 0.05898) < 1e-5
# Continuous value at the centre: t0^2 t^2 / (t0 + t)^4, which is 1/16 at t = t0.
centre = [doh_response(img, s)[16][16] for s in SCALES]
for s, c in zip(SCALES, centre):
want = 16 * s * s / (4 + s) ** 4
assert abs(c - want) < 0.1 * want
assert max(range(9), key=lambda i: centre[i]) == 4 # t = 4
def test_selected_scale_follows_blob_variance():
for t0 in (2.0, 4.0, 8.0):
(x, y, t, d), = doh_blobs(blob(32, 16, 16, t0), SCALES, 1e-5)
assert abs(t - t0) < 0.08 * t0
assert abs(x - 16) < 1e-9 and abs(y - 16) < 1e-9
def test_dark_blob_and_offset_give_the_same_blob():
img = blob(32, 16, 16, 4.0)
a = doh_blobs(img, SCALES, 1e-5)
b = doh_blobs([[1 - p for p in r] for r in img], SCALES, 1e-5)
c = doh_blobs([[p + 5 for p in r] for r in img], SCALES, 1e-5)
for other in (b, c):
assert len(other) == 1 and all(abs(p - q) < 1e-9 for p, q in zip(a[0], other[0]))
def test_straight_edge_and_flat_give_nothing():
edge = [[1.0 if u >= 16 else 0.0 for u in range(32)] for v in range(32)]
assert all(abs(x) < 1e-12 for row in doh_response(edge, 2.0) for x in row)
assert doh_blobs(edge, SCALES, 1e-5) == []
assert doh_blobs([[0.3] * 10 for _ in range(10)], [1, 2, 4], 0.0) == []
def test_rotation_by_90_degrees():
rng = random.Random(31)
h, w = 7, 9
img = [[rng.random() for _ in range(w)] for _ in range(h)]
rot = [[img[h - 1 - j][i] for j in range(h)] for i in range(w)]
a, b = doh_response(img, 1.0), doh_response(rot, 1.0)
for v in range(h):
for u in range(w):
assert abs(a[v][u] - b[u][h - 1 - v]) < 1e-12
History
Paul Beaudet proposed the determinant of the Hessian as a rotation invariant interest operator in 1978, at a single scale. Tony Lindeberg built the theory of scale selection in the 1990s and showed in his 1998 paper that the scale-normalized determinant and the scale-normalized Laplacian, with , select the scale of a blob, and he used both as blob detectors. Mikolajczyk and Schmid used the Hessian in the Hessian-Laplace and Hessian-affine detectors, and Bay and others made it fast with box filters in SURF in 2006, with the journal version in 2008.
References
- Tony Lindeberg, Feature detection with automatic scale selection, International Journal of Computer Vision 30(2), 79-116, 1998, the sections on blob detection (the scale-normalized Laplacian and the scale-normalized determinant of the Hessian, and the scale selected for a Gaussian blob)
- Herbert Bay, Andreas Ess, Tinne Tuytelaars and Luc Van Gool, Speeded-up robust features (SURF), Computer Vision and Image Understanding 110(3), 346-359, 2008, the section on the fast Hessian detector (box filters on an integral image)
- Krystian Mikolajczyk, Tinne Tuytelaars, Cordelia Schmid, Andrew Zisserman, Jiri Matas, Frederik Schaffalitzky, Timor Kadir and Luc Van Gool, A comparison of affine region detectors, International Journal of Computer Vision 65(1-2), 43-72, 2005, the section on the Hessian-affine detector
- Tinne Tuytelaars and Krystian Mikolajczyk, Local invariant feature detectors, a survey, Foundations and Trends in Computer Graphics and Vision 3(3), 177-280, 2008, the part on blob detectors (the Hessian detector)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 698GJR Edit this page History Report an error Markdown