4

I am trying to add two images of different sizes using bitwise operations in OpenCV using python. I want a particular point in Image1(an image of face of a person) to coincide with a particular point in Image2(image of a spectacle frame). The particular points are not the cornermost points of the images.I know the 2 mid points of the frame glasses and the pupil of the eyes. I want the frame mid points to coincide with the pupil points of the eyes in the face. The code which I am using adds the second image's leftmost corner point to the specific point of Image1 as in Line 10, whereas i want the mid point of left glass frame to be added.

The face image can be any random image and the spectacle image is as -

Image 2

I am using the code:

import cv2
import numpy as np

img_frame = cv2.imread('image1.jpg',1)
img_in = cv2.imread('face.jpg',1)
new_image = np.zeros(img_frame.shape,dtype=np.uint8)

i,j,k = img_frame.shape

for ii in range (1,i):
    for jj in range (1,j):
        pixel = img_frame[ii,jj]

        img_in[339+ii,468+jj] = pixel

cv2.imwrite('pc2_with_frame_7.jpg',img_in)            
cv2.imshow('win',img_in)
cv2.waitKey(0)
cv2.destroyWindow('win')

Any kind of help would be appreciated.

Thank you.

M4rtini
  • 13,186
  • 4
  • 35
  • 42
Anuradha
  • 1,089
  • 4
  • 18
  • 29
  • You'll need to calculate the x-distance between the pupils and the x-distance between the centres of the glasses and scale the glasses by that ratio to fit the face width. – Mark Setchell May 19 '14 at 06:34
  • You'll also need to calculate whether the eyes are level, i.e. if the y-coordinates of the two eyes are the same, if not, you will need to calculate the y-difference between the eyes and divide it by the x-difference, take the tan inverse and use that angle to rotate the glasses. – Mark Setchell May 19 '14 at 06:40
  • yes, scaling part is already done, This is the next problem, that is to place it at corect place – Anuradha May 19 '14 at 07:20
  • Will you change the glasses picture to be a PNG, which will support transparency (whereas JPEGs do not) and allow the eyes to show through? – Mark Setchell May 19 '14 at 07:32
  • yes,I would like the eyes to be shown or it can only be the opaque part or just the frame of the spec. – Anuradha May 19 '14 at 09:20

1 Answers1

2

Ok, it seems nobody else much can help so I will offer what I can...

What you are trying to do is called alpha-compositing. You can read about it here on Wikipedia and also here in the OpenCV documentation.

My tool of choice for this would be ImageMagick, which is free and has Perl, Python, C/C++ bindings as well as command-line tools. If I start with this photo (face.jpg):

enter image description here

and take your glasses.jpg file and convert it to a PNG with transparency, whcih looks like this:

enter image description here

I can run the following ImageMagick command at the Terminal

composite glasses.png face.jpg out.jpg

and I get this:

enter image description here

It seems that OpenCV has problems maybe with transparency, and a solution is presented here. If you want to try the masking method suggested by @ypnos in that post, I have made you the necessary input files and you can download them from my website at:

glasses.png with alpha channel

input-mask.png

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I was able to convert the file into .png but was not able to extract alpha channel using - convert input.png -channel Alpha -negate -separate input-mask.png – Anuradha May 20 '14 at 04:05
  • How does your glasses.png look like that? Did you edit the image? How did you do that?I am not able to remove the white pixels from the background – Anuradha May 20 '14 at 04:11
  • I loaded your glasses.jpg into Photoshop and used Select->Color Range to select the black frames, then I inverted the selection so I had the "white/clear" areas selected then I deleted the selection so it looks like a chessboard and saved as PNG because that format supports transparency. – Mark Setchell May 20 '14 at 06:31
  • You do not need to extract the alpha channel, and indeed you cannot extract the alpha channel from a file that was a JPEG since JPEGs cannot contain transparency. – Mark Setchell May 20 '14 at 06:36