how to infer alpha value from pair of opaque images

58 Views Asked by At

I have two images, one with a white background, and one with a black background, I want to convert these images into one semi transparent image

I have found that

color*alpha=blackBackgroundImage
color*alpha+(1-alpha)=whiteBackgroundImage

I want a function that takes blackBackgroundImage and whiteBackgroundImage and outputs the color and the alpha

I'm bad at math

1

There are 1 best solutions below

2
On BEST ANSWER

You're almost there. You just need to solve the system of equations:

    color*alpha=blackBackgroundImage
<=> color = blackBackgroundImage / alpha

Plugging that into the second equation:

    blackBackgroundImage / alpha * alpha + (1 - alpha)*(1,1,1) = whiteBackgroundImage
<=> blackBackgroundImage + (1 - alpha)*(1,1,1) = whiteBackgroundImage
<=> (1 - alpha)*(1,1,1) = whiteBackgroundImage - blackBackgroundImage

So alpha should be 1 + blackBackgroundImage.r - whiteBackgroundImage.r. The same applies to any other channel.