'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:

And here is what it was supposed to look like:

Thanks to anyone in advance!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pe Dro |
