Using for loop for cell array
2 views (last 30 days)
Show older comments
Amir Azadeh Ranjbar
on 3 Dec 2021
Commented: Amir Azadeh Ranjbar
on 4 Dec 2021
clc;clear;close all
bw = rgb2gray(imread('image_3.png'));
figure;imshow(bw);title('orginal image')
[B,~,N]=bwboundaries(bw);
obj1=B{1}; x1=obj1(:,2); y1=obj1(:,1);
area1=polyarea(x1,y1)
obj2=B{2}; x2=obj2(:,2); y2=obj2(:,1);
area2=polyarea(x2,y2)
obj3=B{3}; x3=obj3(:,2); y3=obj3(:,1);
area3=polyarea(x3,y3)
AREA=[area1 area2 area3];
[Max,name]=max(AREA(:));
MAX=B{name};
hold on
plot(MAX(:,2),MAX(:,1),"Color","r","LineWidth",2);
title(['max sphere = ',num2str(Max)])

% I want insert for loop for obj 1:3
% Thanks
Accepted Answer
Voss
on 4 Dec 2021
clc;clear;close all
bw = rgb2gray(imread('image_3.png'));
figure;imshow(bw);title('orginal image')
[B,~,N]=bwboundaries(bw);
nB = numel(B);
AREA = zeros(1,nB);
for i = 1:nB
obj = B{i};
x = obj(:,2);
y = obj(:,1);
area = polyarea(x,y);
AREA(i) = area;
end
[Max,name]=max(AREA(:));
MAX=B{name};
hold on
plot(MAX(:,2),MAX(:,1),"Color","r","LineWidth",2);
title(['max sphere = ',num2str(Max)])
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!