moments: Calculate the Moments of a Shape

View source: R/shape.R

momentsR Documentation

Calculate the Moments of a Shape

Description

moments calculates all of the moments up to the third order of a polygon or rasterized shape.

Usage

moments(x, binary = FALSE)

Arguments

x

Either a Nx2 matrix of the X-Y coordinates of a polygon (e.g., a contour produced by findContours), or a single-channel Image object.

binary

If set to TRUE (default: FALSE), all non-zero image pixels are treated as 1's. The parameter is used for images only.

Value

A data frame with 2 columns:

  • "moment": the name of the moment. See Note below.

  • "value": the value of the moment.

Note

The spatial moments m_{ji} are computed as:

m_{ji}= \sum _{x,y} \left ( \texttt{contour} (x,y) \cdot x^j \cdot y^i \right )

The central moments \mu_{ji} are computed as:

{\mu_{ji}}= \sum _{x,y} \left ( \texttt{contour} (x,y) \cdot (x - \bar{x} )^j \cdot (y - \bar{y} )^i \right )

where (\bar{x}, \bar{y}) is the mass center:

\bar{x} = \frac{m_{10}}{m_{00}} , \; \bar{y} = \frac{m_{01}}{m_{00}}

The normalized central moments \eta_{ji} are computed as:

\eta_{ji}= \frac{\mu_{ji}}{m_{00}^{(i+j)/2+1}} .

Author(s)

Simon Garnier, garnier@njit.edu

See Also

findContours, huInvariants

Examples

dots <- image(system.file("sample_img/dots.jpg", package = "Rvision"))
dots_gray <- changeColorSpace(dots, "GRAY")
dots_bin <- dots_gray < 200
contours <- findContours(dots_bin)
contour0 <- contours$contours[contours$contours[, 1] == 0, 2:3]
moments(contour0)


swarm-lab/Rvision documentation built on Feb. 7, 2024, 4:59 a.m.