【问题标题】:Adding Images Together With A Mask将图像与蒙版一起添加
【发布时间】:2021-10-03 06:39:59
【问题描述】:

所以我目前在下面有 2 张图片。这些是在图像的前景和背景部分使用神经风格转移网络获得的。

使用的代码如下:

add = cv2.add(image1, image2)
cv2.imshow('Addition', add)
cv2.waitKey(0)
cv2.destroyAllWindows()

我已将它们合并以创建以下结果图像:

但是,我想在获得的图片上使用显着性蒙版。显着面具在下面。我尝试将掩码参数输入到 add 函数但没有运气。我试过看这篇文章,但不知道该怎么做Add 2 images together based on a mask

编辑:

发现问题是我没有转换数据类型。更新后的代码是:

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)

但是,生成的图像是,我不确定为什么:

【问题讨论】:

  • add = cv2.add(image1, image2, mask=maskimage) 其中 maskimage 为 8 位,单通道(灰度)。如果这不符合您的要求,请说明您希望蒙版如何针对这两个图像起作用。所有 3 张图片的宽度和高度必须相同,并正确对齐。
  • 所以我尝试了这个。我很确定这 3 个图像的尺寸相同,我尝试将它们分成 3 个不同的图像。但是我在函数'cv::arithm_op'中收到以下错误(-215:断言失败)(mtype == CV_8UC1 || mtype == CV_8SC1)&& _mask.sameSize(*psrc1)。我尝试查看尺寸并重塑所有内容,使它们相同,但仍然没有运气。
  • 每张图片的形状是什么?你能把那个贴出来吗?据我所知,您发布的蒙版图像与其他两张图像的尺寸不同。同时发布 dtypes。
  • 我已经添加了我发现的问题,但仍然得到一个奇怪的结果。
  • 您真的要这样添加它们吗?也许将一个乘以掩码,并将另一个乘以掩码的倒数。假设每个图像和掩码是 8 位,将每个除以 255。然后添加它们,剪辑到范围 0 到 255 并确保结果为 8 位。 result=(I1*Mask+I2*(255-Mask))/255 然后result=result.clip(0,255).astype(np.uint8)

标签: python opencv image-processing style-transfer


【解决方案1】:

我认为您可能想要的只是将两个图像的大小调整为相同大小,然后添加它们。然后使用遮罩在 Python/OpenCV 中进行强光合成。

img1:

img2:

掩码(显着性):

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()

结果:

【讨论】:

  • 这真是太棒了!我喜欢这种方法,从不知道你可以做到这一点。如果我做多张不同的图片,我需要改变偏差值吗?我计划对不同的内容和样式图像使用这种方法,并使用地图过滤所有内容。此外,有没有办法强调眼睛的颜色而不是让眼睛变白,而不是让它变亮?
  • 你可能会。这取决于组合图像的外观,更重要的是显着性的外观。原则上,显着性应具有相同的暗和亮,以使平均值为中灰色。但这对于这张图片和我的偏好来说有点太暗了,所以我添加了一个偏差。但我只尝试过你的一张图片。一旦你尝试更多,你就会更好地了解需要什么。
  • 强光复合在蒙版亮的地方变亮,在蒙版暗的地方变暗。
  • 这是否意味着我需要为每张图片添加自己的偏见?有没有办法自动增加每个显着图的亮度?
  • 我已经包含了通过将平均值调整为中灰色来规范化显着性的代码。但这并不完全符合我的口味,所以我添加了一个偏见。您将不得不使用其他图像进行测试。我不能说它有多强大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多