Calculating Centroids in a Specfic Region of an Image

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

 Respuesta aceptada

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 comentarios

Ibraheem Salim
Ibraheem Salim el 15 de Jun. de 2015
Editada: Image Analyst el 16 de Jun. de 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
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.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 9 de Jun. de 2015

Comentada:

el 16 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by