use of "if" and "elseif" in regionprops
2 次查看(过去 30 天)
显示 更早的评论
I want you establish a range scale for the outputs on images for example if my value is coming between
value >= 7.94 & value <= 11.11 ==============> this should display "#3" on the image.
I have tried to develop a script with 'if" and "elseif" but I am unable to make it work.
Follwoing is the code I am using
L1 = bwlabel(barsH);
k=regionprops(L1,'Area','Perimeter','Centroid');
score= (([k.Area])./([k.Perimeter]/1.5));
figure, imshow(barsH);
morespace=60;
for cnt = 1:length(k)
if score >= 7.94 & score <= 11.11
score = "#3";
text(k(cnt).Centroid(1),k(cnt).Centroid(2)+morespace,...
num2str(score(cnt))+" mm",'FontSize',12,'color','red');
elseif score >= 11.12 & score <= 14.28
score = "#4";
text(k(cnt).Centroid(1),k(cnt).Centroid(2)+morespace,...
num2str(score(cnt))+" mm",'FontSize',12,'color','red');
elseif score >= 14.29 & score <= 17.45
score = "#5";
text(k(cnt).Centroid(1),k(cnt).Centroid(2)+morespace,...
num2str(score(cnt))+" mm",'FontSize',12,'color','red');
end
end
File "barsH.mat" is enclosed.
0 个评论
采纳的回答
Voss
2022-6-19
编辑:Voss
2022-6-19
Index score by cnt inside the loop, and don't use the same variable score for the numeric score and the label (#3, #4, etc.) you are creating. Also, the calls to text are identical, so you can move them to after the if/elseif.
load barsH
L1 = bwlabel(barsH);
k=regionprops(L1,'Area','Perimeter','Centroid');
score= (([k.Area])./([k.Perimeter]/1.5));
figure, imshow(barsH);
morespace=60;
score_type = strings(1,numel(k));
for cnt = 1:numel(k)
if score(cnt) >= 7.94 && score(cnt) <= 11.11
score_type(cnt) = "#3";
% elseif score(cnt) >= 11.12 && score(cnt) <= 14.28 % what about a score between 11.11 and 11.12?
elseif score(cnt) <= 14.28
score_type(cnt) = "#4";
% elseif score(cnt) >= 14.29 && score(cnt) <= 17.45 % what about a score between 14.28 and 14.29?
elseif score(cnt) <= 17.45
score_type(cnt) = "#5";
else
continue % what to do when score < 7.94 or score > 17.45?
end
text(k(cnt).Centroid(1),k(cnt).Centroid(2)+morespace,...
num2str(score(cnt))+" mm ("+score_type(cnt)+")",'FontSize',12,'color','red');
end
disp(score_type);
4 个评论
Image Analyst
2022-6-19
Maybe
else
score_type(cnt) = "??";
% what to do when score < 7.94 or score > 17.45
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!