Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Shape Index Under Gaussian Derivatives

assisted-by - Unknown Directive
claude-code:claude-opus-5

shape_index built its Hessian with finite differences up to scikit-image 0.26. From 0.27 it uses the moment-corrected Gaussian derivative kernels of hessian_matrix. on_hessian.md and hessian_review.md measure why the Hessian changed. This notebook asks what the change does to the shape index, and whether the old route has any claim left.

The answer is that the shape index is not the Hessian, and the ordering can reverse: below a crossover near sigma 0.7 the finite-difference shape index is closer to a closed form than the Gaussian-derivative one, while its Hessian is further away.

Subscripts ii and jj name the image axes in skimage’s rc order: axis ii is the first array axis, down the rows, and axis jj the second, across the columns.

fatal: not a git repository (or any of the parent directories): .git

1. The two routes

Both smooth at scale sigma and take second derivatives. They differ in what does the differentiating.

The Gaussian-derivative route convolves once per element with a kernel whose discrete moments have been restored. Sampling the derivative and using the taps as they fall leaves a kernel that responds to a constant image and under-reports curvature; imposing sum(k) == 0 and sum(k * x**2) / 2 == 1 repairs both.

The shape index itself is the same arithmetic either way: Koenderink and van Doorn’s measure of local curvature, built from the two Hessian eigenvalues.

The finite-difference transcription has to be the library’s, or nothing below means anything.

Loading...

The Gaussian-derivative kernels are pinned by their two moment conditions, so check those instead.

Loading...

2. The shape index only sees a ratio

The Hessian comparisons in hessian_review.md are about magnitudes. The shape index is not: it is a function of the quotient of the two eigenvalues, so multiplying the whole Hessian by any positive constant leaves it alone.

Loading...

That is the design, not an accident of the formula. Koenderink and van Doorn build the shape index from a polar coordinate system in the (κ1,κ2)(\kappa_1, \kappa_2) plane, in which “the direction encodes the shape, whereas the distance from the origin encodes the size” Koenderink & Doorn, 1992. Their definition, with κ1≥κ2\kappa_1 \geq \kappa_2, is the one the library implements:

s=2πarctan⁡κ2+κ1κ2−κ1s = \frac{2}{\pi}\arctan\frac{\kappa_2 + \kappa_1}{\kappa_2 - \kappa_1}

and they note that ss is directly proportional to the angle with the κ1+κ2=0\kappa_1 + \kappa_2 = 0 axis. Proportional, not merely monotonic, so an error in that angle is an error in ss scaled by a constant.

Loading...

So the angle is not a proxy for the shape index. It is the shape index, to a factor of 2/π2/\pi. §6 uses that to take the error apart.

The companion measure in the same paper is the curvedness, c=(κ12+κ22)/2c = \sqrt{(\kappa_1^2 + \kappa_2^2)/2}, which carries the distance from the origin. Between them ss and cc hold everything the Hessian has. The shape index discards cc entirely, and cc is what the moment correction of hessian_matrix repairs.

3. How much the pictures move

The docstring sorts the shape index into nine named classes, from spherical cup at -1 to spherical cap at +1. The fraction of pixels that change class is the change a caller sees.

Loading...

At sigma of 0.5 most of the picture is reclassified; by sigma of 4 almost none of it is. Neither route produces NaN on this photograph, so the undefined flat regions the docstring warns about do not arise here.

<Figure size 924x330 with 4 Axes>

The difference is structured rather than scattered. Splitting the frame by how far the eigenvalue pair is from isotropic shows where it concentrates.

Loading...

The disagreement is smallest in the middle of that range and largest at the top, where the two eigenvalues approach equal and opposite, which is where the saddles are. The rank correlation over the individual pixels is only +0.21, so this is a tendency rather than a rule, and the figure is the better description of the pattern.

4. Which one is right

A Gaussian blob smoothed by a Gaussian is a Gaussian, so its Hessian and therefore its shape index are known in closed form. That makes a standard neither route was built against.

Loading...

The ordering reverses between sigma 0.5 and 0.8 on this grid; §10 sweeps finely and puts the crossover at 0.70 for this blob. Below it the finite-difference shape index is the closer of the two, by a factor of 14 at sigma 0.5 and 28 at sigma 0.3. Above it the Gaussian-derivative one wins, and by four orders of magnitude by sigma 2.

<Figure size 616x352 with 1 Axes>

5. A better Hessian, a worse shape index

The reversal is not the Gaussian-derivative Hessian being worse at small sigma. On the same images it is better at every scale measured.

Loading...

At sigma 0.3 and 0.5 the Gaussian-derivative Hessian is the closer one and its shape index is the further one. The accuracy of a Hessian is therefore not a proxy for the accuracy of the shape index built from it, and §2 is why: the shape index discards exactly the part of the Hessian that the moment correction repairs.

6. What turns the eigenvalue pair

§2 showed the angle of the eigenvalue pair is the shape index, to a factor of 2/π2/\pi. Measuring the angle is therefore not a different experiment from §4; it is the same error, in units that can be taken apart element by element.

Loading...

The finite-difference angular error is flat at about 5×10−35 \times 10^{-3} radians at every scale, because its stencil error is set by the pixel grid and not by sigma. The Gaussian-derivative one starts an order of magnitude worse and falls away to nothing.

Which element carries it is decidable: substitute the exact value for one element and measure the angle again.

Loading...

Two different stories in one table.

For the Gaussian-derivative route at sigma 0.3, handing it the exact mixed element drops the angular error by a factor of about 50, and handing it the exact diagonals does not help at all (0.119 against 0.108 as computed). Its small-sigma shape-index error is the mixed element. That follows from how the kernels are built: the diagonal elements get the second-derivative kernel, whose moment correction forces exactness on a quadratic, while the mixed element is two first-derivative kernels whose correction fixes only the first moment.

For the finite-difference route, handing it the exact mixed element makes the angle nine times worse, and the exact diagonals twenty-seven times worse. Its small angular error is not accuracy in any element. It is three element errors that cancel, and repairing any one of them destroys the cancellation.

7. Reasons to prefer the finite-difference result

Three were measurable. The first is §4’s accuracy below the crossover. The second is noise.

Loading...

The finite-difference shape index is the steadier of the two under noise, by about 1.8 times at sigma 1 and 1.2 times at sigma 2. Its stencil spans two pixels rather than one and rolls off towards Nyquist, so it passes less of the noise that a second derivative amplifies.

The third is cost.

Loading...

The corrected kernels are truncated at eight standard deviations, so they grow with sigma where the central differences do not.

The fourth reason is not measurable here and is the strongest: a published result computed with the old route is reproduced only by the old route. skimage2.migration.shape_index_finite_difference is that route, kept for the purpose.

8. What other libraries do

The shape index is implemented elsewhere, but almost entirely on explicit surfaces -- triangle meshes or fields of normals -- rather than on a raster. FreeSurfer’s mris_curvature_stats, pycurv, VCGLib and the eos library all carry it; MIRTK carries the curvedness without it. ITK, SimpleITK, VIGRA, DIPlib, OpenCV and Pillow carry neither.

That matters for §4 and §7: a mesh implementation fits a quadratic to a vertex neighbourhood, so it never has to choose between finite differences and Gaussian derivatives. The choice this notebook is about belongs to the raster setting, and there is no other library in that setting to check against.

The implementations that do exist disagree with each other on two things. The first is the sign.

Loading...

pycurv reverses the denominator, so its shape index is the negative of Koenderink’s: the dome that scikit-image scores at +0.795 it scores at -0.795. FreeSurfer omits the 2/π2/\pi, so its range is [−π/2,+π/2][-\pi/2, +\pi/2] rather than [−1,+1][-1, +1]. scikit-image follows the paper exactly, in the formula, in the κ1≥κ2\kappa_1 \geq \kappa_2 ordering, and in the nine category bounds of the docstring table.

The second disagreement is the degenerate case, and it is the subject of §9. FreeSurfer returns 0 when the two curvatures are equal:

return (af_k1 == af_k2 ? 0 : atan((af_k1+af_k2)/(af_k2 - af_k1)));

pycurv returns 0 only when both are zero, and leaves the equal-and-non-zero case to the arithmetic:

if kappa_1 == 0 and kappa_2 == 0:
    return 0
else:
    return 2 / pi * atan((kappa_1 + kappa_2) / (kappa_1 - kappa_2))

Neither matches the paper. §9 is what the paper asks for.

FreeSurfer puts the shape index behind an explicit --shapeIndex command-line flag, with the comment “The ‘Shape Index’ can be problematic due to the atan calculations”, so the fragility §9 measures has been met before.

9. What not to conclude

9.1 The accuracy below the crossover is not a property to rely on

§6 is the reason. The finite-difference route is close on the angle because its three element errors cancel, not because any element is right. Whether the cancellation holds for an image unlike this blob is not measured here, and it cannot be with the method used: §4 needs a closed form, which a photograph does not have. A property that survives only while three errors stay in step is a coincidence to be grateful for, not a design to depend on.

10. Should the library switch route below the crossover

§4 found the finite-difference shape index closer to the closed form at small sigma, so the question is whether shape_index should dispatch on sigma. Three measurements say no.

The first is that the crossover is a property of the image, not a constant.

Loading...

It moves between 0.55 and 0.70 over blob widths from 2 to 16 pixels, on the one family of shapes where a closed form exists at all. A photograph contains many widths at once, so no single sigma divides the two regimes for a whole image.

The second is that a switch is a discontinuity in sigma, and the shape index is often swept over sigma.

Loading...

Across a sigma step of 0.02 a switch moves the shape index by 0.134, where staying on one route moves it by 0.011. The artefact is twelve times the signal, and more than half the width of a category bin from the docstring table.

The third is §6. The advantage being bought is a cancellation of three element errors, and §6 locates the Gaussian-derivative shortfall in the mixed element. A better mixed-element kernel is worth having on its own account, but it does not remove the reason for a switch: imposing the first-derivative kernel’s third moment, which is 270% out at sigma 0.3 and exact above 0.8, improves the mixed element by about 50 times and the typical shape-index pixel by 3 to 6 times, and still moves the crossover only from 0.70 to 0.65. The gap below the crossover is not the mixed element alone.

Callers who want the old numbers at every sigma, continuously, already have them in skimage2.migration.shape_index_finite_difference.

11. The umbilic, where the formula cannot say what the paper says

Koenderink and van Doorn separate two degenerate cases, and only one of them is undefined Koenderink & Doorn, 1992. At a planar point, κ1=κ2=0\kappa_1 = \kappa_2 = 0, the direction in the (κ1,κ2)(\kappa_1, \kappa_2) plane is the direction of the origin, so the shape index “is really indeterminate”, as they put it, and NaN is the right answer. At an umbilic, κ1=κ2≠0\kappa_1 = \kappa_2 \neq 0, it is not indeterminate: it is s=+1s = +1 for the outside of a sphere and s=−1s = -1 for the inside, and those are “true endpoints of the scale”.

The formula cannot deliver that. It reads the sign of κ2−κ1\kappa_2 - \kappa_1, which at an umbilic is zero, so the answer comes from a rounding residue.

Loading...

At the centre of a radially symmetric blob the two curvatures are equal, so lambda2 - lambda1 is either exactly zero or a residue of order 10-17, and its sign sends the shape index to -1 or +1 — spherical cup or spherical cap, the two ends of the scale. This is a determinate value that the implementation cannot reach, not a quantity the two routes disagree about on their merits.

The two computed routes do at least agree with each other, and flip together with the sign of the curvature. The exact Hessian, whose residue is a positive zero rather than a negative one, lands on the other end in both cases.

Loading...

It needs near-exact symmetry, so it is a synthetic-image problem:

Loading...

12. A more careful mixed element

§6 put the Gaussian-derivative shortfall in the mixed element. That element is two passes of the first-derivative kernel, so the question is which of its moments is wrong.

correlate1d at the origin returns sum(k * x**n) for f=xnf = x^n. Smoothing x3x^3 at scale σ\sigma gives x3+3σ2xx^3 + 3\sigma^2 x, whose derivative at the origin is 3σ23\sigma^2. So the kernel owes three conditions, and the shipped one imposes the first two.

Loading...

The third moment is 270% out at sigma 0.3 and exact from about 0.8, which is where the crossover of §10 sits. Below sigma of about 0.5 the kernel has collapsed towards the plain central difference [−12,0,+12][-\tfrac{1}{2}, 0, +\tfrac{1}{2}], whose third moment is 1 whatever sigma is, while the truth asks for 3σ23\sigma^2.

Imposing it needs one more degree of freedom. Two odd basis functions carry two conditions.

Loading...

