Playing with Alpha Channels – part 2

In part 1, we learned that splitting a gray scale image into a background and a transparent foreground, we need to do is solve $T=B(1-\alpha )+F\alpha $ with variables T (target gray), B (background gray), F, (foreground gray) and the alpha transparency. All values need to be greater than or equal to zero and less than or equal to one. What I did was rerrange the equation to get the linear equation $F = \frac{{\alpha – 1}}{\alpha }B + T$ – a line with the slope $\frac{{\alpha – 1}}{\alpha }$, which will always be negative.

On the graphs below, the columns are selected alphas (.25, .5, .75) and the rows are target colors (.25, .5, .75). Each graph represents the possible values for the foreground gray (x-axis) and the background gray (y-axis) for the given $\alpha$ and target gray. Note again that all variables must be between 0 and 1, inclusive, so we ignore values outside that range. So, given a T and choosing an $\alpha$ we can pick a B and F.

$\alpha$=.25$\alpha$=.50$\alpha$=.75
.25
.50
.75

So, we need to solve for the endpoints of the line of $T=B(1-\alpha )+F\alpha $ for a given target color and alpha. I will refer to the lower right point as $(x_1,y_1)$ and the upper left point as $(x_2,y_2)$.

If $\frac{T}{{1 – \alpha }} > 1$ then $(x_1,y_1) = (1,\frac{{T + \alpha – 1}}{\alpha})$ else $(x_1,y_1) =(\frac{{T}}{{1 – \alpha }},0)$

If $\frac{T}{\alpha } > 1$ then $(x_2,y_2)=(\frac{{T – \alpha }}{{1 – \alpha }},1)$ else $(x_2,y_2)=(0,\frac{{T}}{\alpha })$

The x’s will give you the range of the foreground color values ($x_2<F<x_1)$ and the y’s will give you the range of possible background colors $(y_1<B<y_2)$. We can just choose F or B within range and that will determine the other. These equations are so important, I wrote a function to solve them rather than duplicate the code. It also allows me not to think about it and just get the points. Later, we’ll have to adjust the function if the target picture is already partially transparent.

Note: the closer we get to black or white, the smaller our possible options become. See the graphs below for a target color of 10%.

.25.50.75
.10

In fact, the only way to split a pure white pixel is a white background and fully transparent foreground, or an opaque white foreground pixel. Same for a pure black pixel. So if your goal is to obfuscate the image as in visual cryptography, the very dark and very light pixels will still form a pattern. What I’ll do is is set limits, like make all black pixels less than 16 to 16 (.0627$) and make pixels greater than 241 to 241 (.9451%).

So let’s split a picture of Tina Louise.

Drag the picture on the right over the left.

Not very exciting (the resulting images, not Tina), but kinda cool how the graininess disappears.

Since the image on the left has no transparency, we could send it through the program again so that all three images would be needed to create the original. Well, we can also split the image with transparencies, too. The equations for that are

$A = \alpha_f + \alpha_b(1 – \alpha_f)$   and   $AT = B(A – \alpha_f ) + F\alpha_f$

Where $A$ is the target alpha value, $\alpha_b$ is the background pixel alpha, and $\alpha_f$ is the foreground pixel alpha. And of course, $T$ is the target gray, $B$ will be the background gray, and $F$ will the the foreground gray pixel value.

The values of $\alpha_f$ and $\alpha_b$ will lie on a hyperbola with endpoints $(0,A)$ and $(A,0)$. The image on the left is for $A=0.9$

So, to solve for the endpoints, $(x_1,y_1)$ and $(x_2,y_2)$, of possible split grays, the equations become:

If $\frac{TA}{{A – \alpha_f}} > 1$ then $(x_1,y_1) = (1,\frac{{TA + \alpha_f – A}}{\alpha_f})$ else $(x_1,y_1) =(\frac{{TA}}{{A – \alpha_f }},0)$

If $\frac{TA}{\alpha_f } > 1$ then $(x_2,y_2)=(\frac{{TA – \alpha_f }}{{A – \alpha _f}},1)$ else $(x_2,y_2)=(0,\frac{{TA}}{\alpha_f})$

Note that if $A=1$, the equations reduce to the original. I split a Tina into six parts. Clicking an image will tell you in what order it should be placed on the upper left image (One first, to five). Download images for next block.

Alpha channel addition is not commutative, so only one order will be closest to the original. Due to round off error, there will be slight differences even when the correct order is used (if you really zoom in on the picture).

So now, lets split the gray pixels $(1,T,T,T)$ into a background $(1,R_1,G_1,B_1)$ and a foreground $(\alpha,R_2,0,0)$. Basically, we’re splitting the red channel into a foreground and background. This gives us two equations to satisfy (three with two being the same):

$T=R_1(1-\alpha)+R_2\alpha$      $T=B_1(1-\alpha)$    $T=G_1(1-\alpha)$

Obviously, $G_1=B_1$ and the constraint on alpha is $\alpha < 1-T$ so we solve the first equation as before then determine $G$ and $B$. The equations for splitting off blue and green channels will be similar, just move around the variables.

Similarly, we can slice off yellow – $(1,T,T,T)$ into a $(1,R_1,G_1,B_1)$ background and $(\alpha,R_2,G_2,0)$ with

$T=R_1(1-\alpha)+R_2\alpha$      $T=G_1(1-\alpha)+G_2\alpha$      $T=B_1(1-\alpha)$

For now, we can solve this by making $R_1=G_1$ and $R_2=G_2$ even though other values are possible. Download the pictures. for the following demonstration. Arrange the pictures to make perfect grayscale images of Tina.

Now, with all this math under my belt (Yes, that’s MATH over my rock hard abs. IT IS! I have a LOT of math under my belt), I can tackle splitting color images. That will be in part 3.

Notes: Use the highest precision on your variables. The round off error on floats can cause problems; doubles work better. Check for zeros in your methods that cause infinities; setting them to a minimum of .001 solved a lot of my issues.

I’m telling you that’s MATH!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.