How to output centroid coordinate from switch case wihtin loop case

2 次查看(过去 30 天)
Hello again. Sorry for my question again.
I have a code to detected the shape using for loop and switch case.
I can plot the centroid on image but I cannot extract the centroid coordinate from it.
I want the centroid coordinate in the form of matrix of excel file or anything that I can use the information in the next step.
This is the code I have. For this code I cannot obtained the all centroid detected within this loop.
Furthermore, this is the code use for 1 image. In short future, I have to read the all images in folders and do this to every image. So, I need to store the centroid coordinate of every image and use it for calculation. I will be so thankful if you can guide me for this.
Thank you in advance.
for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
W(i) = W(i) + 4 * uint8((STATS(i).Extent - 1) <= 0.7854); %Extent = pi/4 case ellipse/circle
centroid = STATS(i).Centroid;
switch W(i)
case 5
plot(centroid(1),centroid(2),'wO');
x_centroid = centroid(1:2:end);
y_centroid = centroid(2:2:end);
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
%case 4
%plot(centroid(1),centroid(2),'w+');
end
end

回答(1 个)

Image Analyst
Image Analyst 2022-11-14
Put all that in a loop over all files. For code snippets, see the FAQ:
If you still can't figure it out, write back.
If you want to save the centroid(s) for the blob(s) in this image, save STATS into a cell array
filePattern = '*.png';
fileList = dir(filePattern);
numImages = numel(fileList);
for frameIndex = 1 : numImages
thisFileName = fullfile(fileList(frameIndex).folder, fileList(frameIndex).name);
fprintf('Processing %s\n', thisFileName)
STATS = regionprops(binaryImage, 'Centroid', 'Extent', 'BoundingBox');
for i = 1 : numel(STATS)
% Code
end
ca{frameIndex} = STATS; % Save centroids for all the blobs into a cell for this particular image.
end
  1 个评论
Chanoknunt Sangsobhon
I have tried the code but the ca obtained only 1 centroid value which I don't know what value it is whether it is which centroid or it is x or y coordinate. I mean, I tried used it with one image first but in that image suppose to have 2 centroids but it obtained only 1 centroid value.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by