What is a good algorithm, and framework, to calculate centres of gravity or mass (cog)? I'd like to take an photograph, subdivide it into a tesselation, either of squares, or (ideally), hexagons, and then find the centre of gravity (or, if you prefer, centre of mass) of each cell of the tesselation.
The output, for any image, would be a matrix of points. For the attached diagram, something like this (in polar coordinates - $(r,\theta)$ ):
(5,5Π/4) (0,0) (1,7Π/4) (1,5Π/4)
     (5,3Π/4) (0,0) (5,Π/2) 
(0.0) (0,0) (0.0) (0,0)  [I know the bottom left dot is actually at (2,3Π/2)]

I've attached an image showing what I mean.

I have, btw, tried asking this question in the Computer Graphics group, but I think it might be a bit mathematical for there, hence my asking it here.
My question, to put it simply; what is the best method to use to calculate the cog, in this tessellation... For squares, it'd be easy to calculate the weighted mean for each row, but that would be too rough an approximation. What's a good way to iterate either a general tessellation (ideal), or an hexagonal one?
 A: Let $I\colon\{0, N-1\}\times \{0, M-1\} \to \{0, L-1\}$ be a grayscale image with $N$ rows and $M$ columns.
Suppose that the tessellation is hexagonal, with centers $c_i = (x_i, y_i)\in [0, N-1]\times [0, M-1]$, for $i \in \{1, \ldots, N_c\}, N_c \in \mathbb{N}$.
Let $d((x,y), c_i)$ denote the euclidean distance from a point $(x, y)$ to the $i$-th center $(x_i, y_i)$.
For every $i \in \{1, \ldots, N_c\}$ define the cell $C_i$ relative to the center $c_i$ as:
$$
C_i = \left\{(x, y) \in [0, N-1]\times [0, M-1] \ \colon d((x,y), c_i) \leq d((x,y), c_j) \ \forall j \in {1, \ldots, N_c}\setminus \{i\} \right\},
$$
i.e., the set of points whose nearest center is $c_i$.
For estimating the center of gravity $c_G(C_i)$ of a cell $C_i$, I would use the formula:
$$
c_G(C) = \frac{\displaystyle\sum_{(x,y)\in C_i} I(x,y) \cdot \begin{bmatrix}x-x_i\\ y-y_i\end{bmatrix}}{\displaystyle\sum_{(x,y)\in C_i} I(x,y)},
$$
that is a weighted sum of the vectors that join the center of the cell with a generic point of the cell.
Once you get the expression of $c_G(C)$ as a vector relative to the center $c_i$, it is straightforward to convert it into polar coordinates.
Notes about the approach:


*

*cells overlapping with the border of the image give centers of gravity moved towards the inside of the image (because pixels outside of the image are not considered in the sum);

*if the cells contain only few pixels, the position of centers of gravity could be not accurate, depending on the discretization imposed by the use of pixels and the choice of the centers $c_i$.

