Video Alpha Blending

<< Back to the EasyImage Toolkit page

Tutorial - Alpha Blending a Video stream

What you will learn

This tutorial shows:

  • how you can alpha-blend a stream of frames atop each other to give an interesting 'motion blur' effect.

In the above picture,

  • the image is the result of successively alpha blending several video frames together of a person moving in the scene

Download

While we recommend you create this program from scratch, you can also download the source and executables.

Source and Executables of this and other examples: EasyImagesExamples.zip
  • Unzip all files into a single folder.
  • Try the executable; a shortcut is in the top-level directory, or look in the debug directory
  • You will need a web camera attached to your computer.

Step 1. Preconditions

Including EasyImages in your new Visual Studio 2005 C# project. This was described in Minimal Camera Example. Make sure to include the using EasyImages; line in your project.

Step 2. Creating GUI controls

Add the following GUI controls to your form window. Thei locations and purpose should be obvious if you check the image at the top of this page.

  • 1 PictureBox ''will display the alpha blended image
    • Name = pbImage
    • Size = 640,480 (the Width and Height of the frame we will put in it)

Step 3. Writing the program

The complete program is listed below.

using System;
using System.Drawing;
using System.Windows.Forms;
using EasyImages;
// Author: Mark Watson, modified by Saul Greenberg, University of Calgary, http://www.cpsc.ucalgary.ca/~saul 
// Copyright: (c) June 7, 2007.
// Documentation: see http://grouplab.cpsc.ucalgary.ca/cookbook/ and select EasyImages.
// The API Documentation and a tutorial explaining this and other programs are available there
// License: See License details included in the distribution package. Essentialy, permission to
// use and/or alter this program for non-commerical and/or educational purposes is granted,
// as long as attribution to the above author is maintained.
namespace VideoAlphaBlending
{
  public partial class Form1 : Form
  {
    EasyImages.CameraClient camera;
    Bitmap BlendedImage; // keep a copy of the last frame
    private delegate void SetPictureBoxImage(PictureBox pbox, Bitmap image);

    public Form1()
    {
      InitializeComponent();
    }

    //As the GUI is loaded, create a new camera object and set its initial properties
    private void Form1_Load(object sender, EventArgs e)
    {
      camera = new EasyImages.CameraClient("DefaultCamera");
      camera.FramesPerSecond = 10;
      camera.ReceivedFrame += new EasyImages.CameraClient.ReceivedFrameEventHandler (camera_ReceivedFrame);
      camera.Start();
    }

    // When we receive a frame, alpha blend it with the last f
    void camera_ReceivedFrame(object sender, EasyImages.CameraEventArgs e)
    {
      Bitmap Temp;
      // If we don't have a blended image (i.e., if its the first frame),
      // then just make it the video frame
      if (BlendedImage == null) {
        Temp = e.Bitmap.Clone() as Bitmap;
      } else {
        // First, create a copy of the current frame
         Temp = e.Bitmap.Clone() as Bitmap;
        // Then blend it with the previous image
        EasyImages.ImageManipulator.AlphaBlend(ref Temp, BlendedImage, (float).15);
      }
      BlendedImage = Temp;
      DisplayImageInPictureBox (this.pbImage, BlendedImage);
    }

    //Display the image in the picture box in the correct thread
    private void DisplayImageInPictureBox(PictureBox pbox, Image image)
    {
      if (pbox.InvokeRequired) // We are in the wrong thread. Call ourselves in the correct thread
      {
        SetPictureBoxImage theDelegate = new SetPictureBoxImage(DisplayImageInPictureBox);
        BeginInvoke(theDelegate, new object[] { pbox, image });
      }
      else // we are in the correct thread, so assign the image
      {
        pbox.Image = image;
      }
    }
  }
}

Explanation

Much of this program is similar to what was seen in the Minimal Camera, and the previous ImageAlphaBlending example, so we only describe what is different.

We create a base image (called BlendedImage), which is the very first video frame we see. Whenever we get a new frame, we alpha blend it onto this image and then that becomes the BlendedImage. The result is that we are continuously alphablending video frames atop each other, which gives a motion blur effect. Old 'images' eventually disappear as they become overwritten by new alpha-blended images.