Clear Filters
Clear Filters

Using for loop for cell array

1 view (last 30 days)
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
  1 Comment
Amir Azadeh Ranjbar
Amir Azadeh Ranjbar on 3 Dec 2021
I want insert for loop for obj 1:3
for more objects if exist
Thanks

Sign in to comment.

Accepted Answer

Voss
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)

Tags

Community Treasure Hunt

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

Start Hunting!