# Find the barycenter of luminance in an image

31 views (last 30 days)
David Corwin on 29 Oct 2015
Commented: David Corwin on 30 Oct 2015
I need to find the weighted center of luminosity of barycenter in an image. (the weighted L center in Lab a b)
##### 2 CommentsShowHide 1 older comment
David Corwin on 29 Oct 2015
Edited: David Corwin on 29 Oct 2015
The image is an rgb image that is converted to an L*A*B* image. (I could just start with a gray scale image) For each pixel there is an L (value 1-100)multiplied by the distance to geometric center. The sum of all this should be the luminosity center or tonal center of the image or using tonal value as weight: the center of mass of the picture.

Image Analyst on 29 Oct 2015
Edited: Image Analyst on 29 Oct 2015
Since R2008a (or maybe b) regionprops has had a "WeightedCentroid" property that you can ask for. So pass in the L channel and ask for that if you want the centroid weighted by the L value.
If you want it weighted by the distance, that's the inertia or moment. See https://en.wikipedia.org/wiki/Image_moment I don't think there's anything built in for that but you can ask for the PixelList which is the location of every pixel in the blob and you can easily calculate it yourself.
Walter Roberson on 30 Oct 2015
No, L is not the label matrix here. L is the luminance channel, in accordance with IA's earlier "So pass in the L channel and ask for that if you want the centroid weighted by the L value."
LAB = rgb2lab(A);
L = LAB(:,:,1);
imageprops = regionprops(L, 'WeightedCentroid');
result = imageprops.WeightedCentroid;
If you want the "top half" then pass in the top half,
imageprops = regionprops(L(1:end/2,:), 'WeightedCentroid');
upperCent = imageprops.WeightedCentroid;
imageprops = regionprops(L(ceil(end/2):end,:), 'WeightedCentroid');
lowerCent = imageprops.WeightedCentroid;

Image Analyst on 30 Oct 2015
David:
I wrote a demo for you to compute the first 5x5 central spatial moments, as given by the Wikipedia page https://en.wikipedia.org/wiki/Image_moment. See attached m-file. The key code is this:
% Sum up the powers of the gray levels, weighted by the distance from the centroids.
% Ref: https://en.wikipedia.org/wiki/Image_moment
highestMomentNumber = 5;
mu = zeros(highestMomentNumber, highestMomentNumber); % Allocate up to 5 moment (probably way overkill).
for q = 1 : size(mu, 2) % in the column, x direction.
for p = 1 : size(mu, 1) % in the row, y direction
for location = 1 : length(allX)
% For every pixel in this blob....
% Sum up for moment "mu sub pq" - i.e. the pqth moment.
thisGrayLevel = grayImage(allY(location), allX(location));
mu(p, q) = mu(p, q) + ...
(allX(location) - xCenter).^q * ...
(allY(location) - yCenter).^p * ...
double(thisGrayLevel);
end
end
end
See file for a complete demo.
David Corwin on 30 Oct 2015
Thanks for all your help. I figured out the solution to the above by simply placing 1 or 255 in what ever part of the image I do not want to be included.

### Categories

Find more on Detection in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!