How to put an image on a t-shirt using Python OpenCV

Question:

I’m trying to put any custom image on a blank t-shirt as done below

enter image description here

I’m using Python’s CV2. Any idea how that could be done? I’m thinking cv2.inRange could point me to the white t-shirt and then I could resize the image to fit the shirt, but I’m not sure how to preserve the shadow and wrinkle effects. Maybe with edge detection?

Asked By: rcmenoni

||

Answers:

You want to "blend" the overlay onto the image. Open an image editor and explore the blend modes it has for layers. I would suggest trying the "multiply" and "overlay" modes.

OpenCV has some limited support for blending but I would suggest formulating the calculation yourself. The Wikipedia article should help. For a "multiply", I would convert both pictures to CV_32F, scale them by 1/255 (so the range is 0.0 .. 1.0), then just multiply them elementwise.

alpha = overlay[:,:,3] * np.float32(1/255) # alpha channel
overlay_color = overlay[:,:,0:3]
composite = base_image * (1-alpha) + overlay_color * alpha
# and deal with data types and value ranges...

Getting a mask for the t-shirt is a useful step. That will let you place the overlay only on the t-shirt, not on the background or skin of the model.

multiply blend using SO logo with white background on a picture from pxhere.com

Edge detection is not at all applicable here.

Answered By: Christoph Rackwitz