Click here to Skip to main content
6,594,432 members and growing! (14,114 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Video     Intermediate

Motion detection using web cam

By Taha Amin

Motion detection using webcam, by C#.
C#.NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:25 May 2004
Updated:9 Jun 2004
Views:238,476
Bookmarked:211 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
44 votes for this article.
Popularity: 6.28 Rating: 3.82 out of 5
2 votes, 4.5%
1
4 votes, 9.1%
2
3 votes, 6.8%
3
9 votes, 20.5%
4
26 votes, 59.1%
5

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


Member
B/OSS.
Occupation: Team Leader
Company: http://www.linkdev.com/
Location: Egypt Egypt

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 79 (Total in Forum: 79) (Refresh)FirstPrevNext
GeneralI need a help! Pinmemberthurein094:08 16 Jul '09  
Questionneeds some advice for my FYP "Golf Swing Analysis Software" PinmemberOak Soe6: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 Pinmemberalish061021:02 5 Apr '09  
QuestionHelp me Pinmemberfindingbooks22:41 1 Apr '09  
Generalnice article Pinmemberarunvikc22: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. PinmemberOmsk0:39 29 Dec '08  
QuestionRotating of WebCam PinmemberYena papa5:04 16 Sep '08  
GeneralHow to get the Video Stream From CF Camera Pinmembervison_xuan7:22 14 Apr '08  
Questionhow many percent changes in the video will consider motion? Pinmembertansc22:46 25 Feb '08  
QuestionHow to capture avi file? [modified] PinmemberHady CV4:46 18 Nov '07  
AnswerRe: How to capture avi file? PinmemberTaha Zayed5:14 18 Nov '07  
QuestionRe: How to capture avi file? PinmemberHady CV5:39 18 Nov '07  
AnswerRe: How to capture avi file? PinmemberTaha Zayed5:52 18 Nov '07  
GeneralRe: How to capture avi file? PinmemberHady CV6:06 18 Nov '07  
GeneralRe: How to capture avi file? Pinmemberlakisoft11:53 28 Nov '07  
GeneralRe: How to capture avi file? PinmemberTaha Zayed12:12 28 Nov '07  
AnswerRe: How to capture avi file? Pinmemberalhambra-eidos8:40 8 May '08  
GeneralMore than one device? Pinmemberchmod222212:23 27 Jul '07  
GeneralRe: More than one device? Pinmembersnort4:05 23 Sep '07  
QuestionThanks & Problems PinmemberAli.64855:04 27 Jul '07  
AnswerRe: Thanks & Problems Pinmemberchmod222214:08 27 Jul '07  
GeneralRe: Thanks & Problems PinmemberAli.648523:14 28 Jul '07  
GeneralRe: Thanks & Problems Pinmemberk^s22:54 14 Jun '09  
Generalvery good work PinmemberAbdel-Hameed6:19 23 Jul '07  
GeneralSimple question about lblTimerValue PinmemberNorshuhada Samudin2:30 24 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Jun 2004
Editor: Smitha Vijayan
Copyright 2004 by Taha Amin
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project