算法:-
我的解决方案使用以下算法实现您的任务:
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.假设只有一个倾斜面需要矫正。
3.该解决方案的精度取决于使用基于Viola-Jones 算法的内置 MATLAB 函数的眼睛检测精度。
4.如果此代码失败,您可以通过添加以下行来检查是否正确检测到眼睛:
BBEyes= [BBLEye ; BBREye];
figure,
imshow(Q);
for i = 1:size(BBEyes,1)
rectangle('Position',BBEyes(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
对于您的图像,由于这有效,您仍然可以检查是否正确检测到眼睛。结果如下是正确的:-