Using for loop for cell array

2 vues (au cours des 30 derniers jours)
Amir Azadeh Ranjbar
Amir Azadeh Ranjbar le 3 Déc 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
  1 commentaire
Amir Azadeh Ranjbar
Amir Azadeh Ranjbar le 3 Déc 2021
I want insert for loop for obj 1:3
for more objects if exist
Thanks

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 4 Déc 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)])
  1 commentaire
Amir Azadeh Ranjbar
Amir Azadeh Ranjbar le 4 Déc 2021
Its Working well
Thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by