Cannot get final output for for loop
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Image Analyst
on 27 Apr 2017
Edited: Image Analyst
on 27 Apr 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.
0 Comments
Image Analyst
on 27 Apr 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;
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!