python - 将图像与蒙版一起添加
问题描述
所以我目前有下面2张图片。这些是在图像的前景和背景部分使用神经风格转移网络获得的。
使用的代码如下:
add = cv2.add(image1, image2)
cv2.imshow('Addition', add)
cv2.waitKey(0)
cv2.destroyAllWindows()
我已将它们合并以创建以下结果图像:
但是,我想在获得的图片上使用显着性蒙版。显着面具在下面。我尝试将掩码参数输入到 add 函数但没有运气。我已经尝试查看这篇文章,但无法弄清楚该怎么做基于蒙版将 2 个图像添加到一起
编辑:
发现问题是我没有转换数据类型。更新后的代码是:
saliencyMap = cv2.resize(saliencyMap, (384,384))
newmap = saliencyMap.astype(np.uint8)
add = cv2.add(image1, image2, mask=newmap)
cv2.imshow('addition', add)
cv2.waitKey(0)
但是,生成的图像是,我不知道为什么:
解决方案
我认为您可能想要的只是将两个图像的大小调整为相同的大小,然后添加它们。然后使用遮罩在 Python/OpenCV 中进行强光合成。
图像1:
图像2:
掩码(显着性):
import cv2
import numpy as np
# read image 1
img1 = cv2.imread('img1.png')
hh, ww = img1.shape[:2]
# read image 2 and resize to same size as img1
img2 = cv2.imread('img2.png')
img2 = cv2.resize(img2, (ww,hh))
# read saliency mask as grayscale and resize to same size as img1
mask = cv2.imread('mask.png')
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
mask = cv2.resize(mask, (ww,hh))
# add img1 and img2
img12 = cv2.add(img1, img2)
# get mean of mask and shift mean to mid-gray
# desirable for hard light compositing
# add bias as needed
mean = np.mean(mask)
bias = -32
shift = 128 - mean + bias
mask = cv2.add(mask, shift)
mask = cv2.merge([mask,mask,mask])
# threshold mask at mid gray and convert to 3 channels
# (needed to use as src < 0.5 "if" condition in hard light)
thresh = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)[1]
# do hard light composite of img12 and mask
# see CSS specs at https://www.w3.org/TR/compositing-1/#blendinghardlight
img12f = img12.astype(np.uint8)/255
maskf = mask.astype(np.uint8)/255
threshf = thresh.astype(np.uint8)/255
threshf_inv = 1 - threshf
low = 2.0 * img12f * maskf
high = 1 - 2.0 * (1-img12f) * (1-maskf)
result = ( 255 * (low * threshf_inv + high * threshf) ).clip(0, 255).astype(np.uint8)
# save results
cv2.imwrite('img12_hardlight.png', result)
# show results
cv2.imshow('img12', img12)
cv2.imshow('mask', mask)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
推荐阅读
- python - 如何仅将字符串数组的整数转换为numpy中的浮点数组?
- json - 如何从 api.weather.gov 获取德克萨斯车站或区域或办公室或网格点或其他任何东西的每小时预报?
- css - 如何应用 object-fit: 也包含到 div?
- symfony4 - @Assert\Choice(callback="...") 是否仅适用于表单构建器?
- kubernetes - 如何将 Windows docker 容器部署到 docker-for-desktop Kubernetes 集群?拉取访问被拒绝
- autodesk-viewer - 恢复状态下的剖切平面工具位置错误
- c# - 带有表达式的 EF LINQ 查询:预期方法名称 - 如何将表达式传递给函数以在 EF 查询中使用?
- reactjs - 如何仅在 ReactJS 中的组件上使用 reactstrap?
- c++ - 未调用 mosquitto c API 的回调函数
- lisp - 在 Common Lisp 的 Progn 中加载包