0

我想在检测到眼睛的位置插入太阳镜(png 图像)(仅考虑平面内旋转)。我们使用内置的 haar 级联检测器来检测 matlab 中的眼睛。目前,通过返回图像中眼睛位置的边界框突出显示检测到的眼睛。让我知道如何完成(我是 matlab 的初学者)。

4

1 回答 1

0

(假设两个图像都是灰度的,但扩展到 rgb 并不是那么困难。未经检查,我不保证我在某些时候没有翻转 x/y 坐标)。

如果您的眼睛位置大致在同一水平线上,例如 对于直脸:

face = your face image
ohyeah = your sunglasses image
fspan = horizontal distance between eyes, in pixels
(You should be able to get this easily from the bounding box info)

gspan = ditto for the sunglasses image
(You can do this manually - only need to measure it once)

cpos = central position, [x y] of the eyes
(Again, calc from bounding box)

检查您是否需要调整太阳镜图像的大小 - 可能仅在它比您需要的大得多或小得多时才需要:

sr = fspan/gspan;
if abs(sr-1)>thresh;
    ohyeah = imresize(ohyeah, sr);
end

现在,我们找到左上角和右下角:

pos1 = pos - ceil(size(ohyeah)/2); 
pos2 = pos1 + size(ohyeah) - 1;

注意:此时您可能希望/需要检查这些值是否超出原始图像的外边缘。

如果上述值是可接受的索引face,您可以简单地将太阳镜图像复制到:

face2 = face;
face2(pos1(1):pos2(1),pos1(2):pos2(2))=ohyeah;
imshow(face2);

这显然只是在正确的位置粘贴了一个矩形。您也可以更喜欢并使用面具,见下文:

对于旋转面:

1)旋转眼镜图像并调整其大小以匹配(例如,如果您知道眼镜图像上您希望眼睛中心移动的点,即简单的几何形状)。

2)制作一个 BW 蒙版,在眼镜所在的位置为 1,否则为 0(如果您的图像位于黑色背景上,则相对容易)。

3) 找到需要戴眼镜的位置

4)裁剪出人脸图像的适当部分:

face2 = face(pos1(1):pos2(1),pos1(2):pos2(2));

5)用你的太阳镜图像替换它的适当部分:

face2(BW) = ohyeah(BW);

6)将其粘贴回原始图像

face(pos1(1):pos2(1),pos1(2):pos2(2)) = face2;
于 2014-04-14T14:37:16.703 回答