Effacer les filtres
Effacer les filtres

Cannot get final output for for loop

1 vue (au cours des 30 derniers jours)
Tinna Armasamy
Tinna Armasamy le 27 Avr 2017
Commenté : Tinna Armasamy le 27 Avr 2017
soilgray=getimage(handles.axes1);
soilgray=rgb2gray(soilgray);
level=graythresh(soilgray);
im=im2bw(soilgray,level);
axes(handles.axes2);
imshow(im);
cc = bwconncomp(im,8);
n= cc.NumObjects;
set(handles.text11,'String',n);
Area = zeros(n,1);
Diameter = zeros(n,1);
MajorAxis = zeros(n,1);
Clay=zeros(1,n);
Silt=zeros(1,n);
Sand=zeros(1,n);
k = regionprops(cc,'Area','EquivDiameter','MajorAxisLength');
for i=1:n
Area(i) = k(i).Area;
Diameter(i) = sqrt(4*[k(i).Area]/pi);
MajorAxis(i) = k(i).MajorAxisLength;
Diameter(i) = (Diameter(i)*25.4)/300;
Diameter(i) = round(Diameter(i),3,'significant');
if Diameter(i) < 0.002
Clay(i) = Diameter(i);
else if Diameter(i) >=0.002 && Diameter(i) < 0.05
Silt(i) = Diameter(i);
else if Diameter(i) >= 0.05 && Diameter(i) < 2
Sand(i) = Diameter(i);
end
end
end
end
NumC = numel(Clay);
ClayPercentage = (NumC/n)*100;
set(handles.text15,'String',ClayPercentage);
NumSi = numel(Silt);
SiltPercentage = (NumSi/n)*100;
set(handles.text16,'String',SiltPercentage);
NumSa = numel(Sand);
SandPercentage = (NumSa/n)*100;
set(handles.text17,'String',SandPercentage);
I need help in getting the percentage. Every time I run the program all the text box shows is 100%. It is suppose to show on the Clay or Silt or Sand percentage not all the n value.

Réponses (2)

Image Analyst
Image Analyst le 27 Avr 2017
Modifié(e) : Image Analyst le 27 Avr 2017
Perhaps the "else if" is messing it up - treating it like two separate statements. Use "elseif" like you're supposed to and see if that works.
Please fix the indenting also. It's very hard to read right now, even after I somewhat fixed the formatting.

Image Analyst
Image Analyst le 27 Avr 2017
Since you assign Clay in the loop and the loop executes n time, then numel(Clay) will be n, and numel(Clay)/1*100 will be 100 always.
You should not add up diameters to get area fraction or number fraction anyway. You need something like
numClayParticles = numClayParticles + 1;
  1 commentaire
Tinna Armasamy
Tinna Armasamy le 27 Avr 2017
Hi, Thank you for the help. I have made the following changes but there is still a problem.
Area = zeros(n,1); Diameter = zeros(n,1); MajorAxis = zeros(n,1);
k = regionprops(cc,'Area','EquivDiameter','MajorAxisLength'); numClayParticles = 0; numSiltParticles = 0; numSandParticles = 0;
for i=1:n
Area(i) = k(i).Area;
Diameter(i) = sqrt(4*[k(i).Area]/pi);
MajorAxis(i) = k(i).MajorAxisLength;
if Diameter(i) < 0.002
numClayParticles = numClayParticles + 1;
elseif Diameter(i) >=0.002 && Diameter(i) < 0.05
numSiltParticles = numSiltParticles + 1;
elseif Diameter(i) >= 0.05 && Diameter(i) < 2
numSandParticles = numSandParticles + 1;
end
end
ClayPercentage = (numClayParticles/n)*100;
set(handles.text15,'String',ClayPercentage);
SiltPercentage = (numSiltParticles/n)*100;
set(handles.text16,'String',SiltPercentage);
SandPercentage = (numSandParticles/n)*100;
set(handles.text17,'String',SandPercentage);
The results is showing for SandPercentage only. The ClayPercentage and SiltPercentage is still zero. It is like it skips the if else of clay and silt and only perform sand if else.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by