Exercise: scale space, blobs and pyramids

Goal

By the end of this exercise you should be able to:

  • read a scale-space picture and explain why the Gaussian never creates structure,
  • find the best scale of a blob from its normalised response, and turn a scale into a size (radius ≈ √2·σ),
  • compute a difference of Gaussians and compare it with the LoG,
  • work out the size and cost of an image pyramid,
  • (math) derive the scale-selection results and the DoG approximation.

Background

Idea What it means
Scale space L(x, y; \sigma) = G_\sigma * I for all σ; blurs chain: \sigma^2 adds
LoG \nabla^2 L = L_{xx} + L_{yy}; zero crossings at edges; -\nabla^2 L large at bright blobs
Normalised LoG -\sigma^2\nabla^2 L: comparable across scales; peaks at the blob’s own scale
Size from scale disc of radius r: peak at \sigma = r/\sqrt2 (draw radius \sqrt2\,\sigma); Gaussian spot of width s: \sigma = s; 1D box of half-width a: \sigma = a
Detection local maxima of -\sigma^2\nabla^2 L in (x, y, \sigma) (26 neighbours) above a threshold
DoG G_{k\sigma} * I - G_\sigma * I \approx (k - 1)\sigma^2\nabla^2 L
Pyramids Gaussian: blur, keep every 2nd pixel, repeat (total 4/3 of the image); Laplacian: level − upsampled next level, exactly invertible

By hand

B1. A 1D signal is 0 except for a box of width 6 (half-width 3) and height 100. Its normalised response -\sigma^2 L_{xx} at the centre of the box is:

σ 1 2 3 4 6
response 2.7 38.9 48.4 45.2 35.2
  1. Which σ wins? What half-width does that give? (b) A second box elsewhere in the signal gives its largest response at σ = 6. How wide is it? (c) Without normalisation, the responses would be the values above divided by σ². Which σ would win then?

B2. Sizes from scales. (a) A blob is detected at σ = 5 pixels. What is its radius? At 0.2 µm per pixel, what is its diameter in µm? (b) Nuclei of about 10 µm diameter are imaged at 0.2 µm per pixel. At which σ will they respond best? Is a scale range of σ = 1 … 16 enough?

B3. DoG by hand. A 1D Gaussian bump (width 3, height 100) blurred at σ = 2 and at kσ = 2.4 (k = 1.2), at x = -4, -2, 0, 2, 4:

x −4 −2 0 2 4
L at σ = 2 44.97 71.34 83.21 71.34 44.97
L at σ = 2.4 45.41 68.19 78.09 68.19 45.41
L_{xx} at σ = 2 0.80 −3.80 −6.40 −3.80 0.80
  1. Compute the DoG row (σ = 2.4 minus σ = 2). (b) Compute (k - 1)\sigma^2 L_{xx}. How close are they? (c) Where is the DoG most negative, and what does a negative value mean here?

B4. Pyramids. (a) List the side lengths of the Gaussian pyramid of a 1024 × 1024 image down to 1 × 1, and the total number of pixels. Compare with \tfrac43 \cdot 1024^2. (b) A Gaussian blur with σ = 16 (kernel cut at 4σ) applied separably to the full image: how many multiplications? The same smoothing is roughly a σ = 1 blur at level 4 (64 × 64): how many multiplications there? (Building levels 1–4 with σ = 1 blurs costs about 25 million.) (c) Why must each level be blurred before taking every second pixel? What would go wrong with a fine stripe pattern?

B5. Predict. You run blob detection over σ = 1 … 16 on an image, then on the same image enlarged ×2 (each pixel becomes 2 × 2). (a) What happens to the scale and the radius of each detection? To the normalised response values? To the number of detections? (b) What would happen to the raw (unnormalised) response values?

B6. A row of LoG values across an edge: 3.1, 2.4, 0.9, −0.9, −2.4, −3.1 (pixels 0 to 5). Where is the edge (Marr–Hildreth)? Which side is brighter?

B1. (a) σ = 3 (48.4) wins: the half-width is 3, as expected for a box (\sigma = a). (b) Half-width 6, so width 12. (c) Divided by σ²: 2.7, 9.7, 5.4, 2.8, 1.0: σ = 2 would win, too small. Without normalisation the curve is pushed towards small σ (here it still has a maximum, at a/\sqrt3 \approx 1.7 for a box; for a disc in 2D at r/2).

