簡體   English   中英

裁剪后如何矯正傾斜的臉?

[英]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' ):-

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

輸出:-

產量


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

對於您的圖像,由於此方法有效,因此您仍然可以檢查是否已正確檢測到眼睛。 結果如下是正確的:

眼睛檢測

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM