if文で数値の条件が​合っているはずなのに​正しく表示されない

8 views (last 30 days)
tsuyoshi tsunoda
tsuyoshi tsunoda on 30 Jan 2022
Commented: tsuyoshi tsunoda on 30 Jan 2022
以下のプログラムで顔のパーツ(口の重心、三角形の重心)から顔の向きを検出しているのですが、条件文で正しく表示されません。
右向きの判別の時は正しくされているのですが、左向きの判別が正しくされません。
位置関係を計算すると、おそらく-2.6ぐらいになると思っているのですが左向きと表示されません。
どこに問題があるのか分からないため教えていただきたいです。
I = imread('画像');
for Obj = {'FrontalFaceCART','Mouth','EyePairBig'} % 学習済カスケード分類モデル毎に繰り返す
ObjDet = vision.CascadeObjectDetector(Obj{:}); % カスケード検出器のオブジェクト作成
ObjBox = ObjDet(I); % 検出枠の[x, y, width, height]情報
if(strcmp(Obj,'Mouth')) % 口の検出時に行う処理
ObjBox(ObjBox(:,2) < size(I,1)/2, :) = []; %【暫定】画像の上半分に検出された口の検出枠を削除
ObjBox(ObjBox(:,3) ~= max(ObjBox(:,3)), :) = []; %【暫定】幅が最大の物を残し口の検出枠を削除
MouthBox = [ObjBox(:,1), ObjBox(:,2), ObjBox(:,3), ObjBox(:,4)/2]; % 口の上半分
I = insertShape(I,'rectangle', MouthBox, 'Color', 'red', 'LineWidth', 3); % 追記
ObjCen = get_centroid(MouthBox); % 検出枠の重心[x,y]座標。これを画像に追記する
I = insertShape(I, 'FilledCircle', [ObjCen ones(size(ObjBox,1),1)*5]); % 追記
Triangle(3,:) = ObjCen; % 三角形の座標を保持
end
if(~strcmp(Obj,'FrontalFaceCART')) % 顔の検出時以外に行う処理
I = insertObjectAnnotation(I, 'rectangle', ObjBox, Obj); % 画像に検出枠を追記
end
if(strcmp(Obj,'EyePairBig')) % 両目の検出時に行う処理
for wd = 0:1 % 検出枠の左上x座標に幅の1/6または4/6を足し、幅を1/6にする⇒左目枠/右目枠の作成
EyeBox = [ObjBox(:,1)+ObjBox(:,3)*(wd*2)/3, ObjBox(:,2), ObjBox(:,3)/3, ObjBox(:,4)];
EyeCen = get_centroid(EyeBox); % 検出枠の重心[x,y]座標
I = insertShape(I,'rectangle', EyeBox, 'Color', 'red', 'LineWidth', 3);
I = insertShape(I, 'FilledCircle', [EyeCen ones(size(ObjBox,1),1)*5]);
Triangle(wd+1,:) = EyeCen; % 三角形の座標を保持
if(wd == 1) % 三角形の座標が揃った時(口の検出後⇒左目の検出後⇒右目の検出時)に行う
triangle = polyshape(Triangle(:,1),Triangle(:,2)); % 重心点を結んだ三角形を定義
[Trcntx, Trcnty] = centroid(triangle); % 三角形の重心
I = insertShape(I, 'Polygon', Triangle, 'Color', 'green'); % 三角形を画像に追記する
I = insertShape(I, 'FilledCircle', [Trcntx, Trcnty, 5], 'Color', 'red'); % 三角形の重心を画像に追記する
end
end
end
release(ObjDet);
end
imshow(I)
if ObjCen - Trcntx > 0.5
text(Trcntx-10,Trcnty-100,'右向き','FontSize',20);
elseif ObjCen - Trcntx < -0.5
text(Trcntx-10,Trcnty-100,'左向き','FontSize',20);
else
text(Trcntx-10,Trcnty-100,'正面','FontSize',20);
end
%%位置の表示
text(Trcntx+10,Trcnty+10,['centroid:[' num2str(Trcntx) ',' num2str(Trcnty) ']'],'FontSize',16);
text(Trcntx+10,Trcnty+60,['centroid:[' num2str(ObjCen) ',]'],'FontSize',16);
function Center = get_centroid(xywh) % 検出枠の重心[x,y]座標を求める
Center = [xywh(:,1) + xywh(:,3)/2, xywh(:,2) + xywh(:,4)/2];
end

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 30 Jan 2022
2つの口が検出されていて、if文の判定対象がベクトルになっています。if文でベクトルを判定する場合、一つでもfalse(非ゼロ)が含まれると、判定結果は偽になります。口が複数検出された場合のロジックを設計する必要がありますね。
if [true false]
disp('TRUE');
else
disp('FALSE');
end
FALSE
  1 Comment
tsuyoshi tsunoda
tsuyoshi tsunoda on 30 Jan 2022
そうゆうことだったのですね、ありがとうございます。

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!