Skip to contents

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

Simon Garnier, garnier@njit.edu

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)
#>    moment         value
#> 1     m00  8.958500e+03
#> 2     m10  5.043943e+06
#> 3     m01  4.582793e+05
#> 4     m20  2.846367e+09
#> 5     m11  2.575802e+08
#> 6     m02  2.981324e+07
#> 7     m30  1.609871e+12
#> 8     m21  1.450999e+11
#> 9     m12  1.674067e+10
#> 10    m03  2.181108e+09
#> 11   mu20  6.454539e+06
#> 12   mu11 -4.467378e+05
#> 13   mu02  6.369592e+06
#> 14   mu30  3.439845e+05
#> 15   mu21 -5.259778e+06
#> 16   mu12  5.018900e+05
#> 17   mu03  4.303903e+06
#> 18   nu20  8.042566e-02
#> 19   nu11 -5.566499e-03
#> 20   nu02  7.936719e-02
#> 21   nu30  4.528461e-05
#> 22   nu21 -6.924352e-04
#> 23   nu12  6.607243e-05
#> 24   nu03  5.665970e-04