从另一个图像中删除覆盖图像 [英] Removing an overlay image from another image
问题描述
所以我有两张图片,原始一张和一张应该是它上面的叠加层.叠加图像是半透明的,比如说白色,alpha 值为 0,5.
So I have two images, the original one and one which is supposed to be the overlay on top of it. The overlay image is semi-transparant, let's say white with alpha 0,5.
我可以将半透明的与原始的重叠,但如何反转此过程?因此,在此代码示例中,如何仅使用 'bld' 和 'fil' 变量来获取 'org' 变量.有没有办法做到这一点?
I can overlay the semi transparant one with the original but how do I reverse this process? So in this code example, how can I get the 'org' variable by only using the 'bld' and the 'fil' variables. Is there an approach that can do this?
import cv2
import numpy as np
import cv2
# Load a colored image and a filter
org = cv2.imread('original.png', cv2.CV_LOAD_IMAGE_UNCHANGED)
fil = cv2.imread('filter.png', cv2.CV_LOAD_IMAGE_UNCHANGED)
# Overlay the filter on the original image
bld = cv2.addWeighted(org,0.5,fil,0.5,0)
# Reverse the process?
推荐答案
well... for linear blending bld = a*org + (1-a)*fil
(在你的例子中 a = 0.5
)
well... for linear blending bld = a*org + (1-a)*fil
(in your example a = 0.5
)
so org = (bld - (1-a)*fil)/a
如果我没记错的话,就是 org = 1/a * bld + (1-1/a) * fil
.
which is org = 1/a * bld + (1-1/a) * fil
if I'm not wrong.
with a = 0.5
: org = 2*bld -1*fil
在代码中:
a = 0.5
org = cv2.addWeighted(bld,1/a,fil,1-1/a, 0)
或
org = cv2.addWeighted(bld,2,fil,-1, 0)
您也可以使用 org = 2*bld - fil
但如果超出例如,openCV 会截断这些值.255 用于 8U 类型(称为 saturate_cast),因此如果您在计算前不转换为 16/32 位类型,这将不起作用.
you could also use org = 2*bld - fil
but openCV truncates the values if they go beyond e.g. 255 for 8U types (called saturate_cast), so this won't work if you dont convert to 16/32 bit types before computation.
通常,如果您没有线性混合,则必须将第一个公式更改为 bld = a*org + b*fil
并从中计算其余部分.
In general, if you didnt blend linearly, you would have to change the first formula to bld = a*org + b*fil
and compute the rest from that.
这篇关于从另一个图像中删除覆盖图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!