Centroid calculation image stack (for loop)

조회 수: 12 (최근 30일)
Lightisthenight
Lightisthenight 2019년 1월 18일
댓글: Lightisthenight 2019년 1월 18일
I want to calculate the total centroid of an image stack. For a single image i could use the regionprops() to calculate it as shown here. For a single image my variable c1 is where the coordinates of all the centroids in the image are saved To calculate the total centroid I just summed all coordinates in the image up and divide it by amount of centroids in the picture. The first row should display the x and y coordinates of my total centroid for the first image, the second row for the second image, etc.
% single image
Ilabel = bwlabel(Ibw);
stat1 = regionprops(Ilabel,'Area','centroid');
c1 = cat(1, stat1.Centroid); % c1 is 16x2
[m1, n1] = size(c1); % m1= 16 amount of centroids in a single image
totalcentroid1 = sum(c1)/m1;
For an image stack my idea was to write a for loop to do this process. My first problem is when i save all centroids for all imagestacks in a variable , that i don't know the exact amount of centroids in a single image to calculate the total centroid of each image. How can i do this ? In the following i tried to implement the calculation of a total centroid for an imagestack. There is error message in this code. How can i fix this issue
%% imagestack
for i=0:options.ImageAmount-1
Ilabel(:,:,i+1) = bwlabel(Ibw(:,:,i+1));
stat1(:,:,i+1) =regionprops(Ilabel(:,:,i+1),'centroid');
%c1(:,:,i+1) = cat(1,stat1.Centroid(:,:,i+1); % unsure aboute this part
%[m1, n1] = size(c1(:,:,i+1));
% totalcentroid1(:,:,i+1) = sum(c1(:,:,i+1)/m1(:,:,i+1);
end
%%
Subscripted assignment dimension mismatch.
Error in delete (line 126)
stat1(:,:,i+1) =regionprops(Ilabel(:,:,i+1),'centroid');

채택된 답변

Image Analyst
Image Analyst 2019년 1월 18일
Your formula for a single image is not even correct. The centroid of a binary image with 16 blobs is not the average of the 16 centroids of course.
Why don't you skip regionprops and just use your binary image itself? Since
mean x = (sum of x*graylevel at x) / (sum of graylevels at x)
or in other words, the mean x is simply the mean of all the x values. So
for sliceNumber = 1 : size(Ibw, 3)
[rows, columns] = find(Ibw(:, :, sliceNumber));
xCentroids(sliceNumber) = mean(columns);
yCentroids(sliceNumber) = mean(rows);
end
meanXCentroid = mean(xCentroids) % Mean column, x
meanYCentroid = mean(yCentroids) % Mean row, y
  댓글 수: 5
Image Analyst
Image Analyst 2019년 1월 18일
No, leave them outside the loop just as I had it. The mean centroids for each slice are stored in the xCentroids and yCentroids arrays.
Lightisthenight
Lightisthenight 2019년 1월 18일
Thank you very much sir!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by