Click here to Skip to main content
15,889,266 members
Articles / Multimedia / DirectX

Motion detection using a webcam

Rate me:
Please Sign up or sign in to vote.
4.05/5 (57 votes)
9 Jun 2004CPOL3 min read 567.6K   45.8K   294   96
Motion detection using webcam, by C#.

Sample Image - md01.jpg

Introduction

This text tries to clear out the mystery of how to make motion detection using a webcam in C#. My friend Walied wants to capture video in his office with a webcam for 24 hours, but the problem is that the space required on a hard disk is very extreme. So, he asked me to find a way to save disk space. Walied proposed to make a motion-detection circuit (IR, ultrasonic) and connect it to the PC serial port and check for input every period of time. If there is any input, then run the record program and start to capture video. But I replied that it’s a bad way because the thief will run away before the slow computer requests a webcam to start capturing video.

So my idea was to take a picture from a webcam every period of time (make it the current picture) and compare it with a previous picture, and if we find a big difference between them, we will save both pictures; otherwise, we will free memory from the old picture and make the new picture the current picture.

But there are two big problems:

  1. How do you communicate with your webcam and grab a frame (picture) from it?
  2. How do I compare the two pictures?

Solution

  1. There are two ways to talk to a web cam:
    1. Use DirectX's component called DirectShow, (but unfortunately, DirectX doesn't provide DirectShow component for C#. NETMaster made an assembly to face this problem in his article, DirectShow.NET). Elgitaro: check this.
    2. Use a 3rd party cam server (you can find it in Laurent Kempé 's article Dynamic Webcam Image).
  2. There are also two ways to compare 2 pictures:
    1. By comparing each pixel's color for both pictures.
    2. Using filters to detect edges, then object recognition...

    I think method (a) will work faster because we are talking about a period of 3 msec.

Try

This is my algorithm:

algorithm-Drawing1.jpg

First, grab an image from a webcam called Old. Second, grab another image from a webcam after a while, called Cur. Compare Cur & Old by comparing each pixel color. If the difference is greater than the tested value (according to the quality of the webcam & light system you are using at 60 or 50 Hz), then save the two pictures. Finally, make the old picture the Cur picture. Back to the second step (infinite loop).

Let's write some code:

  • Get the camserver.dll & ijl15.dll (copy both files to c:\windows\system32 and then write regsvr32 camserver.dll in Start->Run to register the COM component).
  • Make a new Windows project, paste three PictureBoxes in your form, paste two Buttons and finally, paste a Timer control and set its enabled property to false.

Your form looks something like this:

motion_detection_wc/catchit.jpg

Catch

This program will not work as fast as I need because of the time for IO operations. So I use another technique to make it fast, by capturing a video on a panel and then grabbing an image from it. This makes the application work as fast as I need. This application is called CatchItV, and the first one is called CatchIt (please try both and send me your comments).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow many percent changes in the video will consider motion? Pin
tansc25-Feb-08 21:46
tansc25-Feb-08 21:46 
QuestionHow to capture avi file? [modified] Pin
Hady CV18-Nov-07 3:46
Hady CV18-Nov-07 3:46 
AnswerRe: How to capture avi file? Pin
Taha Elsayed18-Nov-07 4:14
Taha Elsayed18-Nov-07 4:14 
QuestionRe: How to capture avi file? Pin
Hady CV18-Nov-07 4:39
Hady CV18-Nov-07 4:39 
AnswerRe: How to capture avi file? Pin
Taha Elsayed18-Nov-07 4:52
Taha Elsayed18-Nov-07 4:52 
GeneralRe: How to capture avi file? Pin
Hady CV18-Nov-07 5:06
Hady CV18-Nov-07 5:06 
GeneralRe: How to capture avi file? Pin
lakisoft28-Nov-07 10:53
lakisoft28-Nov-07 10:53 
GeneralRe: How to capture avi file? Pin
Taha Elsayed28-Nov-07 11:12
Taha Elsayed28-Nov-07 11:12 
AnswerRe: How to capture avi file? Pin
kiquenet.com8-May-08 7:40
professionalkiquenet.com8-May-08 7:40 
QuestionMore than one device? Pin
chmod222227-Jul-07 11:23
chmod222227-Jul-07 11:23 
AnswerRe: More than one device? Pin
snort23-Sep-07 3:05
snort23-Sep-07 3:05 
QuestionThanks & Problems Pin
Ali.648527-Jul-07 4:04
Ali.648527-Jul-07 4:04 
AnswerRe: Thanks & Problems Pin
chmod222227-Jul-07 13:08
chmod222227-Jul-07 13:08 
GeneralRe: Thanks & Problems Pin
Ali.648528-Jul-07 22:14
Ali.648528-Jul-07 22:14 
GeneralRe: Thanks & Problems Pin
JoanComasFdz14-Jun-09 21:54
JoanComasFdz14-Jun-09 21:54 
Generalvery good work Pin
abdelhameedm23-Jul-07 5:19
abdelhameedm23-Jul-07 5:19 
GeneralSimple question about lblTimerValue Pin
Norshuhada Samudin24-Jun-07 1:30
Norshuhada Samudin24-Jun-07 1:30 
GeneralHi , i need your help Pin
Exelioindia12-Jun-07 3:56
Exelioindia12-Jun-07 3:56 
QuestionI need the help for My graduation project Pin
Ali Habib10-May-07 10:54
Ali Habib10-May-07 10:54 
Questionwhere and how? Pin
itce200523-Apr-07 10:48
itce200523-Apr-07 10:48 
AnswerRe: where and how? Pin
S.S.Cheral26-Oct-07 1:32
S.S.Cheral26-Oct-07 1:32 
QuestionError Handling Pin
Abu Syed Khan9-Mar-07 7:08
Abu Syed Khan9-Mar-07 7:08 
QuestionCan i get the code for doing it in matlab Pin
balaji rao8513-Jan-07 16:23
balaji rao8513-Jan-07 16:23 
AnswerRe: Can i get the code for doing it in matlab Pin
Member 421587326-Sep-08 1:53
Member 421587326-Sep-08 1:53 
GeneralVideo SHOW on C# Pin
CyBeRiaMiaM12-Jan-07 4:51
CyBeRiaMiaM12-Jan-07 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.