B2. (a) r = \sqrt2 \cdot 5 = 7.07 pixels; diameter 14.1 pixels = 2.83 µm. (b) Diameter 10 µm = 50 pixels, radius 25 pixels, so \sigma = 25/\sqrt2 = 17.7: just above the range; σ should go up to about 20 (or the image should be shrunk ×2 first, i.e. one pyramid level down).

B3. (a) DoG: 0.45, −3.15, −5.12, −3.15, 0.45. (b) (k - 1)\sigma^2 = 0.2 \cdot 4 = 0.8, so 0.8\,L_{xx} = 0.64, −3.04, −5.12, −3.04, 0.64. At the centre they agree to two decimals; in the tails the DoG is a little smaller (the approximation has an error of order (k - 1)^2). (c) Most negative at the centre, x = 0: blurring more lowers the peak (the bump flattens), which is what a negative curvature means. With the sign flipped, -\text{DoG} is the Mexican-hat response, largest at the blob’s centre.

B4. (a) 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1: 11 levels, 1\,398\,101 pixels in total, = \tfrac43 \cdot 1024^2 = 1\,398\,101.3 (the geometric series stops at 1 × 1). (b) σ = 16: 2 \cdot 4 \cdot 16 + 1 = 129 taps, 2 \cdot 129 = 258 multiplications per pixel, 258 \cdot 1024^2 \approx 271 million. At level 4: 9 taps, 18 \cdot 64^2 = 73\,728; with the 25 million for building the levels, about 10 times cheaper, and the other levels come for free. (c) Taking every second pixel can only represent detail coarser than 2 pixels (Nyquist, Fourier unit). Finer detail is not removed but aliased: a fine stripe pattern turns into false coarse stripes (moiré). The blur removes the fine detail first.

B5. (a) Every detection moves to σ × 2 and radius × 2, at position × 2; the normalised responses stay the same (scale invariance), and the number of detections stays the same, provided 2σ is still inside the scale range (detections near σ = 16 drop out; new ones at σ < 2 may appear only from finer detail, of which there is none). (b) Raw values drop by a factor of 4 (second derivatives scale as 1/\text{length}^2): the same blobs would look weaker, which is why the raw LoG cannot compare scales.

B6. The zero crossing is between pixels 2 and 3, halfway (0.9 and −0.9): the edge is at 2.5. The LoG is positive on the dark side and negative on the bright side, so the brighter side is towards pixel 5.

Math questions

B7. (math) (a) For a 1D box of half-width a, show that \sigma^2 L_{xx}(0) = -2\frac{a}{\sigma}\varphi\big(\frac{a}{\sigma}\big) (\varphi = standard normal density) and that its size is maximal at \sigma = a. (b) For a 2D disc of radius r and height 1, use \nabla^2 L(0) = \oint_{|\mathbf u| = r}\nabla G_\sigma\cdot\mathbf n\,ds to show \sigma^2\nabla^2 L(0) = -\frac{r^2}{\sigma^2}e^{-r^2/2\sigma^2}, maximal in size at r = \sqrt2\,\sigma.

B8. (math) Show that the 2D Gaussian G_\sigma(x, y) = \frac{1}{2\pi\sigma^2}e^{-(x^2 + y^2)/2\sigma^2} satisfies \partial_\sigma G_\sigma = \sigma\,\nabla^2 G_\sigma, and conclude G_{k\sigma} - G_\sigma \approx (k - 1)\sigma^2\nabla^2 G_\sigma.

B9. (math) A Gaussian blob of width s: I = A\,e^{-(x^2 + y^2)/2s^2}. Show that L = G_\sigma * I = \frac{A s^2}{s^2 + \sigma^2}e^{-(x^2 + y^2)/2(s^2 + \sigma^2)} (semigroup), compute \sigma^2\nabla^2 L(0) and find the σ where it is largest in size.

B7. (a) L(x) = \int_{-a}^{a} g_\sigma(x - u)\,du = \Phi\big(\frac{x + a}{\sigma}\big) - \Phi\big(\frac{x - a}{\sigma}\big). Differentiating twice: L_{xx}(x) = \frac{1}{\sigma^2}\big[\varphi'\big(\frac{x + a}{\sigma}\big) - \varphi'\big(\frac{x - a}{\sigma}\big)\big] with \varphi'(t) = -t\varphi(t), so L_{xx}(0) = \frac{1}{\sigma^2}\big[-\frac{a}{\sigma}\varphi(\frac a\sigma) - \frac{a}{\sigma}\varphi(\frac a\sigma)\big] and \sigma^2 L_{xx}(0) = -2u\varphi(u) with u = a/\sigma. \frac{d}{du}u e^{-u^2/2} = (1 - u^2)e^{-u^2/2} = 0 at u = 1: \sigma = a. (b) \nabla^2 L(0) = \int_{\text{disc}}\nabla^2 G_\sigma(\mathbf u)\,d\mathbf u (the Gaussian is symmetric), and the divergence theorem turns this into the flux of \nabla G_\sigma through the circle. On the circle \nabla G_\sigma\cdot\mathbf n = \partial_r G_\sigma = -\frac{r}{\sigma^2}\frac{1}{2\pi\sigma^2}e^{-r^2/2\sigma^2}, constant, times the length 2\pi r: \nabla^2 L(0) = -\frac{r^2}{\sigma^4}e^{-r^2/2\sigma^2}, so \sigma^2\nabla^2 L(0) = -v\,e^{-v/2} with v = r^2/\sigma^2, largest in size at v = 2: r = \sqrt2\,\sigma.

B8. \partial_\sigma G_\sigma = G_\sigma\big(-\frac{2}{\sigma} + \frac{x^2 + y^2}{\sigma^3}\big). And \partial_{xx}G_\sigma = G_\sigma\big(\frac{x^2}{\sigma^4} - \frac{1}{\sigma^2}\big), likewise in y, so \nabla^2 G_\sigma = G_\sigma\big(\frac{x^2 + y^2}{\sigma^4} - \frac{2}{\sigma^2}\big) and \sigma\nabla^2 G_\sigma = \partial_\sigma G_\sigma. A first-order Taylor step in σ: G_{k\sigma} - G_\sigma \approx (k\sigma - \sigma)\,\partial_\sigma G_\sigma = (k - 1)\sigma^2\nabla^2 G_\sigma.

B9. I = 2\pi s^2 A\,G_s, so L = 2\pi s^2 A\,G_{\sqrt{s^2 + \sigma^2}} = \frac{A s^2}{s^2 + \sigma^2}e^{-r^2/2(s^2 + \sigma^2)}. With v = s^2 + \sigma^2, \nabla^2 e^{-r^2/2v} at r = 0 is -2/v, so \sigma^2\nabla^2 L(0) = -\frac{2As^2\sigma^2}{(s^2 + \sigma^2)^2}. Maximise \sigma^2/(s^2 + \sigma^2)^2: the derivative in \sigma^2 is \frac{(s^2 + \sigma^2) - 2\sigma^2}{(s^2 + \sigma^2)^3} = 0 at \sigma = s, where the value is -A/2.

Explore: a 1D scale space

A 1D signal of 256 pixels: three box bumps of half-widths 2, 6 and 18 (at x ≈ 40, 100, 180); a step and a ramp; a bright line in a dark band; or a line through the micrograph. Top: the signal and its blur at the current σ. Middle: the scale-space image (x across, σ from 0.5 to 32 upwards on a log axis) of L, the slope or the curvature, with features traced as dots. Bottom: the response at the clicked x against σ, normalised (red) and raw (grey). Click the scale-space image to choose both x and σ.

S1. Preset noise lives at small σ (noise 10, L). Count roughly how many dots (extrema of L) there are along the bottom row of the scale-space image and how many survive at σ ≈ 4. Where do the surviving curves lead?

S2. Preset raw LoG prefers small σ (curvature, not normalised, x = 99, the centre of the bump with half-width 6). At which σ does the grey (raw) curve peak, and at which the red (normalised) one? Click the centres of the other two bumps: compare the heights of their raw and of their normalised peaks.

S3. Preset normalised: each bump picks its σ. Read the peak σ for the three bumps and compare with their half-widths 2, 6 and 18.

