Click here to Skip to main content
Licence 
First Posted 25 May 2004
Views 363,021
Bookmarked 263 times

Motion detection using web cam

By | 9 Jun 2004 | Article
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

About the Author

Taha Amin

Team Leader
http://www.linkdev.com/
Egypt Egypt

Member

B/OSS.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUrgent!!! PinmemberMember 858515418:39 26 Feb '12  
GeneralTUNER Pingroupnithinravi53522:55 26 Dec '10  
GeneralDear Mr. Taha Amin Pinmemberuvik11:33 30 Sep '10  
QuestionIn which assembly i found camserver.dll & ijl15.dll ? PinmemberSaira1317:28 7 Apr '10  
Questionmotion detection using web cam in C# PinmemberSaira134:34 7 Apr '10  
Generalmotion detection code with MATLAB Pinmemberasalrasul11:02 20 Mar '10  
GeneralRe: motion detection code with MATLAB Pinmembertrzik2923:36 2 May '11  
Generalhelp Me! Pinmemberbrysamae20:02 26 Jan '10  
GeneralRe: help Me! Pinmemberuvik22:54 30 Sep '10  
GeneralDetect movement from left or from right PinmemberALEXNG882:21 28 Dec '09  
GeneralThanks Pinmemberstartstarts2:02 15 Dec '09  
GeneralI need a help! Pinmemberthurein093:08 16 Jul '09  
Questionneeds some advice for my FYP "Golf Swing Analysis Software" PinmemberOak Soe5:45 26 Jun '09  
QuestionRetrieving the COM class factory for component with CLSID {FDD4C8C1-69C9-11D3-9DC8-525400E38CF9} failed due to the following error: 80040154 Pinmemberalish061020:02 5 Apr '09  
QuestionHelp me Pinmemberfindingbooks21:41 1 Apr '09  
Generalnice article Pinmemberarunvikc21:51 21 Feb '09  
GeneralRetrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154. PinmemberOmsk23:39 28 Dec '08  
QuestionRotating of WebCam PinmemberYena papa4:04 16 Sep '08  
QuestionHow to get the Video Stream From CF Camera Pinmembervison_xuan6:22 14 Apr '08  
Questionhow many percent changes in the video will consider motion? Pinmembertansc21:46 25 Feb '08  
QuestionHow to capture avi file? [modified] PinmemberHady CV3:46 18 Nov '07  
Hi, sorry for my bad english.
I tried to capture avi file when motion detected.
But I'm stucked, because the media subtype in SetupGraph() of mainform.cs is RGB24, if we want to capture avi, we use subtype.avi, wasn't it?
What I want to do is, capture avi when motion detected, stop capture avi when there are no motion detected, and like that continuously.
So, the motion detection detect all the time.
Anyone have an idea?Confused | :confused:
thanks.
my e-mail: hady.cv@gmail.com
 

 
-- modified at 9:55 Sunday 18th November, 2007
AnswerRe: How to capture avi file? PinmemberTaha Zayed4:14 18 Nov '07  
QuestionRe: How to capture avi file? PinmemberHady CV4:39 18 Nov '07  
AnswerRe: How to capture avi file? PinmemberTaha Zayed4:52 18 Nov '07  
GeneralRe: How to capture avi file? PinmemberHady CV5:06 18 Nov '07  

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.

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