面部对齐后的眼睛位置

时间:2015-02-09 11:47:18

标签: image matlab math image-processing

我有一个问题是在面部对齐后找到眼睛中心(imrotate) 1000306439_a1744969b8_1369_11099615@N00.jpg

以下是代码:

img = imread('1000306439_a1744969b8_1369_11099615@N00.jpg');
eyes = [230 238; 126 124];
if length(size(IMG)) > 2
    img = rgb2gray(img);
end
subplot(1,2,1),imshow(img);hold on;
plot(eyes(1,:),eyes(2,:),'-rx'); hold off;
tg_a = diff(eyes(2,:))/diff(eyes(1,:));
angle = tg_a*(180/pi);
img_rot = imrotate(img, angle,'crop'); 
Cx = size(img,2)/2;
Cy = size(img,1)/2;
R_EyeX = (Cx+(eyes(1,1)-Cx)*cosd(tg_a)-(eyes(2,1)-Cy)*sind(tg_a));
R_EyeY = (Cy+(eyes(1,1)-Cx)*sind(tg_a)+(eyes(2,1)-Cy)*cosd(tg_a));
L_EyeX = (Cx+(eyes(1,2)-Cx)*cosd(tg_a)-(eyes(2,2)-Cy)*sind(tg_a));
L_EyeY = (Cy+(eyes(1,2)-Cx)*sind(tg_a)+(eyes(2,2)-Cy)*cosd(tg_a));
subplot(1,2,2),imshow(img_rot);hold on;
plot([R_EyeX L_EyeX],[R_EyeY L_EyeY],'-rx');hold off;

结果: enter image description here

1 个答案:

答案 0 :(得分:0)

固定代码:

img = imread('1000306439_a1744969b8_1369_11099615@N00.jpg');
eyes = [230 238; 126 124];
if length(size(IMG)) > 2
    img = rgb2gray(img);
end
subplot(1,2,1),imshow(img);hold on;
plot(eyes(1,:),eyes(2,:),'-rx'); hold off;
tg_a = diff(eyes(2,:))/diff(eyes(1,:));
angle = tg_a*(180/pi);
tg_a = -angle * (pi/180);
img_rot = imrotate(img, angle,'crop'); 
Cx = size(img,2)/2;
Cy = size(img,1)/2;
R_EyeX = (Cx+(eyes(1,1)-Cx)*cos(tg_a)-(eyes(2,1)-Cy)*sin(tg_a));
R_EyeY = (Cy+(eyes(1,1)-Cx)*sin(tg_a)+(eyes(2,1)-Cy)*cos(tg_a));
L_EyeX = (Cx+(eyes(1,2)-Cx)*cos(tg_a)-(eyes(2,2)-Cy)*sin(tg_a));
L_EyeY = (Cy+(eyes(1,2)-Cx)*sin(tg_a)+(eyes(2,2)-Cy)*cos(tg_a));
subplot(1,2,2),imshow(img_rot);hold on;
plot([R_EyeX L_EyeX],[R_EyeY L_EyeY],'-rx');hold off;