Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import numpy as np
- import cv2
- def blend_transparent(face_img, overlay_t_img):
- # Split out the transparency mask from the colour info
- overlay_img = overlay_t_img[:,:,:3] # Grab the BRG planes
- overlay_mask = overlay_t_img[:,:,3:] # And the alpha plane
- # Again calculate the inverse mask
- background_mask = 255 - overlay_mask
- # Turn the masks into three channel, so we can use them as weights
- overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
- background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)
- # Create a masked out face image, and masked out overlay
- # We convert the images to floating point in range 0.0 - 1.0
- face_part = (face_img * (1 / 255.0)) * (background_mask * (1 / 255.0))
- overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))
- # And finally just add them together, and rescale it back to an 8bit integer image
- return np.uint8(cv2.addWeighted(face_part, 255.0, overlay_part, 255.0, 0.0))
- glasses = cv2.imread('fake_glasses.png', -1)
- face = cv2.imread('cage.png', -1)
- pts1 = np.float32([[ 0, 0], [599, 0],
- [ 0,208], [599,208]])
- pts2 = np.float32([[ 94,231], [354,181],
- [115,316], [375,281]])
- M = cv2.getPerspectiveTransform(pts1,pts2)
- rotated = cv2.warpPerspective(glasses, M, (face.shape[1], face.shape[0]))
- cv2.imwrite("rotated_glasses.png", rotated)
- result_2 = blend_transparent(face, rotated)
- cv2.imwrite("merged_transparent.png", result_2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement