我正在做面部特征提取项目。我编写了用于直方图均衡,面部检测和面部裁剪的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'): -
答案 0 :(得分:3)
<强>算法: - 强>
我的解决方案使用以下算法实现您的任务:
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-Jones算法,被使用。
4. 如果此代码失败,您可以通过添加以下行检查是否正确检测到眼睛:
BBEyes= [BBLEye ; BBREye];
figure,
imshow(Q);
for i = 1:size(BBEyes,1)
rectangle('Position',BBEyes(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
对于您的图像,由于这有效,您仍然可以检查眼睛是否被正确检测到。结果如下: -