Video Alpha Blending

Toolkits.VideoAlphaBlending History

Hide minor edits - Show changes to output

July 17, 2007, at 07:45 PM by 24.64.76.194 -
Changed lines 55-56 from:
to:
private delegate void SetPictureBoxImage(PictureBox pbox, Bitmap image);
Changed line 86 from:
this.pbImage.Image = BlendedImage;
to:
DisplayImageInPictureBox (this.pbImage, BlendedImage);
Added lines 88-101:

//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;
}
}
June 23, 2007, at 01:47 PM by 24.64.76.194 -
Deleted lines 38-40:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Deleted line 39:
using System.Text;
Changed lines 41-48 from:
to:
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.
Changed lines 51-56 from:
public partial class Form1 : Form
to:
public partial class Form1 : Form
{
EasyImages.CameraClient camera;
Bitmap BlendedImage; // keep a copy of the last frame

public Form1()
Changed lines 58-63 from:
EasyImages.Camera camera;
Bitmap BlendedImage; // keep a copy of the last frame

public Form1()
{
InitializeComponent();
to:
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
);
Changed lines 84-112 from:

//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.Camera();
camera.FramesPerSecond = 15;
camera.Width = 640;
camera.Height = 480;
camera.ReceivedFrame += new EasyImages.Camera.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;
this.pbImage.Image = BlendedImage;
}
to:
BlendedImage = Temp;
this.pbImage.Image = BlendedImage;
Added line 87:
}
June 10, 2007, at 05:27 PM by 24.64.76.194 -
Changed line 1 from:
%ralign% Attach:VideoAlphaBlending.png
to:
%rfloat% Attach:VideoAlphaBlending.png
June 10, 2007, at 05:26 PM by 24.64.76.194 -
Added line 1:
%ralign% Attach:VideoAlphaBlending.png
Changed lines 7-8 from:
Attach:VideoAlphaBlending.png
to:
June 10, 2007, at 05:25 PM by 24.64.76.194 -
Deleted line 0:
Changed lines 44-49 from:
// Author: Mark Watson, modified by Saul Greenberg
// University of Calgary
// (c) June 7, 2007.
// License: See License details included in this distribution. 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.
to:
June 10, 2007, at 05:24 PM by 24.64.76.194 -
Added lines 1-98:

[[Toolkits.EasyImage | << Back to the EasyImage Toolkit page]]
\\
\\

!!!Tutorial - Alpha Blending a Video stream
Attach:VideoAlphaBlending.png

!!!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.
(:table border=1 cellpadding=17 cellspacing=0 align=entre bgcolor=#eeeeee :)
(:cell:) [+ '''Source and Executables of this and other examples: ''' [[(Attach:)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.
(:tableend:)

!!!Step 1. Preconditions
Including EasyImages in your new Visual Studio 2005 C# project. This was described in [[Toolkits.CameraMinimal | 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.
(:source lang=csharp tabwidth=1 :) [=
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
// Author: Mark Watson, modified by Saul Greenberg
// University of Calgary
// (c) June 7, 2007.
// License: See License details included in this distribution. 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.Camera camera;
Bitmap BlendedImage; // keep a copy of the last frame

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.Camera();
camera.FramesPerSecond = 15;
camera.Width = 640;
camera.Height = 480;
camera.ReceivedFrame += new EasyImages.Camera.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;
this.pbImage.Image = BlendedImage;
}
}
}
=]

!!!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.