use of "if" and "elseif" in regionprops

1 view (last 30 days)
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.

Accepted Answer

Voss
Voss on 19 Jun 2022
Edited: Voss on 19 Jun 2022
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" "#4" "#3" "#4" "#4" "#4" "#3" "#5"
  4 Comments
Abdul Hannan Qureshi
Abdul Hannan Qureshi on 19 Jun 2022
@Voss Thanks alot. very much appreciated. I have no query now.
Image Analyst
Image Analyst on 19 Jun 2022
Maybe
else
score_type(cnt) = "??";
% what to do when score < 7.94 or score > 17.45

Sign in to comment.

More Answers (0)

Categories

Find more on Agriculture in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!