# Calculating connected component's central moments

I am learning about image's moments. As an exercise I got this: For x̄ value I get 5 from this formula:

$$= \frac{\sum_{x=1}^{X} \sum_{y=1}^{Y} xf(x,y)}{\sum_{x=1}^{X} \sum_{y=1}^{Y} f(x,y)}$$

As this is square image, it should be the same value of 5 for ȳ as well.

Finally, this formula should be used to get all the requested central moments, but it seems that computation would be quite complicated, and time consuming (which does not correspond with how many points this exercise is worth). Am I missing some simplification?

$$\mu_{pq} = \sum_{x}\sum_{y}(x-\bar{x})^p(y-\bar{y})^qf(x,y)$$

• Please indicate the values of the black and which squares in your question. I assume that black squares are 1 and white squares are 0? Please also label the coordinates, are you going from 0..4, 1..5, etc. May 29, 2022 at 15:26
• I have converted your equations from images to MathJax (edit may be pending), please check that I have done so correctly and consider using MathJax in the future. May 29, 2022 at 15:32
• Thanks for the reply! It's the other way around, white is 1, black 0. Btw, this needs to be solved by hand. May 29, 2022 at 15:37
• It says “consider the connected component depicted by the solid black pixels below”. This means you skip the white pixels (they’re not considered at all in the computation). The black pixels will have a value of 1. Please tell us why this computation is complicated, and why you think the amount of points has anything to do with this? May 30, 2022 at 17:19

μ01 is one axis only. Do it again. Two (2) in the μ indexing is for second moment. First moment is equivalent to the average. Variance is a mix of first n 2nd moments. You r welcome

It is easy to just calculate using the formulation you gave:

import numpy as np

def central_moment(fxy, p, q):

n_rows, n_cols = fxy.shape

index_x, index_y = np.meshgrid(range(n_rows), range(n_cols), indexing='xy')

mean_x = np.mean(index_x * fxy) / np.mean(fxy)
mean_y = np.mean(index_y * fxy) / np.mean(fxy)

return np.sum(np.power(index_x - mean_x, p) * np.power(index_y - mean_y, q) * fxy

fxy = np.zeros(shape=(5, 5))

fxy[[1, 2, 2, 2, 3], [1, 1, 2, 3, 3]] = 1

mu_11 = central_moment(fxy, 1, 1)
mu_20 = central_moment(fxy, 2, 0)
mu_02 = central_moment(fxy, 0, 2)



then

print(f'mu_11 = {mu_11}')
print(f'mu_20 = {mu_20}')
print(f'mu_02 = {mu_02}')


output:

mu_11 = 2.0
mu_20 = 4.0
mu_02 = 2.0