Substracting colors and extracting transparency values

0

Lets say a picture was made to semi-transparent and added on a flat background color. The original picture, background color and result(mixture) is known. But, that added transparency(alpha value) is unknown and is varying along the picture. Is there an automated way to calculate this added transparency value for each pixel of image? Can we copy these values and apply to another picture?

[ Picture + Transparency(unknown and varies) ] + FlatBackground = Mix(opaque)

= [ Semi-tranparent Picture ] + FlatBackground = Mix(opaque)

An example of copying each added transparency value corresponding to pixels of picture:

for pixel(0,0) added 127
for pixel(1,0) added 124
for pixel(0,1) added 124
for pixel(1,1) added 120

But ofcourse I don't want to print these values. I just want to use it like mask(?). So that, i can apply these values to another picture to create same effect.

destor

Posted 2015-05-02T09:03:53.867

Reputation: 183

Would something like "color to alpha" help? I.e. a background color is interpreted to be fully transparent, and colors that are close to it are interpreted to be semi-transparent? – George – 2015-05-02T13:59:52.380

Answers

0

Transparency is multiplied by, not added to, the original pixel to obtain the mixed pixel. See the discussion in the PNG Specification.

Assuming 8-bit color samples that range from 0..255 and normalizing to range 0..1.0, do this for each pixel in the image:

m = Mix/255
p = Picture/255
a = alpha = Transparency/255

m = (p * a) + (b * (1.0 - a))

Solve for "a"

a = (m - b) / (p - b)

Convert back to original range, e.g., 0..255:

Transparency = a * 255

Unfortunately this means that you cannot extract Transparency (alpha) from all pixels. In particular, when Picture == Background (p - b == 0) , then Transparency could be anything in the range 0..255 (a == anything from 0.0 to 1.0); if you use this formula in a code you'd need to protect against divide-by-zero.

When Picture-Background is non-zero, then you can obtain Transparency, but it's not very accurate when the difference (p - b) is small.

Glenn Randers-Pehrson

Posted 2015-05-02T09:03:53.867

Reputation: 477