python - 在 python 中屏蔽然后粘贴两个图像

标签 python numpy scipy python-imaging-library scikit-image

我有一个看起来像这样的图像:

enter image description here

我想将背景转换为白色,将所有其他像素转换为黑色,以便我的图像看起来像这样:

enter image description here

假设原始图像是img,上面的结果是mask。当我尝试从原始图像中获取mask时,事情并没有按预期进行。我这样做了:

mask = np.ones_like(img)*255
mask[img > 0] = 0

理想情况下我应该得到预期的结果,但这就是我得到的结果。

enter image description here

此外,我还有另一张图像,如下所示:

enter image description here

我想将预期的蒙版粘贴到最终的日落图像上。如何使用 numpy/scipy/PIL/skimage 做到这一点?

最佳答案

由于我们希望将 img 中任何非黑色的内容在 mask 中设置为零,因此只需沿着三个 channel (最后一个轴)并使用该 bool 数组掩蔽到 mask -

mask[(img>0).any(-1)] = 0

给定样本 #1 的输出 -

enter image description here

将其与日落图像混合img2 -

from scipy.misc import imresize

mask_resized = imresize(mask, size=img2.shape)
out = (mask_resized==255)*img2 

输出 -

enter image description here

关于python - 在 python 中屏蔽然后粘贴两个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45980728/

相关文章:

python - 在 python 中为一对分布生成 MLE

python - 如何从 python 中执行加载数据命令

python-2.7 - pandas.DataFrame.describe() 与 numpy.percentile() NaN 处理

python - 作为积分变量的函数

python - Python 中的术语文档矩阵和余弦相似度

python - 2x2 矩阵的数值稳定逆

python - 为什么 tkinter/window.update 在我的程序中随着时间的推移变得更慢?

python - 如何系统地识别 Python 在其可访问的包/模块树中的依赖关系?

python - 如何操作 numpy.loadtxt 后的数据?

Python 等同于 MATLAB 的 normplot?