I’ve been trying to wrap my head around this for ahwile and I’m not even sure if it’s possible, but at the very least I’m hoping someone can explain why it’s not possible and under what conditions (if any) I could make this work.
So here’s the problem: Given two images where some unkown foreground image is composited onto a pure white background and a pure black background. Is it possible to extract the foreground image?
Here’s an example set of images I’ve been using for this problem:
Dice on white
Dice on black
So far I’ve looked at the A onto B composite equations from this wikipedia article
And after re-arranging the second equation for the foreground RGB and then making some assumptions about the alpha values of the background and output always having full opacity (i.e. equal 1) I was left with:
## f = foreground, o = output, b = background
fRGB = (oRGB - bRGB * (1 - fAlpha)) / fAlpha
I figure I can then use this as an equality since fRGB
and fAlpha
are the same in both the onWhite and onBlack images.
(oBlackRGB - bBlackRGB * (1 - fAlpha)) / fAlpha = (oWhiteRGB - bWhiteRGB * (1 - fAlpha)) / fAlpha
I can re-arrange this to solve for fAlpha
. The problem is that given I have 3 color channels I can get different results for each one which makes this feel like a dead-end.
Something else I tried is is again rearranging for the second equation for fAlpha
fAlpha = (bRGB - oRGB) / (bRGB - fRGB)
Again, since fAlpha
should be the same this can be used as an equality and rearranged to to solve for fRGB.
Now, this result seemingly is a bit more useful at it gets the 3 color channels (correct as far as I can tell), but I’m not sure how to get the alpha channel?
Here’s my code for calculating the color channels as described above if that’s helpful
def reverseBlendRGB(foregroundA, backgroundA, foregroundB, backgroundB):
alpha = 0
rgb = [0, 0, 0]
for i in range(3):
ma = foregroundA[i]
mb = foregroundB[i]
ba = backgroundA[i]
bb = backgroundB[i]
d = (ma - ba + bb - mb)
if d > 0:
alpha = 255
rgb[i] = int((ma * bb - mb * ba) / d)
return (rgb[0], rgb[1], rgb[2], alpha)
Edit:
A similar question was brought to my attention here:
Calculate hex color with transparency based on other colors
The answer given follows very closely with what conclusions I’ve come to myself so far. At the end of the answer the author states:
Yes, my alpha isn’t a single value but a vector. That’s not right, but it’s good enough for those simple equations. You could set up more complex ones that carry each color component (RGB) individually, so that all color planes use the same alpha.
That seems to be exactly where I need some guidance. How would I setup and calculate these more complex equations to get a singular alpha?
Mikearonio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2