use of "if" and "elseif" in regionprops
    조회 수: 3 (최근 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!
