Q:

Understanding the computation of IntensityCentroid?

If I have an image img, and I ask ComponentMeasurements to return "IntensityValues" for the image:
m=MorphologicalComponents[Binarize[img]];
ivalues=ComponentMeasurements[{m,img},"IntensityData"];

How can I replicate the output of ComponentMeasurements[{m,img},"IntensityCentroid"]?
The challenge I'm having is understanding how to interpret a calculated intensity centroid from "IntensityValues" in the context of larger image with the overlayed morphological component?  Is it flipped or rotated?  It would be fantastic to see an example going from the raw "IntensityValues" data to the output of ComponentMeasurements[{m,img},"IntensityCentroid"] for the same morphological component.

A:

Something like this:
img     = ExampleData[{"TestImage", "Lena"}];
m       = MorphologicalComponents[Binarize[img]];
ivalues = ComponentMeasurements[{m, img}, "IntensityData"];

cents = ({#[[2]] - .5, ImageDimensions[img][[2]] - #[[1]] + .5} & /@ 
        (Total[Position[m, #] (# /. ivalues)]/Total[(# /. ivalues)] & /@  Range[Max[m]]));

Testing:
Chop[cents - ComponentMeasurements[{m, img}, "IntensityCentroid"][[All, 2]]]

{{0,0}, {0,0}, ... {0,0}}

