william malo
william malo

Reputation: 2506

how to infer alpha value from pair of opaque images

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

Upvotes: 0

Views: 44

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32667

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.

Upvotes: 1

Related Questions