7

Alpha Blending

Chapter Outline

Alpha blending, or alpha compositing, is a video-processing technique that is used to combine two or more video streams on the same frame. This technique is increasingly used to overlay some graphics on a video, such as the 10-yard line in a football match or to put a different background behind a speaker.

We will learn how this is achieved and what kind of processing is required to overlay images.

7.1 Introduction

We will start with two images. Video consists of frames moving at a certain speed (30 fps or 60 fps) and we generally talk about processing within the frame, or spatial processing. When we process across frames it is called temporal processing. Alpha blending is an example of spatial processing.

Each frame is made of pixels, so when we alpha blend two frames we are blending two sets of pixels and coming up with a new pixel in the process. How we blend the two pixels depends on what we want to achieve.

We will start with a simple example: you want to overlay a logo on a frame and you want the logo to appear in the left bottom corner for each and every frame. The simple thing would be to identify all the pixels on the original image that would be overlain by the logo and replace those pixels with the pixels that constitute that logo.

This is simple in principal but hard to do across video frames that are zipping past at 60 fps. What if you did not want a simple overlay, but a translucent logo? Or what if in some video clips you didn’t want a logo? All of these options and more can be accommodated in real-time by a silicon architecture that can do fast alpha-blending.

7.2 Concept and Math Behind Alpha Blending

First the concept: to mix two frames of video we define a third frame of video called the alpha frame. This frame also has pixels that I will call alpha values. When pixels from frame one are flying through, and the frame that constitutes the logo appears, the hardware creates a composite pixel whose value is defined as:

Composite pixel = pixel from frame 1 × alpha + pixel from frame 2 × (1−alpha)

Think about this simple equation. The alpha frame can be defined as one in all the positions where there is no logo in frame two. And the alpha values are defined as zero in those places where the logo appears.

Composite pixel = pixel from frame one (when the alpha value is one, i.e. where there is no logo)

Composite pixel = pixel from frame two (when the alpha value is zero, i.e. where there is logo)

And that is the math behind alpha blending.

There are many advantages to defining the blending of video in this manner. For example, if you want the logo to be translucent, you define the alpha value as 0.5 wherever the logo appears. Therefore the value of the composite pixel in places where the logo appears becomes:

Composite pixel = pixel from frame 1 × 0.5 + pixel from frame 2 × 0.5

This could be described as half-and-half or, in other words, the logo is translucent.

To take another example, you may want to blend three frames of video. You define two alpha frames: the first alpha frame is used to blend frame one and frame two: the second alpha frame blends the newly created composite frame with frame three. And now you have a blended frame.

If you wish to remove the logo from the frames for the next five minutes, you would leave the hardware in place and just update the alpha frame for the next five minutes. That would wipe out the logo, and after five minutes the logo would come back.

Many effects can be achieved with this simple concept.

7.3 Implementing Alpha Blending in Hardware

Although it is simple in concept, implementing alpha blending in real-time requires fast, dedicated hardware. Let’s take a look at the equation above – written in a short form here:

Ci = p1i(α) + p2i(1−α)

This means you need two multipliers and one adder. As implementing a multiplier in hardware is expensive, you can rearrange your equation as shown in Figure 7.1 to minimize the multipliers.

image

Figure 7.1 The equations for alpha blending.

In Figure 7.1:

 C is the composite pixel value.

 F is the foreground pixel – we were referring to it as the pixel from frame one.

 B is the background pixel – we were referring to it as the pixel from frame two.

Figure 7.1 also illustrates that when we talk about creating a composite pixel, the calculations have to be done separately for each color component. We do the same calculation for red, green and blue. Which means not two multipliers, but six multipliers for one pixel…

A simple rearrangement of the equation as shown lets us use only one multiplier and two adders for each color component. Which means three multipliers for the pixel…

As adders are cheaper in silicon than multipliers this is the route we would choose. Figure 7.2 shows the hardware required to calculate the three color components of the composite pixel.

image

Figure 7.2 The hardware to implement the alpha blending equations.

7.4 Creating a Different Background

It is common to see a background, behind a newscaster or a weatherman for example, that is different to the actual physical background behind that person. As the person moves, the background adjusts perfectly. This effect uses alpha blending principles.

First the subject sits or stands in front of a background that is a different color (such as bright green) to the subject’s shirt, suit or face.

The background on which we wish to superimpose the subject is shot separately. Then all we have to do is alpha blend – with a slight change. Each pixel is compared to the green value. If the pixel is green, then by adjusting the alpha value the desired background pixel wins and replaces the green pixel. If the pixel is not green then we can safely assume that it is some portion of the subject’s anatomy, and it is left alone. The background pixel loses and the subject’s pixel wins.

By virtue of this approach the composite frame is adjusted so that the background changes around the subject smoothly, even in motion.

This is but one example of the very useful type of video processing that can be done using alpha blending.

7.5 Conclusion

Readers beginning with video processing must understand that the alpha blending function has uses that ranges from simple graphics overlay to creating special effects with multiple video streams. But in spite of how different these effects may look – the fact remains that what is required to be done is to generate a composite pixel – which is a function of two different pixels. In effect the underlying function – alpha blending – is the same.

Alpha blending, though mathematically a simple concept requires fast hardware since the processing has to be done at pixel rates. As we have seen earlier these pixel rates are very high especially for HD video. The applications of alpha-blending are many and with the advent of low-cost FPGAs that can handle HD pixel rates, this computationally intensive function can be provided in a cost-effective manner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset