Click here to Skip to main content
11,500,576 members (72,837 online)
Click here to Skip to main content

Motion detection using web cam

, 9 Jun 2004 434.5K 38K 289
Rate this:
Please Sign up or sign in to vote.
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 web cam with C#. My friend Walied want to capture video to his office by a web cam for 24 hrs, but the problem is that the space required on hard-disk is very extreme. So, he asked me to make a way to save disk space. Walied proposed to make a motion-detection circuit (IR, Ultra sonic) 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 web cam to start capturing video.

So my idea was to take a picture from a web cam 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 else will free memory from the old picture and make the new picture the current picture.

But there are two big problems:

  1. How to talk with your web cam and grab frame (picture) from it?
  2. How to 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 3RDPARTY like 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 m secs.

Try

This is my algorithm:

algorithm-Drawing1.jpg

First, grab image from web cam called Old. Second, grab another image from web cam after a while, called Cur. Compare Cur & Old by comparing each pixel color. If difference is greater than tested value (according to the quality of your web cam & light system you are using is 60 or 50 Hz), then save the two pictures. Finally, make the old picture the Cur picture. Back to 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 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:

catchit.jpg

Catch

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Taha Amin
Architect http://www.linkdev.com/
Egypt Egypt
Senior Development Manager, with 11 years of experience. Focused on design/developing/delivery B/OSS modules to satisfy customer/business requirements in accordance to TM Forum's TAM/SID framework. Excellent communication and interpersonal skills, flexibility, ability to adjust to new people and situations, strong problem solving skills and willing to work effectively and efficiently. Gained additional leadership skills such as: - High effective mentorship and coaching - Clearly communication and solutions sharing with my team - Decision making
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralMy vote of 3 Pin
Игорь Жилин30-Nov-14 12:56
memberИгорь Жилин30-Nov-14 12:56 
GeneralMy vote of 4 Pin
Member 1094452113-Jul-14 23:57
memberMember 1094452113-Jul-14 23:57 
QuestionUrgent!!! Pin
Member 858515426-Feb-12 18:39
memberMember 858515426-Feb-12 18:39 
GeneralTUNER Pin
nithinravi53526-Dec-10 22:55
groupnithinravi53526-Dec-10 22:55 
GeneralDear Mr. Taha Amin Pin
uvik30-Sep-10 11:33
memberuvik30-Sep-10 11:33 
QuestionIn which assembly i found camserver.dll & ijl15.dll ? Pin
Saira137-Apr-10 17:28
memberSaira137-Apr-10 17:28 
Questionmotion detection using web cam in C# Pin
Saira137-Apr-10 4:34
memberSaira137-Apr-10 4:34 
Generalmotion detection code with MATLAB Pin
asalrasul20-Mar-10 11:02
memberasalrasul20-Mar-10 11:02 
GeneralRe: motion detection code with MATLAB Pin
trzik292-May-11 23:36
membertrzik292-May-11 23:36 
Generalhelp Me! Pin
brysamae26-Jan-10 20:02
memberbrysamae26-Jan-10 20:02 
GeneralRe: help Me! Pin
uvik30-Sep-10 22:54
memberuvik30-Sep-10 22:54 
GeneralDetect movement from left or from right Pin
ALEXNG8828-Dec-09 2:21
memberALEXNG8828-Dec-09 2:21 
GeneralThanks Pin
startstarts15-Dec-09 2:02
memberstartstarts15-Dec-09 2:02 
GeneralI need a help! Pin
thurein0916-Jul-09 3:08
memberthurein0916-Jul-09 3:08 
Questionneeds some advice for my FYP "Golf Swing Analysis Software" Pin
Oak Soe26-Jun-09 5:45
memberOak 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
memberalish06105-Apr-09 20:02 
QuestionHelp me Pin
findingbooks1-Apr-09 21:41
memberfindingbooks1-Apr-09 21:41 
Generalnice article Pin
arunvikc21-Feb-09 21:51
memberarunvikc21-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
memberOmsk28-Dec-08 23:39 
QuestionRotating of WebCam Pin
Yena papa16-Sep-08 4:04
memberYena papa16-Sep-08 4:04 
QuestionHow to get the Video Stream From CF Camera Pin
vison_xuan14-Apr-08 6:22
membervison_xuan14-Apr-08 6:22 
Questionhow many percent changes in the video will consider motion? Pin
tansc25-Feb-08 21:46
membertansc25-Feb-08 21:46 
QuestionHow to capture avi file? [modified] Pin
Hady CV18-Nov-07 3:46
memberHady CV18-Nov-07 3:46 
AnswerRe: How to capture avi file? Pin
Taha Zayed18-Nov-07 4:14
memberTaha Zayed18-Nov-07 4:14 
QuestionRe: How to capture avi file? Pin
Hady CV18-Nov-07 4:39
memberHady CV18-Nov-07 4:39 
AnswerRe: How to capture avi file? Pin
Taha Zayed18-Nov-07 4:52
memberTaha Zayed18-Nov-07 4:52 
GeneralRe: How to capture avi file? Pin
Hady CV18-Nov-07 5:06
memberHady CV18-Nov-07 5:06 
GeneralRe: How to capture avi file? Pin
lakisoft28-Nov-07 10:53
memberlakisoft28-Nov-07 10:53 
GeneralRe: How to capture avi file? Pin
Taha Zayed28-Nov-07 11:12
memberTaha Zayed28-Nov-07 11:12 
AnswerRe: How to capture avi file? Pin
alhambra-eidos8-May-08 7:40
memberalhambra-eidos8-May-08 7:40 
QuestionMore than one device? Pin
chmod222227-Jul-07 11:23
memberchmod222227-Jul-07 11:23 
AnswerRe: More than one device? Pin
snort23-Sep-07 3:05
membersnort23-Sep-07 3:05 
QuestionThanks & Problems Pin
Ali.648527-Jul-07 4:04
memberAli.648527-Jul-07 4:04 
AnswerRe: Thanks & Problems Pin
chmod222227-Jul-07 13:08
memberchmod222227-Jul-07 13:08 
GeneralRe: Thanks & Problems Pin
Ali.648528-Jul-07 22:14
memberAli.648528-Jul-07 22:14 
GeneralRe: Thanks & Problems Pin
k^s14-Jun-09 21:54
memberk^s14-Jun-09 21:54 
Generalvery good work Pin
Abdel-Hameed23-Jul-07 5:19
memberAbdel-Hameed23-Jul-07 5:19 
GeneralSimple question about lblTimerValue Pin
Norshuhada Samudin24-Jun-07 1:30
memberNorshuhada Samudin24-Jun-07 1:30 
GeneralHi , i need your help Pin
Exelioindia12-Jun-07 3:56
memberExelioindia12-Jun-07 3:56 
QuestionI need the help for My graduation project Pin
aishar10-May-07 10:54
memberaishar10-May-07 10:54 
Questionwhere and how? Pin
itce200523-Apr-07 10:48
memberitce200523-Apr-07 10:48 
AnswerRe: where and how? Pin
s.s.cheral26-Oct-07 1:32
members.s.cheral26-Oct-07 1:32 
QuestionError Handling Pin
Abu Syed Khan9-Mar-07 7:08
memberAbu Syed Khan9-Mar-07 7:08 
QuestionCan i get the code for doing it in matlab Pin
balaji rao8513-Jan-07 16:23
memberbalaji rao8513-Jan-07 16:23 
AnswerRe: Can i get the code for doing it in matlab Pin
Member 421587326-Sep-08 1:53
memberMember 421587326-Sep-08 1:53 
GeneralVideo SHOW on C# Pin
CyBeRiaMiaM12-Jan-07 4:51
memberCyBeRiaMiaM12-Jan-07 4:51 
GeneralRe: Video SHOW on C# Pin
prat938-Feb-07 0:48
memberprat938-Feb-07 0:48 
GeneralRe: Video SHOW on C# Pin
CyBeRiaMiaM10-Feb-07 23:41
memberCyBeRiaMiaM10-Feb-07 23:41 
GeneralRe: Video SHOW on C# Pin
TheCreeep21-Feb-07 7:04
memberTheCreeep21-Feb-07 7:04 
Generalis it free Pin
yamanalp9-Jan-07 23:10
memberyamanalp9-Jan-07 23:10 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 10 Jun 2004
Article Copyright 2004 by Taha Amin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid