MATLAB: I want to count the number of metric elements in if statement using ‘sum’, but it shown all the content of elements, not the number. I’m new.. pls help..

circularityif statementImage Processing Toolboxsum

here the code
stats = regionprops(L,'Area','Centroid', 'BoundingBox');
threshold = 0.88;
for k = 1:length(B)
boundary = B{k};
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
area = stats(k).Area;
metric = 4*pi*area/perimeter^2;
metric_string = sprintf('%2.2f',metric);
if metric >= threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
as = sum(metric);
end
end

Best Answer

Way too clumsy. Try something like this (untested):
stats = regionprops(L,'Area','Centroid', 'Perimeter');
allAreas = [stats.Area];
allPerimeters = [stats.Perimeter];
circularities = (4*pi*allAreas) ./ allPerimeters.^2; % Inverted compared to how I use it.
threshold = 0.88;
roundObjects = circularities > threshold;
as = sum(circularities(roundObjects));
Related Question