Clear Filters
Clear Filters

How to output centroid coordinate from switch case wihtin loop case

1 view (last 30 days)
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

Answers (1)

Image Analyst
Image Analyst on 14 Nov 2022
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 Comment
Chanoknunt Sangsobhon
Chanoknunt Sangsobhon on 15 Nov 2022
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.

Sign in to comment.

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!