dip::MomentAccumulator class

MomentAccumulator accumulates the zeroth order moment, the first order normalized moments, and the second order normalized central moments, in N dimensions.

Samples are added one by one, using the Push method. Other members are used to retrieve the moments.

It is possible to accumulate samples in different objects (e.g. when processing with multiple threads), and add the accumulators together using the + operator.

Constructors, destructors, assignment and conversion operators

MomentAccumulator(dip::uint N)
The constructor determines the dimensionality for the object.

Functions

void Reset()
Reset the accumulator, leaving it as if newly allocated.
void Push(dip::FloatArray const& pos, dip::dfloat weight)
Add a sample to the accumulator. pos must have N dimensions.
auto Sum() const -> dip::dfloat
Sum of weights (zeroth order moment)
auto FirstOrder() const -> dip::FloatArray
First order moments, normalized
auto SecondOrder() const -> dip::FloatArray
Second order central moment tensor, normalized
auto PlainSecondOrder() const -> dip::FloatArray
Plain second order central moments, normalized

Operators

auto operator+=(dip::MomentAccumulator const& b) -> dip::MomentAccumulator&
Combine two accumulators

Function documentation

dip::FloatArray SecondOrder() const

Second order central moment tensor, normalized

The moments are stored in the same order as symmetric tensors are stored in an image (see dip::Tensor::Shape). That is, fist are the main diagonal elements, then the elements above the diagonal, column-wise. This translates to:

  • 2D: xx, yy, xy
  • 3D: xx, yy, zz, xy, xz, yz
  • 4D: xx, yy, zz, tt, xy, xz, yz, xt, yt, zt
  • etc.

The second order moment tensor (inertia tensor) is defined as

\[ I = \Sigma_k m_k ((\vec{r_k} \cdot \vec{r_k}) E - \vec{r_k} \otimes \vec{r_k}) \]

where \(E\) is the identity matrix ( \(E = \Sigma_i \vec{e_i} \otimes \vec{e_i}\) ), \(m_k\) is the weight of point \(k\) , and \(\vec{r_k} = (x_k, y_k, \ldots)\) is its position relative to the center of mass. In 2D, this leads to

\begin{eqnarray*} I_{xx} & = & \mathbin{\phantom{-}}\Sigma_k m_k y_k^2 \\ I_{yy} & = & \mathbin{\phantom{-}}\Sigma_k m_k x_k^2 \\ I_{xy} & = & - \Sigma_k m_k x_k y_k \end{eqnarray*}

In 3D, it leads to

\begin{eqnarray*} I_{xx} & = & \mathbin{\phantom{-}}\Sigma_k m_k y_k^2 + \Sigma_k m_k z_k^2 \\ I_{yy} & = & \mathbin{\phantom{-}}\Sigma_k m_k x_k^2 + \Sigma_k m_k z_k^2 \\ I_{zz} & = & \mathbin{\phantom{-}}\Sigma_k m_k x_k^2 + \Sigma_k m_k y_k^2 \\ I_{xy} & = & - \Sigma_k m_k x_k y_k \\ I_{xz} & = & - \Sigma_k m_k x_k z_k \\ I_{yz} & = & - \Sigma_k m_k y_k z_k \end{eqnarray*}

In 1D the tensor is always 0, see dip::MomentAccumulator::PlainSecondOrder for a useful result in 1D.

Note that here we normalize each components by the sum of weights. This makes the tensor invariant to scaling of the weights (e.g. scaling the image intensity).

dip::FloatArray PlainSecondOrder() const

Plain second order central moments, normalized

Sometimes one just needs the normalized central moments directly, not in tensor form. Here we return them, in the same order as described above for dip::MomentAccumulator::SecondOrder. Each of the components are defined by

\[ M_{ij} = \frac{\Sigma_k m_k r_{ki} r_{kj}}{\Sigma_k m_k} \]

with \(m_k\) the weight of point \(k\) , and \(\vec{r_k} = (r_{k1}, r_{k2}, \ldots)\) its position relative to the center of mass.

The normalization makes the moments invariant to scaling of the weights (e.g. scaling of the image intensity). Divide each component by dip::MomentAccumulator::Sum (the zeroth order moment) to obtain size-invariant second order moments.