裁剪后如何拉直倾斜的脸?

How to straighten a tilted face after cropping?

我正在做一个面部特征提取的项目。我已经为直方图均衡、面部检测和面部裁剪编写了 MATLAB 代码。现在我想把倾斜的脸拉直。你能帮我处理 MATLAB 代码吗?这是我到目前为止编写的代码。

clear all
clc

I=imread('100_3082.jpg');
figure(1)
imshow(I);
J=rgb2gray(I);
figure(2)
imshow(J);                                                             
P = histeq(J);
figure(3)
imshow(P);

FDetect = vision.CascadeObjectDetector;

BB = step(FDetect,P);
hold on
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r');

end
for i = 1:size(BB,1)
Q= imcrop(P,BB(i,:));
figure(4)
imshow(Q);
end
title('Face Detection');   

hold off;

这是图像('100_3082.jpg')我正在处理:-

算法:-
我的解决方案使用以下算法实现您的任务:

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)))));

输出:-


P.S:
1. 这可能不是一个完美的解决方案。
2.假设只有一张斜脸需要矫正。
3.这个解决方案的准确性取决于内置 MATLAB 函数的眼睛检测的准确性,它基于 Viola -使用琼斯算法
4. 如果此代码失败,您可以通过添加以下行来检查是否正确检测到眼睛:

BBEyes= [BBLEye ; BBREye];
figure,
imshow(Q); 
for i = 1:size(BBEyes,1)
 rectangle('Position',BBEyes(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end

对于您的图像,由于此方法有效,您仍然可以检查是否正确检测到眼睛。结果如下是正确的:-