Exercise: the Hessian
Before this unit: Scale space, blobs and pyramids (previous unit); Day 1: Images, filters and frequencies.
Goal
By the end of this exercise you should be able to:
- write down the Hessian of a simple brightness pattern and find its eigenvalues and eigenvectors,
- estimate the second derivatives from a 3 × 3 patch,
- classify a pixel as bright/dark blob, ridge, saddle or flat,
- compute trace, determinant and vesselness, and predict what they do,
- (math) derive the closed-form eigenvalues and the best scale of a line.
Background
| Idea | What it means |
|---|---|
| Hessian | H = \begin{pmatrix} L_{xx} & L_{xy} \\ L_{xy} & L_{yy}\end{pmatrix} of the image smoothed at scale σ; x right, y down |
| Eigenvalues | the extreme curvatures, ordered \lvert\lambda_1\rvert \le \lvert\lambda_2\rvert; eigenvectors = their directions (perpendicular) |
| Closed form | \lambda = \frac{a + c}{2} \pm \sqrt{\big(\frac{a - c}{2}\big)^2 + b^2} for \begin{pmatrix} a & b \\ b & c\end{pmatrix} |
| Classes | (\lambda_1, \lambda_2) both ≪ 0: bright blob; \lambda_2 \ll 0, \lambda_1 \approx 0: bright ridge; opposite signs: saddle; both ≈ 0: flat; dark = signs flipped |
| Trace, determinant | \lambda_1 + \lambda_2 (= LoG), \lambda_1\lambda_2 |
| Vesselness | \mathcal V = e^{-R_B^2/2\beta^2}\big(1 - e^{-S^2/2c^2}\big) if \lambda_2 < 0 (else 0), R_B = \lambda_1/\lambda_2, S = \sqrt{\lambda_1^2 + \lambda_2^2} |
| Second differences | L_{xx} \approx [1\;-2\;1] along a row, L_{yy} along a column, L_{xy} \approx \tfrac14\big[f(1,1) - f(1,-1) - f(-1,1) + f(-1,-1)\big] |
By hand
B1. For each brightness pattern write the Hessian, its eigenvalues (ordered by size) with eigenvectors, and the class. Sketch the pattern as an image (bright = high). (a) f = -x^2; (b) f = -(x^2 + y^2); (c) f = x^2 - y^2; (d) f = -(x + y)^2.
B2. A 3 × 3 patch of a smoothed image (rows y = -1, 0, 1 from top to bottom, columns x = -1, 0, 1):
| 9 | 5 | 1 |
|---|---|---|
| 5 | 9 | 5 |
| 1 | 5 | 9 |
- Estimate L_{xx}, L_{yy} and L_{xy} at the centre. (b) Find the eigenvalues and the eigenvector of the weaker one. (c) Classify the pixel. In which direction does the structure run? Does that match the picture?
B3. Trace, determinant and vesselness (β = 0.5, c = 15) for (a) λ = (−10, −10); (b) λ = (0, −20); (c) λ = (−2, −20). Which of the three numbers separates the line from the blob best?
B4. A bright disc of radius 10 (brightness 100 on 0). Predict the class (and the sign of the eigenvalues) at σ = 2: at the centre, 8 pixels from the centre (just inside the rim), 12 pixels from the centre (just outside) and 20 pixels from the centre. Then at σ = 7. Explain the difference.
B5. A Hessian at a pixel is \begin{pmatrix} -20 & 2 \\ 2 & -3\end{pmatrix}. Compute the eigenvalues, the class, and the vesselness for β = 0.5, c = 15. What happens to the vesselness if β = 0.1? If c = 50?
B1. (a) H = \begin{pmatrix} -2 & 0 \\ 0 & 0\end{pmatrix}: \lambda_1 = 0, eigenvector (0, 1) (along y); \lambda_2 = -2, eigenvector (1, 0). A bright ridge running vertically (along y). (b) H = \begin{pmatrix} -2 & 0 \\ 0 & -2\end{pmatrix}: \lambda_1 = \lambda_2 = -2, every direction is an eigenvector. A bright blob (hilltop). (c) H = \begin{pmatrix} 2 & 0 \\ 0 & -2\end{pmatrix}: \lambda = 2 (eigenvector (1, 0)) and -2 (eigenvector (0, 1)), equal in size; a saddle: brightness rises left and right, falls up and down. (d) f = -(x^2 + 2xy + y^2), H = \begin{pmatrix} -2 & -2 \\ -2 & -2\end{pmatrix}: \lambda_1 = 0 with eigenvector (1, -1)/\sqrt2, \lambda_2 = -4 with eigenvector (1, 1)/\sqrt2. A bright ridge along the line x + y = 0; with y down, (1, -1) points up and to the right on the screen.
B2. (a) L_{xx} = 5 - 18 + 5 = -8; L_{yy} = 5 - 18 + 5 = -8; L_{xy} = \tfrac14(9 - 1 - 1 + 9) = 4. (b) \lambda = -8 \pm 4: \lambda_1 = -4, \lambda_2 = -12. The eigenvector of -4: \begin{pmatrix} -8 & 4 \\ 4 & -8\end{pmatrix}\begin{pmatrix} 1 \\ 1\end{pmatrix} = -4\begin{pmatrix} 1 \\ 1\end{pmatrix}, so (1, 1)/\sqrt2. (c) |\lambda_1/\lambda_2| = 1/3: one curvature three times the other, both negative: a bright ridge (with this ratio threshold of ½), running along (1, 1), i.e. from top left to bottom right. The patch indeed has its bright values (9) on that diagonal.
B3.
| trace | determinant | vesselness | |
|---|---|---|---|
| (a) blob (−10, −10) | −20 | 100 | 0.05 |
| (b) line (0, −20) | −20 | 0 | 0.59 |
| (c) line (−2, −20) | −22 | 40 | 0.58 |
The trace cannot separate them at all; the determinant can, but it ranks the slightly curved line (c) between; vesselness separates lines (≈ 0.6) from the blob (0.05) clearly.
B4. At σ = 2 (much smaller than the disc): centre flat (λ ≈ 0: the inside of the disc is flat at this scale); 8 px: bright ridge (λ = −5.2, −22.9: the brightness starts to fall towards the rim); 12 px: dark ridge (λ = −3.4, +23.0: it bends up as it reaches the dark floor); 20 px: flat. The rim shows up as two parallel half-ridges, not as a blob. At σ = 7 (≈ r/√2): centre bright blob (λ = −36.9, −36.9); 8 px: bright ridge (−8.0, −26.3); 12 px: saddle (11.9, −16.5); 20 px: dark ridge (−3.1, +14.5). Only when σ matches the disc’s size is the disc seen as one blob.
B5. \lambda = \frac{-23}{2} \pm \sqrt{8.5^2 + 4} = -11.5 \pm 8.73: \lambda_1 = -2.77, \lambda_2 = -20.23. Ratio 0.14: a bright ridge. R_B = 0.137, S = 20.42: \mathcal V = e^{-0.137^2/0.5} \cdot (1 - e^{-20.42^2/450}) = 0.963 \cdot 0.604 = 0.58. With β = 0.1 the blob penalty becomes strict: e^{-0.137^2/0.02} = 0.39, \mathcal V = 0.24. With c = 50 the structure counts as weak: 1 - e^{-20.42^2/5000} = 0.080, \mathcal V = 0.08.
Math questions
B6. (math) Derive the closed-form eigenvalues of \begin{pmatrix} a & b \\ b & c\end{pmatrix} from the characteristic polynomial, and show that they are always real. When are they equal?
B7. (math) Show that trace and determinant of the Hessian do not change when the image is rotated. (Hint: rotating the image by R turns H into R^\top H R.)
B8. (math) A bright line with a Gaussian profile of width s: I(x, y) = A\,e^{-x^2/2s^2}. (a) Show that after smoothing with G_\sigma, L(x) = \frac{As}{\sqrt{s^2 + \sigma^2}}e^{-x^2/2(s^2 + \sigma^2)}. (b) Compute \sigma^2 L_{xx}(0) and find the σ where its size is largest. (c) What does this mean for reading the width of a vessel from the best scale?
B6. \det\begin{pmatrix} a - \lambda & b \\ b & c - \lambda\end{pmatrix} = \lambda^2 - (a + c)\lambda + (ac - b^2) = 0, so \lambda = \frac{a + c}{2} \pm \sqrt{\frac{(a + c)^2}{4} - ac + b^2} = \frac{a + c}{2} \pm \sqrt{\big(\frac{a - c}{2}\big)^2 + b^2}. The square root is of a sum of squares, so it is real. The eigenvalues are equal only if a = c and b = 0, i.e. H is a multiple of the identity (a perfectly round blob or a flat pixel).
B7. If L'(\mathbf x) = L(R\mathbf x), the chain rule gives H' = R^\top H R. Then \operatorname{tr}(R^\top H R) = \operatorname{tr}(H R R^\top) = \operatorname{tr}H (cyclic property, RR^\top = I), and \det(R^\top H R) = \det R^\top \det H \det R = \det H. Equivalently, H' has the same eigenvalues as H; only the eigenvectors rotate.
B8. (a) In x the profile is A\sqrt{2\pi}\,s\,g_s(x); convolving with g_\sigma gives A\sqrt{2\pi}\,s\,g_{\sqrt{s^2 + \sigma^2}}(x) (semigroup), which is the stated expression. (b) With v = s^2 + \sigma^2: L_{xx}(0) = -\frac{As}{\sqrt v}\cdot\frac1v, so \sigma^2 L_{xx}(0) = -As\,\frac{\sigma^2}{v^{3/2}}. Setting the derivative with respect to \sigma^2 to zero: v^{-3/2} - \tfrac32\sigma^2 v^{-5/2} = 0 \Rightarrow v = \tfrac32\sigma^2 \Rightarrow \sigma^2 = 2s^2, \sigma = \sqrt2\,s. (c) The best scale is proportional to the width, so multi-scale vesselness measures vessel width: width s \approx \sigma_\text{best}/\sqrt2 for this normalisation. (Other normalisations, \sigma^{2\gamma} with \gamma = 3/4, make the best scale equal s; the factor depends on the convention, the proportionality does not.)
Explore: the Hessian per pixel
Images: an editable quadratic f = ax^2 + bxy + cy^2 (with contours; its Hessian is shown in the units of f), a test scene (a bright blob, a thin and a thick bright line, a dark line, a saddle of two bright and two dark spots, a step edge at the bottom left; noise 3), curved lines, or the micrograph (256 × 256). Tabs show the class map, λ1, λ2, trace, determinant, vesselness and the best scale. Click a pixel: the glyph shows the eigenvector axes (length ∝ |λ|, red = negative curvature), the readout the matrix, the eigenvalues and the verdict; its point is circled in the (λ1, λ2) plane.
E1. Presets hilltop, ridge, saddle: check your answers to B1(a)–(c) in the readout. Then set a = c = -1, b = -2 for B1(d). Where does the glyph point, and what does the image look like?
E2. Preset an edge is not a ridge (test scene, σ = 3). Click on the step at (25, 114), then one pixel to the left and one and three pixels to the right. Read λ1, λ2 and the class each time. Compare the size of λ2 with that of the thin line at (86, 27).
E3. Preset LoG cannot tell blob from ridge; det can. Compare the trace and then the determinant at the blob (28, 28) and on the thin line (86, 27). Look at the whole trace and determinant images: which structures light up in each?
E4. Preset vesselness: blob suppressed (σ = 3). Read the vesselness at the blob, the thin line and the thick line (30, 70). Untick bright lines: what lights up now? Change β and c and describe their effect.
E5. Preset one σ misses the thick line (σ = 1.5), then multi-scale. Compare the vesselness of the thick line in both. Which σ wins for the thin and for the thick line (best σ tab)? Compare with B8.
E6. Image curved lines, multi-scale, vesselness tab. Where does the vesselness drop along the lines? Why?
E7. Preset membranes in the micrograph (dark lines, σ = 2, c = 20). Which structures get high vesselness? Click on the nuclear envelope at (94, 126). What else responds, and how could you reduce it?
E1. Hilltop: H = [−2 0; 0 −2], λ = (−2, −2), bright blob. Ridge: H = [−2 0; 0 0], λ = (0, −2), bright ridge; the glyph is one red bar across the ridge (the λ1 axis has length 0). Saddle: H = [2 0; 0 −2], λ = (2, −2), a blue and a red bar of equal length. With a = c = -1, b = -2: H = [−2 −2; −2 −2], λ = (0, −4), a bright ridge running from lower left to upper right; the red bar points across it, along (1, 1).
E2. At σ = 3: on the step (25, 114) λ = (−0.15, −3.87): flat (|λ2| below the threshold 4). One pixel left (24, 114): λ = (−0.02, +4.25): a weak dark ridge. One pixel right (26, 114): (−0.27, −10.76), three right (28, 114): (−0.39, −14.52): a bright ridge. The edge appears as a weak dark half-ridge outside, a bright half-ridge inside and nothing on the edge; λ2 there (−14.5) is half that of the thin line (−28.65), although the edge has the same contrast class.
E3. Trace: −31.3 (blob), −28.8 (thin line): almost the same. Determinant: 245 (blob), 2.7 (line). In the trace image all bright structures (blob, lines, the bright side of the edge) are dark (negative) and all dark structures bright; in the determinant image only the blob and the saddle’s spots stand out (the saddle centre itself is strongly negative, −96).
E4. Blob 0.10, thin line 0.84, thick line 0.40. With dark lines selected, the dark line lights up and the bright structures vanish; the dark side of the edge and the dark spots of the saddle respond a little. Larger β lets blobs through (the blob brightens); smaller β darkens everything that is not perfectly line-like. Larger c darkens weak structures first; smaller c brings up the noise.
E5. At σ = 1.5 the thick line scores only 0.06 (the kernel sees the flat top of the line); at σ = 3, 0.40; multi-scale (σ = 1 … 8) gives 0.70, won at σ = 5.94. The thin line wins at σ = 2.44 (0.87). The thick line has width 4.5 and the thin one 1.5: √2 × 4.5 = 6.4 and √2 × 1.5 = 2.1, both close to the winning σ on the 8-level grid, as B8 predicts.
E6. Vesselness drops where two lines cross or meet: there both curvatures are large, R_B approaches 1 and the blob penalty switches the response off. It also drops at line ends, and where a thin line passes near a thick one (they blend at the larger scale).
E7. The nuclear envelope and the membranes and fibres of the cytoplasm, which are dark thin lines; at (94, 126) the vesselness is 0.97. The dense granular texture inside the nucleus and the fibrous background also respond at σ = 2. A larger σ (or the multi-scale maximum with σ ≥ 2) and a larger c suppress the fine texture; but the Hessian alone cannot know which dark lines are “membranes”: that needs a classifier (pixel classification on the random-forest day) or a model of the shape.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in edges/src/hessian/exercise.rs. The Gaussian blur of the edges unit is given, ready to use.
C1. hessian_at(l, x, y): the second differences of the Background table at one pixel, and hessian(img, sigma): blur with σ, then hessian_at at every pixel, times σ² (so that scales can be compared, as in the scale-space unit). The checks are B2(a) and the quadratics of B1(a) and (d).
C2. eigen_sym(h): the eigenvalues of \begin{pmatrix} a & b \\ b & c\end{pmatrix}, ordered by size, and the unit eigenvector of the weaker one:
λ± = (a + c)/2 ± sqrt( ((a − c)/2)² + b² )
λ1, λ2 = λ+ and λ− ordered so that |λ1| ≤ |λ2|
u = (b, λ1 − a) or (λ1 − c, b), whichever is longer, scaled to length 1
The checks are B1, B2(b) and B5.
C3. classify(l1, l2, flat):
if |λ2| < flat: flat
else if |λ1| < ½ |λ2|: ridge (bright if λ2 < 0, dark if λ2 > 0)
else if λ1, λ2 same sign: blob (bright if negative, dark if positive)
else: saddle
The checks are B1, B2(c), and B4: the disc of radius 10 at σ = 2 and σ = 7, at 0, 8, 12 and 20 pixels from its centre.
C4. vesselness(l1, l2, beta, c): Frangi’s formula of the Background table. The checks are B3 and B5.
C5. multiscale_vesselness(img, sigmas, beta, c, bright): the largest vesselness over the scales at every pixel, and the σ where it was largest. For dark lines, flip the signs of λ1 and λ2 first. The check is B8: a line of width s wins near \sigma = \sqrt2\,s.
Then cargo run -p edges -- hessian and open the pictures in out/hessian/. The test scene (grey levels 0 … 255) has a bright spot, a thin and a thick bright line, a thin dark line, a junction where two thin lines cross, and a bright square at the bottom left; cell-dark-lines.png shows the membranes of the micrograph as dark lines. Values are normalised (× σ²).
C6. In classes.png (σ = 3), which class does each structure get? Read the printed λ1, λ2 and eigenvector for the spot, the thin line, the junction and the pixel just inside the square. Which way does the eigenvector point on the thin line?
C7. Compare trace.png with determinant.png, and the printed trace and determinant of the spot and of the thin line. Which one tells them apart?
C8. Compare vesselness-sigma1.5.png with vesselness-multi.png (σ = 1 … 8). Read the printed vesselness of the thin and the thick line at σ = 1.5 and over all scales, and the best σ. Compare with B8. What happens at the junction?
The model solution is edges/src/hessian/solution.rs in the download.
C6. The spot is a bright blob (yellow): λ = (−27.3, −27.7). The thin line is a bright ridge (red) with λ = (−0.10, −35.2) and the eigenvector of λ1 is (−0.01, 1.00): straight down, along the line. The thick line is a bright ridge too. The dark line is a dark ridge (cyan) with λ = (0.55, 10.6). The junction is a bright blob, λ = (−34.2, −34.9): both curvatures are large where two lines cross. The edges of the square show as a red half-ridge just inside (λ2 = −18.6 three pixels in, about half that of the thin line) and a cyan half-ridge just outside; every line is flanked by a thin cyan band, where the brightness bends up again into the background.
C7. The trace is −55.0 at the spot and −35.3 on the thin line: both strongly negative, and in trace.png both lines and the spot are dark. The determinant is 756 at the spot and 3.7 on the line: in determinant.png only the spot, the two crossings and the end of the thick line stand out. The determinant tells them apart; the trace (the LoG) cannot.
C8. At σ = 1.5 the thin line scores 0.91 and the thick line only 0.20: the small kernel sees the flat top of the thick line. Over all scales, the thick line reaches 0.96 at σ = 5.94 and the thin one 0.95 at σ = 2.44. B8 predicts \sqrt2 \cdot 4.5 = 6.4 and \sqrt2 \cdot 1.5 = 2.1: the nearest grid scales win. At the junction the vesselness drops to 0.16 (a dark gap in the bright cross), because there it looks like a blob. The bright side of the square’s edges also responds in the multi-scale picture: the Hessian sees a half-ridge there.
Where this leads
The Hessian measures second derivatives and finds the centres of blobs and lines. The next unit uses first derivatives, squared and averaged over a window: the structure tensor, which measures orientation and finds corners. Later, multi-scale vesselness feeds the skeletons of neurons and vessels (skeletons of networks).