Reputation: 147
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.
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?
Upvotes: 0
Views: 2337
Reputation: 20160
well... for linear blending bld = a*org + (1-a)*fil
(in your example a = 0.5
)
so org = (bld - (1-a)*fil) / a
which is org = 1/a * bld + (1-1/a) * fil
if I'm not wrong.
with a = 0.5
: org = 2*bld -1*fil
in code:
a = 0.5
org = cv2.addWeighted(bld,1/a,fil,1-1/a, 0)
or
org = cv2.addWeighted(bld,2,fil,-1, 0)
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.
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.
Upvotes: 3