Centroid calculation image stack (for loop)

5 ビュー (過去 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 件)

カテゴリ

Help Center および File ExchangeComputer Vision Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by