S4. Preset edges and bumps as curves (step and ramp, slope \sigma L_x). Click on the step (x = 69) and on the middle of the ramp (x = 170). How does \sigma L_x change with σ at each? Why does the step give the same value at every scale?

S5. Preset box kernel creates structure (σ = 8). What happens to the bright line in the blurred signal? Untick box kernel: what does the Gaussian of the same σ do? Find the σ at which the box blur first splits the line.

S1. At σ = 0.5 the noisy signal has 143 extrema; at σ ≈ 1, 93; at σ = 2, 45; at σ ≈ 3.9, only 18. The noise extrema form short curves at the bottom that end quickly; the curves that continue upwards belong to the three bumps (and the dips between them). Curves end as σ grows; none starts.

S2. Raw: peak at σ = 3.39 (value 2.57); normalised: σ = 6.14 (value 48.3). Raw peaks are 23.77 (half-width 2), 2.57 (6) and 0.29 (18): the small bump wins by far, although all three have the same height. Normalised peaks are 46.5, 48.3 and 48.6: equal, as they should be for the same shape at different sizes.

S3. σ = 2.00, 6.14 and 17.67, close to the half-widths 2, 6 and 18 (the demo’s 64 scales are spaced by 7 %, so 6 and 18 fall between grid points).

S4. At the step \sigma L_x = 43.3 (σ ≈ 1), 46.9 (2), 47.6 (3.9), 47.8 (8), 47.9 (15.5): constant. A step looks the same at every scale; normalised, its response is 120/\sqrt{2\pi} = 47.9 for a step of 120. At the ramp centre (slope 100/60 per pixel): −1.6, −3.3, −6.5, −13.3, −24.5, i.e. \sigma times the slope, growing until σ is comparable to the ramp’s length (60 pixels). A ramp is an edge at a coarse scale.

S5. With the box (27 pixels wide at σ = 8) the line becomes two bumps with a dip in between (5 extrema instead of 3: at the centre L = 46.7, at x = 118 it is 51.1). The Gaussian keeps one maximum at the centre (54.9). On the demo’s grid of scales the box first splits the line at σ = 8.00 (27 pixels wide); at σ = 7.49 (25 pixels) it still has 3 extrema. The Gaussian never does, at any σ.

Explore: blobs over scales

A 256 × 256 synthetic image (discs of radii 2–20 px, Gaussian spots, a touching pair, an elongated blob, a faint disc, on an uneven background with noise; the truth is known) or the micrograph. The normalised response -\sigma^2\nabla^2 L (or the DoG) is computed for 16 scales σ = 1 … 16. Detections are circles of radius \sqrt2\,\sigma, coloured by scale. The middle panel shows the response at the current σ (0 = grey), the plot below it the response at the clicked pixel against σ. The readout scores detections against the truth (a hit is within max(3, r/2) pixels of a true centre). Pyramid shows the Gaussian and Laplacian pyramids.

E1. Preset one σ finds one size (σ = 4.39, maxima at this σ only). How many detections, how many of the 15 blobs are hit? Which are missed? Move σ to 1.45 and to 9.19: which blobs are found now?

E2. Preset all scales, normalised (threshold 26). How many detections and hits? Which blob is missed, and which false alarms remain? Click the disc at (146, 30) (true radius 6): at which σ does its curve peak, and what radius does that give?

E3. Preset raw LoG: only the small spots (normalise off, threshold 12). At which σ are all detections? Which blobs are found? Click the centre of the disc at (120, 92) and compare the grey and red curves.

E4. Preset radius = √2·σ (truth overlay). Compare the circles with the dashed truth for the discs of radius 3, 9 and 20. Are the radii right? What about the Gaussian spots?

E5. Presets touching blobs and elongated blob. Describe what is detected in each case and why.

E6. Preset low threshold: edges respond too (threshold 18). Where do the extra detections appear, and at which scales? Compare the response of an edge with that of a blob.

E7. Preset DoG matches LoG (k = 1.2). Compare detections with all scales, normalised. Increase k to 1.6: what changes?

E8. Preset uneven background does not matter (σ = 16). Look at the response map: does the bright patch in the lower right (no blobs) give a strong response? Why not?

E9. Preset the pyramid. How many pixels does the Gaussian pyramid hold, and what is the reconstruction error of the Laplacian pyramid? Why is it not exactly 0 in the readout?

E10. Preset dark granules in the micrograph (dark blobs, threshold 50). What is detected? Click the dark granule near (106, 68): at which σ does it respond best?

E1. At σ = 4.39: 13 detections, 8 hits, 5 false alarms; missed are the smallest discs (r = 2, 3) and the large ones (r = 12, 16, 20), the elongated blob and the faint disc. All circles have the same radius \sqrt2 \cdot 4.39 = 6.2. At σ = 1.45 only the small blobs are hit (4 hits, 12 detections); at σ = 9.19 the large ones (8 hits, 12 detections) but not the small. One σ finds one size.

E2. 28 detections, 14 hits, 14 false alarms; missed is the elongated blob (its centre is not a maximum; detections lie along it). Of the false alarms, 9 sit along the elongated blob, 2 in the notch of the touching pair, and 3 at the rims of discs. The disc at (146, 30) peaks at σ = 3.65: radius \sqrt2 \cdot 3.65 = 5.2 (true 6; the scales are 20 % apart, and the predicted σ = 4.24 lies between 3.65 and 4.39).

E3. Every detection is at the smallest scale, σ = 1 (radius 1.4): 146 detections, mostly noise, and only 5 hits (the discs of radius 2, 3, 4 and the two small Gaussian spots). At (120, 92) the raw curve is tiny and peaks at σ = 7.64; the normalised one peaks at σ = 11.06 with value 36.1 (radius 15.6, true 16).

E4. Radius 3: σ = 2.09, circle 3.0; radius 9: σ = 6.35, circle 9.0; radius 20: σ = 13.30, circle 18.8. Within one scale step (20 %) of the truth, slightly small for the largest. Gaussian spots of width 2, 4, 7 are found at σ = 2.09, 3.65, 7.64, close to their widths; their dashed circles are drawn at \sqrt2 s.

E5. Touching pair (two discs of radius 10 touching at (54, 215)): two circles at σ = 6.35 (radius 9.0) on the discs, plus one large circle at σ = 11.06 around both (and a small false alarm in the notch): the pair is also a blob at a coarser scale. Elongated blob (26 × 5): a row of small detections along it at σ = 1.2 to 5.3: the LoG fits the width (5), not the length.

E6. 102 detections (88 false alarms): small circles along the rims of the larger discs (35 on the disc of radius 20 alone), along the elongated blob and around the touching pair, almost all at small σ (1.2 to 3.65). The normalised response of a straight edge is at most 0.24 \times its contrast, that of a blob of the same contrast 0.74 \times: about a third. A threshold between the two separates them, as long as blob contrasts are not too different.

E7. At k = 1.2: 21 detections, 13 hits (8 false alarms), nearly the same blobs as the LoG; the smallest disc (r = 2) is missed. At k = 1.6: 17 detections, 12 hits; the faint disc also goes. Larger k smooths over a wider range of scales: fewer false alarms, but the response is less selective and weak or tiny blobs drop below the threshold.

E8. Hardly: at the centre of the patch (180, 192) the noise-free response at σ = 16 is only 5.0, far below the threshold. A smooth background is almost linear over the width of the Mexican hat, and the LoG of a constant or linear function is 0. A global threshold on the image itself would be thrown off by the same patch.

E9. 87 296 pixels for 256² (65 536) and its levels: 4/3 as many. The reconstruction error is about 10^{-6}: exact up to the rounding of 32-bit floating-point numbers (the telescoping sum is exact in real arithmetic).

E10. Dark granules, vesicles and dark spots in the fibrous background: 144 detections at threshold 50 (no truth for this image). The granule near (106, 68) responds best at σ = 3.65 (radius 5.2). The nucleus itself, about 45 pixels in radius, would need σ ≈ 32, outside the range.

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/scale_space/exercise.rs. The Gaussian blur of the edges unit is given, ready to use.

C1. normalised_log(img, sigma): the normalised LoG, with bright blobs positive, and zero_crossings(row):

L = I blurred with a Gaussian of width σ
for every pixel (x, y):
    Lxx = L(x−1, y) − 2 L(x, y) + L(x+1, y)
    Lyy = L(x, y−1) − 2 L(x, y) + L(x, y+1)
    out(x, y) = −σ² · (Lxx + Lyy)

