Question

I want to insert sunglasses(png image) at a position where the eyes gets detected(considering only the in-plane rotation). We have used the in-built haar cascade detector for detecting the eyes in matlab. Currently have the eyes detected highlighted by the bounding boxes which return the position of the eyes in the image.Let me know how it can be done(I am a beginner in matlab).

Was it helpful?

Solution

(Presuming both images are greyscale, but expansion to rgb is not that difficult. Unchecked and I'm not promising I haven't flipped x/y coords at some point).

If your eye positions are roughly on the same horizontal line, e.g. For a straight face:

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)

Check if you need to resize the sunglasses image - might only need to if it's a lot larger or smaller than you need:

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

Now, we find the top left and bottom right corners:

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

Note: you may want/need to check at this point that those values don't go beyond the outer edges of your original image.

Provided that the above values are acceptable indices into face, you can simply copy the sunglasses images over:

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

This obviously just pastes a rectangle in the correct position. You can also be fancier and use a mask, see below:

For rotated faces:

1) Rotate and resize the glasses image to match (for example if you know the points on the glasses image where you want the centres of the eyes to go that is simple geometry).

2) Make a BW mask which is 1 where the glasses are and 0 otherwise (relatively easy if your image is on a black background).

3) Find the position where you need to put the glasses on

4) Clip out the appropriate part of the face image:

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

5) Replace the appropriate parts of it with your sunglasses image:

face2(BW) = ohyeah(BW);

6) Stick it back into the original image

face(pos1(1):pos2(1),pos1(2):pos2(2)) = face2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top