Alexandra
Alexandra

Reputation: 35

open cv2 python multiply images

I have this simple code and I'm new at learning open cv2. This was supposed to do the chroma key efect but isn't working. Here's the code

import cv2

objectImage = cv2.imread("falcon.jpg")
background = cv2.imread("florest.jpg")
mask = cv2.imread("mask.png")

falcon = cv2.multiply(objectImage, mask)

back = cv2.multiply(background, (255 - mask))

result = cv2.add(falcon, back)

cv2.imshow("Image",result)
cv2.waitKey(0)
cv2.destroyWindow("Image")

And here's the result:

enter image description here

And here is what it was supposed to look like:

enter image description here

Thanks to anyone in advance!

Upvotes: 1

Views: 21257

Answers (2)

Pe Dro
Pe Dro

Reputation: 3043

A better alternative would be

result = cv2.bitwise_and(image, mask)

For anyone who thinks that they are OK with

result = (image/255.)*(mask/255)*255

just because it does generate a similar output. Avoid it as it does not involve OpenCV ensuring that the image and the mask are homogeneous (same shape and stride). When feeding the result as inputs to other OpenCV methods like findContours(), you are very likely to enter into high-verbose errors which only tell you that the image properties are not same (even though you think .shape for both image and mask outputs the same values.) Read more on strides difference when the shape seems same.

Also, ensure that both image and mask possess the same datatype (use mask.dtype) before using the together.

Upvotes: 1

Alexandra
Alexandra

Reputation: 35

Already made it I needed to divide by 255 to normalize the values

Upvotes: 1

Related Questions