The change is 0.12 of a tap at sigma 0.3, 3e-4 at 0.8 and nothing at 1.0, so it cannot disturb the regime that already works. The 2 by 2 system is badly conditioned at sigma 0.3, which is the first thing to watch.

Loading...

The mixed element improves by about fifty times at the small scales where it was wrong, and by less as the shipped kernel comes right. The typical shape-index pixel improves by three to six times, which is what §2 says it must: the angle and the shape index move together, and they do.

It still does not reach the finite-difference route, and the crossover barely moves.

Loading...

So the correction is worth having on its own account — a fifty times better mixed element at small sigma, negligible above 0.8, one more condition on a kernel that already carries two — and it is not an answer to §10. The gap below the crossover is not the mixed element alone.

12.1 The blob flatters finite differences

§4 measures against a Gaussian blob because that is the only shape with a closed form. A blob is also the input least able to show what finite differences get wrong, because a central difference errs on high spatial frequencies and a smoothed blob has almost none.

Loading...

At sigma 2 the finite-difference error is twenty times larger on the photograph than on the blob, and it moves 16% of pixels across a category boundary against 0.03%. It decays as sigma removes the frequencies the stencil gets wrong, and by sigma 8 it is under 1% on both.

Part of that gap is that a photograph has more pixels sitting near a boundary, but only a small part.

Loading...

A factor of 1.8 in boundary proximity against a factor of 20 in the error, so most of the difference is the error itself and not the binning. Read §4’s numbers as the best case for finite differences, not the typical one.

13. Summary

questionanswer
how much does the shape index move71% of pixels change class at sigma 0.5, 46% at 1, 16% at 2, 5% at 4
which is closer to a closed formfinite differences below the crossover, Gaussian derivatives above, by up to 4×1044 \times 10^{4}
where the crossover issigma 0.70 for a blob of width 6; 0.55 to 0.70 over widths 2 to 16
is the better Hessian the better shape indexno: at sigma 0.3 and 0.5 the Gaussian-derivative Hessian is closer and its shape index is further
whythe shape index sees only the eigenvalue pair’s angle; the Gaussian-derivative angle error is all in the mixed element, and the finite-difference one is three errors cancelling
reasons to keep the old routeaccuracy below the crossover; 1.2 to 1.8 times steadier under noise; cheaper, and more so as sigma grows; bit-reproducibility of published numbers
should the library switch route at small sigmano: the crossover moves with the image, and a switch jumps s by 0.134 against a real change of 0.011
does a better mixed element close the gapno: the third moment improves the mixed element 50x and the typical pixel 3-6x, and moves the crossover only 0.70 to 0.65
is the FD error important at large sigmaon a photograph yes up to sigma about 4 (16% of pixels miscategorised at 2, 5% at 4, 0.7% at 8); on a smooth blob never (under 0.1%)
do other libraries agreepycurv has the opposite sign, FreeSurfer omits the 2/π2/\pi; none matches the paper at the umbilic
is the accuracy reason soundno: §6 shows it rests on a cancellation, not on any element being right
what happens at an umbilicthe paper says s=±1s = \pm 1, determinate; the formula reads the sign of a zero and cannot deliver it

Limits. One photograph (camera, decimated to 256²) and one synthetic Gaussian blob of width 6, at sigma from 0.3 to 4, in two dimensions only. The closed form is a single isotropic blob; a scene of several blobs at different widths is hessian_review.md’s corpus, not this one. The crossover near sigma 0.70 is measured on that one blob family and its position moves with the image; §10 measures that movement only over blob width, not over image content. The noise figures are 12 draws at two amplitudes on pixels carrying the top 70% of curvature, so they say nothing about low-curvature regions, where both routes return essentially noise. The timings are single runs on one machine. The reading of Koenderink and van Doorn in §2 and §8.2 is of their definitions and their statements about planar and umbilical points; this notebook does not reproduce their psychophysics. float32 input is not tested, nor is mode other than the default constant, and the Gaussian-derivative route is the transcription above rather than an import, because the installed scikit-image here predates the change.

scikit-image  0.27.0rc2.dev0
scipy         1.18.1
installed shape_index route: finite differences
References
  1. Koenderink, J. J., & van Doorn, A. J. (1992). Surface shape and curvature scales. Image and Vision Computing, 10(8), 557–564. 10.1016/0262-8856(92)90076-f