New User?   Join Now     Login            Forgot Password?    
Browse by Category
Browse Projects
XamlQuery v1.2
Browse Topics
Motion Detection Algorithm  (21305 hits)
 Share this Article
Posted by Prabu Arumugam on Apr-09-2013
Languages: C#, Silverlight

This article explains a simple motion-detection algorithm with full source code in C# and Silverlight. The algorithm is implemented by reading and manipulating the images pixel-by-pixel (no third party libraries are used). This is an ideal example for programmers who begin morphological image processing algorithms.

Motion-Detection Demo in Silverlight

The following demo illustrates the output of the motion-detection algorithm when applied on two frames of a video. You can adjust the input parameters and see the output visually in the demo. The source code of this demo is available for download below.

What is Motion-Detection?

Motion-detection is the process of detecting moving objects (particularly people) from a captured or live video. Motion-detection has many applications like security, surveillance, automated lighting control, etc. A video comprises a set of continuous frames (images). There are many approaches for detecting motions in a video; most of them are based on comparing a frame of the video with the next-frame or previous-frame or the background.

Motion-Detection Steps

Motion-detection is a two-step process.

Step1 identifies the objects that have moved between the two frames (using difference and threshold filter). The difference between each corresponding pixel of two frames is calculated; and the pixels with difference greater than the specified threshold are marked in foreground color (white). All the other pixels are marked in background color (black). So, the output of Step1 will be a binary-image with only two colors (black and white).

The intensity value (brightness level) of the pixels are used to calculate the difference. The intensity value of each pixel in a grayscale image will be in the range 0 to 255. If RGB images are used, then the grayscale intensity value can be calculated as (R + G + B / 3).

Step2 identifies the significant movements and filters out the noise that are wrongly identified as motions (using erosion-filter). Erosion-filter removes the pixels that are not surrounded by enough amount of neighboring pixels. Essentially, this gives the ability to shrink the objects thereby removing the noisy pixels i.e. stand-alone pixels. The binary image (output of Step1) is scanned pixel-by-pixel; and if enough number of pixels in current window are not turned on, then the entire window is turned off i.e. ignored as noise.

The following code implements the complete motion-detection algorithm for two given frames. A frame/image is represented in PGM (portable gray map) format, which is a simple grayscale image format which is explained in detail in this article.

public const byte Background = 0;
public const byte Foreground = 255;

public static List<PGM> Detect(PGM frame1, PGM frame2, int differenceThreshold, int noiseFilterSize)
    int width = (int)frame1.Size.Width;
    int height = (int)frame1.Size.Height;

    //first-pass: difference and threshold filter (mark the pixels that are changed between two frames)
    PGM step1Image = new PGM(width, height);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            int diff = Math.Abs(frame1.GetPixel(x, y) - frame2.GetPixel(x, y));
            step1Image.SetPixel(x, y, diff >= differenceThreshold ? Foreground : Background);

    //second-pass: erosion filter (remove noise i.e. ignore minor differences between two frames)
    PGM step2Image = new PGM(width, height);
    int m = noiseFilterSize, n = (m - 1) / 2; //'m' will be an odd number always
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            //count the number of marked pixels in current window
            int marked = 0;
            for (int i = x - n; i < x + n; i++)
                for (int j = y - n; j < y + n; j++)
                    marked += step1Image.GetPixel(i, j) == Foreground ? 1 : 0;

            if (marked >= m) //if atleast half the number of pixels are marked, then mark the full window
                for (int i = x - n; i < x + n; i++)
                    for (int j = y - n; j < y + n; j++)
                        step2Image.SetPixel(i, j, Foreground);

    return new List<PGM>() { step1Image, step2Image };

Motion-Detection in Video

The above steps can be applied to a series of consecutive frames extracted from a captured video or live streaming video. Some interesting features (like silhouette) can be extracted from the output-image of Step2, and the features can be used to identify a person using methods like template-matching.

The methods explained in this article can be implemented in biometric applications, especially identifying humans using gait. Gait refers to the walking-style of an individual. Gait based recognition of humans is particularly useful in surveillance applications in public places like airport, where it is difficult to get useful information (like face or iris) at required resolution without willful intervention of the persons. Given the video of an unknown individual, the features extracted can be used as a cue to find who among the set of individuals in the database the person is.

 Downloads for this article
File Language Tools  1.56 mb  (374 downloads) C#, Silverlight 4 Visual Studio 2010

 Share this Article

 Comments on this Article
Comment by Aiden on Jul-04-2015
Comment by jannette on May-02-2014
slt , svp comment je peux lancer une enregistrement vidéo suivi d'une détection de mouvement en utilisant langage C# et la bibliothèque EmguCV ??
Comment by spiffyladd on Mar-05-2014
Comment by Sheraz on Dec-17-2013
I am a student and want to learn some good subject of image processing Thanks\ Sheraz
Comment by afanullah on Jul-04-2013
 Post your comment here
Your Name
Your Email
Post Comment

About About      Terms Terms      Contact Contact
codeding on Facebook Facebook      codeding on Twitter Twitter