0

I'm trying to change the perspective of a fake glasses image with OpenCV, but the transparent parts and opacity get lost. Resulting image has no transparencies. I want to change the perspective in order to stamp the resulting image over another image.

Can I do this with OpenCV?

#!/usr/bin/python
import numpy as np
import cv2

glasses = cv2.imread('fake_glasses.png')

RES_SIZE = (500,640)

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, RES_SIZE)
cv2.imwrite("rotated_glasses.png", rotated)

fake_glasses.png (with transparent parts

mask.png

xabi
  • 193
  • 1
  • 9

1 Answers1

2

You are loading the image incorrectly, dropping the transparency layer. This is easy to verify -- print the shape of the image after you load it.

>>> img1 = cv2.imread('fake_glasses.png')
>>> print(img1.shape)
(209, 600, 3)

When not specified, the flags parameter of imread is set to IMREAD_COLOR. According to the documentation this means

If set, always convert image to the 3 channel BGR color image.

Instead, you should use IMREAD_UNCHANGED

If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

With this change, the image loads correctly, including the alpha plane.

>>> img2 = cv2.imread('fake_glasses.png', cv2.IMREAD_UNCHANGED)
>>> print(img2.shape)
(209, 600, 4)
Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
  • Is there a way to "stamp" the transformed image into another image? I'm trying to do it with cv2.warpPerspective but I can't get to the point. Should be something like: `final_image = cv2.warpPerspective(glasses, M, RES_SIZE, face_image, borderMode=cv2.BORDER_TRANSPARENT)` – xabi Feb 10 '17 at 11:24
  • 1
    ValueError: operands could not be broadcast together with shapes (614,500,3) (640,500,3) – xabi Feb 10 '17 at 11:39
  • This is my code: [link](https://drive.google.com/open?id=0B8kqD8em1Hp-WTVpTEE2dVI1YTg) – xabi Feb 10 '17 at 11:42
  • Close but no cigar :) ... now images are blended, but with wrong colors. Glasses are red, but blended are black [link](http://imgur.com/a/LsWqo) – xabi Feb 10 '17 at 11:59
  • @xabi Odd, this [variant of your script](http://pastebin.com/40aRrCTA) produces [this result](http://i.imgur.com/e9yTmBr.png) on my machine (tried both OpenCV 2.4.11 and 3.1.0). – Dan Mašek Feb 10 '17 at 12:04
  • Hmm, hard to tell what version it really is based on that, but seems pretty old, since they [fixed that version display bug](http://code.opencv.org/issues/1804) sometime in 2012 (and [merged](https://github.com/opencv/opencv_attic/commit/061c7aed0c1f89445736bbbf4119079ce267b8b0#diff-1f98d22cee58d7695217d8cc7557fca4R1013) it to trunk soon after that). | At this point I don't really see how you could end up with the black glasses... – Dan Mašek Feb 10 '17 at 12:31
  • Testing in other linux machine with clean opencv and works like a charm. – xabi Feb 10 '17 at 12:47