简体   繁体   English

打开 cv2 python 乘以图像

[英]open cv2 python multiply images

I have this simple code and I'm new at learning open cv2.我有这个简单的代码,而且我是学习 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!提前感谢任何人!

我已经做到了,我需要除以255来归一化这些值

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).只是因为它确实生成了类似的 output。避免它,因为它不涉及 OpenCV,确保imagemask是均匀的(相同的形状和步幅)。 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.当将result作为输入提供给其他 OpenCV 方法(如findContours()时,您很可能会遇到非常详细的错误,这些错误只会告诉您图像属性不相同(即使您认为imagemask输出都为.shape相同的值。)阅读更多关于形状看起来相同时的步幅差异

Also, ensure that both image and mask possess the same datatype (use mask.dtype ) before using the together.此外,在一起使用之前,请确保imagemask具有相同的数据类型(使用mask.dtype )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM