アルゴリズム:-
私のソリューションは、次のアルゴリズムを使用してタスクを実装します。
1.両目の位置を見つける。
2.それらの間の角度を見つける。
3.その角度に基づいて画像を回転します。
入力画像:-
このコードの入力画像は、コードの最後に取得するものですQ
。

コード:-
% Dividing the image in two halves for better detection
% To see why , see this: https://www.mathworks.com/matlabcentral/answers/155126-how-does-the-vision-cascadeobjectdetector-detect-left-and-right-eyes-separately-it-is-constantly-de
n = fix(size(Q,2)/2);
lefthalf = Q(:,1:n,:);
righthalf = Q(:,n+1:end,:);
RightEyeDetect = vision.CascadeObjectDetector('RightEyeCART');
LeftEyeDetect = vision.CascadeObjectDetector('LeftEyeCART');
% vision.CascadeObjectDetector(EyePairBig) is not much efficient in this case
% because the image is tilted. So, detecting both eyes separately.
%Bounding Boxes
BBREye= step(RightEyeDetect,lefthalf); %Right eye is on our left
BBLEye= step(LeftEyeDetect,righthalf); %Left eye is on our right
BBLEye(1)=BBLEye(1)+n; %correcting the x position of left eye (caused due to dividing the image in two halves)
figure
imshow(imrotate(Q,(180/pi)*atan((BBREye(2)-BBLEye(2))/(BBREye(1)-BBLEye(1)))));
出力:-

PS:
1.これは完璧な解決策ではないかもしれません。
2.補正する傾斜面は 1 つだけであると仮定します。
3.このソリューションの精度は、 Viola-Jones Algorithmに基づく組み込みの MATLAB 関数が使用される目の検出の精度に依存します。
4.このコードが失敗した場合は、次の行を追加して目が正しく検出されたかどうかを確認できます。
BBEyes= [BBLEye ; BBREye];
figure,
imshow(Q);
for i = 1:size(BBEyes,1)
rectangle('Position',BBEyes(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
画像については、これが機能したため、目が正しく検出されたかどうかを確認できます。結果は次のとおりです。これは正しいです:-
