Find the barycenter of luminance in an image
Show older comments
I need to find the weighted center of luminosity of barycenter in an image. (the weighted L center in Lab a b)
2 Comments
Walter Roberson
on 29 Oct 2015
Weighted by what?
David Corwin
on 29 Oct 2015
Edited: David Corwin
on 29 Oct 2015
Accepted Answer
More Answers (1)
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.
6 Comments
David Corwin
on 30 Oct 2015
Edited: Walter Roberson
on 30 Oct 2015
Image Analyst
on 30 Oct 2015
No, David, that's not right. Don't change my code like that - the number of pixels in allX is the number of pixels in each blob, not the number of columns in your entire image. They're completely different things!
David Corwin
on 30 Oct 2015
Image Analyst
on 30 Oct 2015
It doesn't. We thought (mistakenly) you had some region(s) identified, which is the most common situation. If your region is the entire image, then you can just do this:
grayImage = imread('cameraman.tif');
stats = regionprops(true(size(grayImage)), grayImage, 'WeightedCentroid');
centroid = stats.WeightedCentroid
David Corwin
on 30 Oct 2015
David Corwin
on 30 Oct 2015
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!