I think there are three possibilities:
this is a bug
Mathematica is using a definition of "Euler number" that does not mean "Euler characteristic"
the image is not being interpreted in the expected manner
tl;dr I think this is a bug, because it calculates a quantity which is not the Euler number under any definition I've seen, and is not even an acceptable algebraic-topology invariant.
Here's my exploration of what could be going on, including some tests to support this conclusion.
The second one seems plausible at first glance. Note that Wolfram Mathworld defines the Euler number differently than the Euler characteristic: for a finite complex $K$, it defines it as
$$\chi'(K) = \sum_{n \in \mathbb{N}} (-1)^n\ \text{rank}(C_n(K)) $$
Contrast this with the definition of the Euler characteristic, which is
$$ \chi(K) = \sum_n (-1)^n\beta_n(K) = \sum_n (-1)^n\text{rank}(H_n(K)) $$
However, even though these are different definitions, Wikipedia implicitly says these agree by defining the Euler characteristic as the Euler number for finite CW-complexes. (Wikipedia uses the alternating sum of number of cells of a given dimension, which translates to the rank of the chain space. I'll come back with a better source (or just a proof!) if I have time, but hopefully the rest renders this immaterial anyway...)
Further, consider the following example.
MorphologicalEulerNumber[
ReplacePart[ConstantArray[1, {3, 3, 1}], {2, 2, 1} -> 0] // Image3D //
Echo]
This produces a 3x3x1 "solid torus" of cubes, as you can see from the Echo
. But MorphologicalEulerNumber
gives 1
, despite the fact that the Euler characteristic is 0
. And yet, even if you manually add up all the cells involved, with alternating signs,
-8 + (8 + 4 + 2*8 + 3*4) - (3*4 + 2*3*4 + 2*4*2 + 4*2 + 4) + 4*4*2
you get 0
.
This makes me lean towards "bug", since both the Euler characteristic and number ought to be 0
for this shape.
However, I wondered if maybe Mathematica was interpreting voxels at the boundary of the image itself differently. However, padding with zeros,
MorphologicalEulerNumber[
ArrayPad[ReplacePart[ConstantArray[1, {3, 3, 1}], {2, 2, 1} -> 0],
1] // Image3D // Echo]
still returns 1.
Interestingly, though, supplying a Padding -> 1
option to the original 3x3x1 torus changes things:
MorphologicalEulerNumber[
ReplacePart[ConstantArray[1, {3, 3, 1}], {2, 2, 1} -> 0] // Image3D //
Echo, Padding -> 1]
This now gives 0! But not for the manually padded version, unfortunately. The documentation on what exactly a "padding value" even is in this context is a bit scarce. If it means, as I'm guessing, "the value that should be padded around the boundary of the image", then that suggests that by padding with 1's all round, Mathematica is essentially creating a 1x1x1 3-dimensional hole (void) in the center, and is now subtracting that from the count of the single connected component.
Indeed, if we make one of those voids ourselves, we get 0
:
MorphologicalEulerNumber[
ReplacePart[ConstantArray[1, {3, 3, 3}], {2, 2, 2} -> 0] // Image3D //
Echo]
My guess, then, is that Mathematica is completely ignoring the first Betti number, and is instead using the following formula in 3 dimensions:
$$ \text{MorphologicalEulerNumber}(K) = \beta_0(K) - \beta_2(K) $$
This explains the value of 1
: the solid torus of cubes has a single connected component and no 3D void.
It also explains why the curve produced in your case is $\beta_0 - \beta_2$, and is entirely missing the $\beta_1$ component; see the figure on page 3 of this paper (though they might have the horizontal axis reversed compared to your convention).
This is also backed up by the description given, as long as we interpret "holes" as 3-dimensional holes (counted by $\beta_2$):
MorphologicalEulerNumber[image]
by default gives the total number of connected white regions in image, minus the number of black holes that occur inside those regions.
In my opinion this is a serious issue, because this is not homotopy invariant. If you're doing any (computational) algebraic topology whatsoever, you'll generally want your invariants to not just be topological invariants, but homotopy ones as well. However, if you imagine retracting this solid-cube-torus down into its 2-dimensional version, the MorphologicalEulerNumber
changes:
MorphologicalEulerNumber[
ReplacePart[ConstantArray[1, {3, 3}], {2, 2} -> 0] // Image // Echo]
This gives 0
. But since these two shapes are homotopy equivalent, their invariants should be the same.
My guess is that this might have been an ad-hoc generalization from 2D to 3D which was made without the knowledge that the Euler number is already well-defined in 3 (and higher) dimensions—or at least without the knowledge that its definition no longer corresponds to "number of connected components minus top-dimensional holes", since this is "just a coincidence" in 2D.
So, I would call this a bug, as this definition does not produce any sort of Euler number or characteristic, which in any definition I've seen is required to be homotopy invariant. (However, it is of course possible that some community uses this niche definition, though I would be surprised, and it would be out of step with a very widespread convention.) At the very least, since the quantity calculated is not an Euler number, the function should be called something different—and then, there should also be a function which calculates the Euler number! :)