Click here to Skip to main content
15,880,503 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.4K   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 to save video in this article Pin
Member 1267153127-Sep-16 6:58
Member 1267153127-Sep-16 6:58 
Questionsaving in video form Pin
Member 126099014-Aug-16 23:15
Member 126099014-Aug-16 23:15 
GeneralMy vote of 3 Pin
Игорь Жилин30-Nov-14 12:56
Игорь Жилин30-Nov-14 12:56 
GeneralMy vote of 4 Pin
Member 1094452113-Jul-14 23:57
Member 1094452113-Jul-14 23:57 
QuestionUrgent!!! Pin
Member 858515426-Feb-12 18:39
Member 858515426-Feb-12 18:39 
GeneralTUNER Pin
nithinravi53526-Dec-10 22:55
nithinravi53526-Dec-10 22:55 
GeneralDear Mr. Taha Amin Pin
uvik30-Sep-10 11:33
uvik30-Sep-10 11:33 
GeneralRe: Dear Mr. Taha Amin Pin
Taha Elsayed22-Feb-24 9:16
Taha Elsayed22-Feb-24 9:16 
QuestionIn which assembly i found camserver.dll & ijl15.dll ? Pin
Saira137-Apr-10 17:28
Saira137-Apr-10 17:28 
Questionmotion detection using web cam in C# Pin
Saira137-Apr-10 4:34
Saira137-Apr-10 4:34 
Generalmotion detection code with MATLAB Pin
rasul631420-Mar-10 11:02
rasul631420-Mar-10 11:02 
GeneralRe: motion detection code with MATLAB Pin
trzik292-May-11 23:36
trzik292-May-11 23:36 
GeneralRe: motion detection code with MATLAB Pin
Guy Tech8-May-23 19:28
Guy Tech8-May-23 19:28 
Generalhelp Me! Pin
brysamae26-Jan-10 20:02
brysamae26-Jan-10 20:02 
GeneralRe: help Me! Pin
uvik30-Sep-10 22:54
uvik30-Sep-10 22:54 
GeneralDetect movement from left or from right Pin
ALEXNG8828-Dec-09 2:21
ALEXNG8828-Dec-09 2:21 
GeneralThanks Pin
Du Sijun15-Dec-09 2:02
Du Sijun15-Dec-09 2:02 
GeneralI need a help! Pin
thurein0916-Jul-09 3:08
thurein0916-Jul-09 3:08 
Questionneeds some advice for my FYP "Golf Swing Analysis Software" Pin
Oak Soe26-Jun-09 5:45
Oak Soe26-Jun-09 5:45 
QuestionRetrieving the COM class factory for component with CLSID {FDD4C8C1-69C9-11D3-9DC8-525400E38CF9} failed due to the following error: 80040154 Pin
alish06105-Apr-09 20:02
alish06105-Apr-09 20:02 
QuestionHelp me Pin
findingbooks1-Apr-09 21:41
findingbooks1-Apr-09 21:41 
Generalnice article Pin
arunvikc21-Feb-09 21:51
arunvikc21-Feb-09 21:51 
GeneralRetrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154. Pin
Omsk28-Dec-08 23:39
Omsk28-Dec-08 23:39 
QuestionRotating of WebCam Pin
Yena papa16-Sep-08 4:04
Yena papa16-Sep-08 4:04 
QuestionHow to get the Video Stream From CF Camera Pin
vison_xuan14-Apr-08 6:22
vison_xuan14-Apr-08 6:22 

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.