Calculating Centroids in a Specfic Region of an Image

7 views (last 30 days)
Hey !
I am here with a question that I am trying to find the total number of centroid in a specific region. Let say I have an image of which I have calculated the centroids,Now i want to calculate the numbers of centroid of some specific region,for example rectangular bottom portion. Hope will get answer ASAP

Accepted Answer

Image Analyst
Image Analyst on 9 Jun 2015
I'll assume you have a list of centroids in arrays xCentroids and yCentroids, and that x1, x2, y1, y2 define your rectangular region. So then
xInRange = xCentroids >= x1 & xCentroids <= x2;
yInRange = yCentroids >= y1 & yCentroids <= y2;
bothInRange = xInRange & yInRange;
% Count the number that are in the rectangular region:
count = sum(bothInRange);
If you have a non-rectangular region, like some arbitrarily shaped blob, then let me know.
  2 Comments
Ibraheem Salim
Ibraheem Salim on 15 Jun 2015
Edited: Image Analyst on 16 Jun 2015
Thank you Image Analyst. I understand your answer but still having some confusion,Hope u'll help me to get out of this. I have also uploaded an image to make my question more obvious. Rectangle in red color boundary is the desired region of whom i want to calculate the total number of centroids. The values(coordinates) of centroids x1,y1 or x2,y2 for next centroid is stored in a struct. Code is pasted below.
% Reading the Image
I = imread('s1.jpg');
%Binary Conversion
b = im2bw(I);
imshow(b);
%Imfill Function
Ibw = imfill(b,'holes');
Ilabel = bwlabel(Ibw);
%Region Proportions on Labelled image
g = regionprops(Ilabel,'centroid');
hold on;
% For Loop for pasting calculated Centroids on Image
for x = 1:numel(g)
plot(g(x).Centroid(1),g(x).Centroid(2),'yo');
end
% Global variable 'k' for usage in below loop
k=2;
% For loop to calculate the distance of each and very next centroid in the
% whole image
for i=1:1:length(g)-1
x(i) = g(i).Centroid(1);
y(i) = g(i).Centroid(2);
x(k)=g(k).Centroid(1);
y(k)=g(k).Centroid(2);
distance=sqrt((x(i)-x(k))^2+(y(i)-y(k))^2)
k=k+1;
%Function to differentiate the average values of distances
abc(distance)
end
Image Analyst
Image Analyst on 16 Jun 2015
I don't know what you want. This algorithm computes the distance from one centroid to the next indexed one, which is kind of meaningless because of the way the indexes are ordered, and it's not even what you asked originally.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!