From iLab Cookbook

Toolkits: ImageDifferencing

<< Back to the EasyImage Toolkit page

Tutorial - Image Differencing

What you will learn

This tutorial shows:

In the above picture,

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.

Step 3. Writing the program

The complete program is listed below.

using System;
using System.Drawing;
using System.Windows.Forms;
using EasyImages;

namespace ImageDifferencing
{
  public partial class Form1 : Form
  {
    EasyImages.CameraClient camera;
    Bitmap lastFrame;
    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)
    {
      lblThreshold.Text = "Threshold: " + tbThreshold.Value.ToString();

      //Create a new camera object and set its initial properties
      camera = new EasyImages.CameraClient ("DefaultCamera");
      camera.FramesPerSecond = 10;
      camera.ReceivedFrame += new EasyImages.CameraClient.ReceivedFrameEventHandler (camera_ReceivedFrame);
      camera.Start();
    }

    //We just received a frame...
    void camera_ReceivedFrame(object sender, EasyImages.CameraEventArgs e)
    {
      // Display the original frame
      this.DisplayImageInPictureBox(pbOriginal, e.Bitmap);

      // Generate a frame that visually shows the one image subtracted from the other
      // We first decide (by the state of the radio buttons) if we are going to subtract
      // the current frame from the last frame seen, or from a static snapshot
      Bitmap tempBitmap;
      if (rbSnapshot.Checked) tempBitmap = (Bitmap) pbSnapshot.Image;
      else tempBitmap = lastFrame;

      if (tempBitmap != null)
      {
        // Now we subtract the image
        this.DisplayImageInPictureBox(pbSubtracted, EasyImages.ImageGenerator.Operation(EasyImages.OperationType.Subtract, e.Bitmap, tempBitmap));
        //Calculate and show the degree differences between images
        lblDiffDegree.Text = "Degree: " + EasyImages.ImageAnalyzer.PercentDifferentDegree(e.Bitmap, tempBitmap).ToString();
        //Calculate and show the number of pixels that differ by at least the threshold amount set in the trackbarr
        lblDiffPixels.Text = "Pixels:     " + EasyImages.ImageAnalyzer.PercentDifferentPixels(tbThreshold.Value, e.Bitmap, tempBitmap).ToString();
      }
      //Save this frame, so we can use it on the next go round.
      lastFrame = e.Bitmap;
    }

    // Interactively set the threshold for calculating the PercentDifferencePixels
    private void tbThreshold_Scroll(object sender, EventArgs e)
    {
      lblThreshold.Text = "Threshold: " + tbThreshold.Value.ToString();
    }

    // When we click the snapshot radio button, take a snapshot.
    private void rbSnapshot_CheckedChanged (object sender, EventArgs e)
    {
      if (rbSnapshot.Checked)
      {
        this.DisplayImageInPictureBox(pbSnapshot, camera.GetFrame ());
      }
    }

    // Display an image in a picture box, making sure we are in the correcct 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, so we only describe what is different. The new parts illustrate how to display an image that is the result of subtracting two images, and how to analyze its difference (returned as a fraction).

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/ImageDifferencing
Page last modified on July 17, 2007, at 07:33 PM