zero crossing between i and i+1 (values a and b of opposite sign) at  i + a / (a − b)

The checks are B1 (the box of half-width 3: σ = 3 wins), a disc of radius 6 (best σ near 6/\sqrt2 = 4.24) and B6 (the edge at 2.5).

C2. detect_blobs(stack, sigmas, threshold): maxima in (x, y, \sigma).

for every scale k and every pixel (x, y) not on the edge of the image:
    v = stack[k](x, y)
    a blob if v > threshold and v > every neighbour stack[k + dk](x + dx, y + dy)
        for dk, dx, dy in −1, 0, 1 (not all 0; only scales that exist)

The check finds two discs of radius 4 and 10 at the right scale (\sqrt2\,\sigma within 25 % of the radius).

C3. dog(img, sigma, k): the image blurred at k\sigma minus the image blurred at σ. The check is B3: −5.12 at the centre of the bump.

C4. The pyramids: downsample (blur with σ = 1, keep every second pixel), upsample (back to twice the size by bilinear interpolation), and

laplacian_pyramid:  g = I
                    repeat levels − 1 times:  g2 = downsample(g);  store g − upsample(g2);  g = g2
                    store g
reconstruct:        g = last level
                    for the other levels l, from the second last to the first:  g = l + upsample(g)

The checks are B4 (64 × 64 gives sides 64, 32, …, 1 and 5461 pixels in all) and an exact reconstruction.

Then cargo run -p edges -- scale-space and open the pictures in out/scale-space/. The test image has discs of radii 3 to 16, a touching pair of radius 10 and an elongated blob (26 × 5), on a background that brightens to the right; the program detects blobs over 16 scales, σ = 1 … 16, and draws them as circles of radius \sqrt2\,\sigma in blobs-detected.png.

C5. How many detections are there, and how many of the 9 discs are hit? Compare the printed radii \sqrt2\,\sigma with the true radii. What is detected at the touching pair and at the elongated blob, and why?

C6. The program also detects with the raw LoG (the normalised one divided by σ² again). How many detections, at which σ, and how many discs are hit? Then compare the printed DoG (k = 1.2, σ = 4) with -(k - 1) times the normalised LoG at the centre of the disc of radius 8.

C7. Look at gaussian-pyramid.png and laplacian-pyramid.png. How many pixels do the five Gaussian levels hold, compared with the image? What is the largest error of the reconstruction, and why is it not exactly 0?

The model solution is edges/src/scale_space/solution.rs in the download.

C5. 13 detections; all 9 discs are hit. The radii come out as 2.96, 5.16, 7.46, 13.00, 15.63, 6.20, 4.29, 8.98 and 8.98 for true radii 3, 5, 8, 12, 16, 6, 4, 10 and 10: within one scale step (20 %) of the truth. The touching pair gets its two circles (σ = 6.35) plus one large circle around both (σ = 11.06, at (63, 205)): at a coarser scale the pair is one blob. The elongated blob gets a row of three small circles (σ = 2.09 to 2.52) along its length: the LoG fits its width (5), not its length.

C6. The raw LoG with threshold 0.12 gives 88 detections, all at the smallest scale σ = 1, and only 2 of the 9 discs are hit; the rest are noise. Blurring flattens the image, so raw second derivatives shrink as σ grows and the smallest scale always wins. The DoG is −0.0574 and -(k - 1) times the normalised LoG is −0.0549: within 5 %, as the lecture’s (k - 1)^2 error term predicts.

C7. 87 296 pixels for the 65 536 of the image: 1.33 times, the 4/3 of B4. The largest reconstruction error is 1.5 \cdot 10^{-8}: the telescoping sum is exact in real numbers, and what is left is the rounding of 32-bit floating-point numbers (as in demo question E9). The detail levels are mostly mid-grey (near 0), with the fine texture at the top level and coarser structure below.

Where this leads

The LoG is the trace of the Hessian: it adds the curvatures in two directions and cannot tell a blob from a line. The next unit uses both curvatures separately: blobs, ridges and saddles, and vesselness. Later, LoG/DoG at the spot size is the first step of point detection, and DoG extrema over scales are the interest points of keypoints.