Search code examples
pythonimage-processingcanopyscikit-image

Scikit-image and central moments: what is the meaning?


Looking for examples of how to use image processing tools to "describe" images and shapes of any sort, I have stumbled upon the Scikit-image skimage.measure.moments_central(image, cr, cc, order=3) function.

They give an example of how to use this function:

from skimage import measure #Package name in Enthought Canopy
import numpy as np

image = np.zeros((20, 20), dtype=np.double) #Square image of zeros
image[13:17, 13:17] = 1 #Adding a square of 1s
m = moments(image)
cr = m[0, 1] / m[0, 0] #Row of the centroid (x coordinate)
cc = m[1, 0] / m[0, 0] #Column of the centroid (y coordinate)

In[1]: moments_central(image, cr, cc)
Out[1]:
array([[ 16.,   0.,  20.,   0.],
       [  0.,   0.,   0.,   0.],
       [ 20.,   0.,  25.,   0.],
       [  0.,   0.,   0.,   0.]])

1) What do each of the values represent? Since the (0,0) element is 16, I get this number corresponds to the area of the square of 1s, and therefore it is mu zero-zero. But how about the others?

2) Is this always a symmetric matrix?

3) What are the values associated with the famous second central moments?


Solution

  • The array returned by measure.moments_central correspond to the formula of https://en.wikipedia.org/wiki/Image_moment (section central moment). mu_00 corresponds indeed to the area of the object.

    Formula from Wikipedia (Image moment)

    The inertia matrix is not always symmetric, as shown by this example where the object is a rectangle instead of a square.

    >>> image = np.zeros((20, 20), dtype=np.double) #Square image of zeros
    >>> image[14:16, 13:17] = 1
    >>> m = measure.moments(image)
    >>> cr = m[0, 1] / m[0, 0]
    >>> cc = m[1, 0] / m[0, 0]
    >>> measure.moments_central(image, cr, cc)
    array([[  8. ,   0. ,   2. ,   0. ],
           [  0. ,   0. ,   0. ,   0. ],
           [ 10. ,   0. ,   2.5,   0. ],
           [  0. ,   0. ,   0. ,   0. ]])
    

    As for second-order moments, they are mu_02, mu_11, and mu_20 (coefficients on the diagonal i + j = 1). The same Wikipedia page https://en.wikipedia.org/wiki/Image_moment explains how to use second-order moments for computing the orientation of objects.