Click here to Skip to main content
15,881,812 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 566.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

 
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 
it will store wanted folder inside release if your catchItV_scr stored c drive then the images are stored in "C:\CatchItV_scr\bin\Release\wanted"Smile | :)

Cheral

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 
GeneralRe: Video SHOW on C# Pin
prat938-Feb-07 0:48
prat938-Feb-07 0:48 
GeneralRe: Video SHOW on C# Pin
CyBeRiaMiaM10-Feb-07 23:41
CyBeRiaMiaM10-Feb-07 23:41 
GeneralRe: Video SHOW on C# Pin
TheCreeep21-Feb-07 7:04
TheCreeep21-Feb-07 7:04 
Generalis it free Pin
yamanalp9-Jan-07 23:10
yamanalp9-Jan-07 23:10 
QuestionYou email is not availble! Pin
beyondjustin23-Dec-06 0:50
beyondjustin23-Dec-06 0:50 
GeneralHELP !!!! Motion Detection to Substitutue Mouse Action Pin
hugomsousa8-Oct-06 1:46
hugomsousa8-Oct-06 1:46 
GeneralRe: HELP !!!! Motion Detection to Substitutue Mouse Action Pin
hugomsousa8-Oct-06 1:47
hugomsousa8-Oct-06 1:47 
GeneralRe: HELP !!!! Motion Detection to Substitutue Mouse Action Pin
TheCreeep21-Feb-07 7:06
TheCreeep21-Feb-07 7:06 
GeneralCapture speed Pin
Eruantalon31-Jul-06 22:25
Eruantalon31-Jul-06 22:25 
Questionhow to kill the detection process in ASP Pin
frankjonathan8124-Apr-06 13:57
frankjonathan8124-Apr-06 13:57 
Questionextraction of frames Pin
almanac.pes23-Mar-06 23:49
almanac.pes23-Mar-06 23:49 
QuestionPicture Quality Pin
mnlux15-Mar-06 9:24
mnlux15-Mar-06 9:24 
GeneralWebcam detection in webpage Pin
poornachander kola13-Mar-06 20:33
poornachander kola13-Mar-06 20:33 

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.