Home > Software design >  Understanding alpha blending operation
Understanding alpha blending operation

Time:04-13

This question is regarding a clarification I needed in the accepted answer pertaining to the right way of implementing alpha-blending(link provided here): How to do alpha compositing with a list of RGBA data in numpy arrays?

In the step where the output image's RGB values are populated, there is a division by the precalculated output alpha values(relevant snippet pasted below)

# Work out resultant RGB
outRGB = (srcRGB*srcA[...,np.newaxis]   dstRGB*dstA[...,np.newaxis]*(1-srcA[...,np.newaxis])) / outA[...,np.newaxis]

I wanted to understand why that is done. Is not just multiplying by srcA and dstA, as is done in the numerator, sufficient? The extra division seems to change the factor for the blending operation. As an example, if srcA is 0.5 and dstA is also 0.5, there should be an equal contribution of 50% from both images. However, with the above, the src gets a weightage of 2/3 whereas dst gets 1/3.

P.S: Asking as a separate question here instead of a comment there since I'm new and do not have enough reputation points to post a question there.

CodePudding user response:

When blending the resulting outRGB with the background, using outA as alpha, one will multiply outRGB by outA. That is why we need to divide by outA.

Think of blending first B with the background, and then A on top (using A and B because it's more logical than src and dst in the linked code; the linked page also uses A and B). Let's call the background X, and Y is the result of mixing B on X. alpha_A is the alpha for color A, etc.:

Y = B * alpha_B   X * (1-alpha_B)
out = A * alpha_A   Y * (1-alpha_A)
    = A * alpha_A   (B * alpha_B   X * (1-alpha_B)) * (1-alpha_A)
    = A * alpha_A   B * alpha_B * (1-alpha_A)   X * (1-alpha_B) * (1-alpha_A)
    = A * alpha_A   B * alpha_B * (1-alpha_A)   X * (1 - (alpha_A   alphaB * (1-alpha_A)))

If we now separate out from the background again (let's call it color C), we must have:

out = C * alpha_C   X * (1-alpha_C)

So comparing the two equations:

out = A * alpha_A   B * alpha_B * (1-alpha_A)   X * (1 - (alpha_A   alphaB * (1-alpha_A)))
out = C * alpha_C   X * (1-alpha_C)

we must have:

alpha_C = alpha_A   alphaB * (1-alpha_A)
C * alpha_C = A * alpha_A   B * alpha_B * (1-alpha_A)

This latter one can be rewritten as:

C = (A * alpha_A   B * alpha_B * (1-alpha_A)) / alpha_C
  • Related