use of "if" and "elseif" in regionprops

조회 수: 2 (최근 30일)
Abdul Hannan Qureshi
Abdul Hannan Qureshi 2022년 6월 19일
댓글: Image Analyst 2022년 6월 19일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by