Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms

Use OpenCVdotNet to Capture Object on Webcam

Rate me:
Please Sign up or sign in to vote.
4.71/5 (10 votes)
27 Jul 2009CPOL1 min read 77.2K   5.8K   48   12
This program uses a webcam to take a picture of an object if there is a change in picture.

Introduction 

This code is based on "Webcam Simple Frame Difference" at the blog: http://haryoktav.wordpress.com/2009/02/28/webcam-c-simple-frame-difference/.

Objectives 

  • Monitoring a room with a webcam
  • No need to save a long movie, just capture pictures showing the change
  • Easy to view the result, no need to view the entire long movie (maybe it does not have any change) 

Background

This code uses OpenCVdotNet library to:

  1. download and install OpenCV 1.0 - http://sourceforge.net/projects/opencv/ (do not install 1.1)
  2. download and install OpenCVDotNet 0.7 - http://code.google.com/p/opencvdotnet/

I use Visual Studio 2005 to design a form like this:

01.PNG

The form has 2 pictureboxes to show the current image and the captured image; 1 timer to repeat checking the difference of image in the room and save to file if needed; 1 button to active timer, 1 label to show the number of captured images.

Using the Code 

There is only one form, and the code is as given below: 

After installing OpenCVDotnet library, add it to the project:

C#
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using OpenCVDotNet; 	//don't forget to add opencvdotnel.dll as Reference

Declare some private variables:

C#
namespace Objcap
{
    public partial class Form1 : Form
    {
        private CVCapture capture;            //buffer for captured image from webcam
        private CVImage backgnd;              //buffer for previous image, 
					//subtracts result and captured image
        private byte threshold=30;            //threshold value = 30
        private bool only_first = false;      //flag
        private int filename = 0//to make file name change after each save 

Build a function for button1_Click: to active timer: 

C#
public Form1()
   {
       InitializeComponent();
   }
   private void button1_Click(object sender, EventArgs e)
   {
       timer1.Enabled = !(timer1.Enabled); //toggle start/stop
       if (timer1.Enabled)
           capture = new CVCapture(0);     //start camera when timer enabled
       else
           capture.Release();              //stop camera when timer disabled
   }        

The main function is in timer1_tick:

Every time Timer1 ticks (set in interval properties = 500 ->500 milliseconds), we need to compare the previous image (saved in backgnd) with current image (frame).
If there is a difference, it means there is an object in or out of your room. So save that image.

C#
private void timer1_Tick(object sender, EventArgs e)
{
    using (CVImage frame = capture.QueryFrame())  //get camera frame
    {
        frame.Resize(160, 120);             	//resize image to 160x120 pixel
        if (only_first == false)            	//only first time
        {
            backgnd = new CVImage(frame);   	//copy buffer to get the same size
            only_first = true;              	//disable flag forever
        }
        //gray scale conversion
        for (int row = 0; row < frame.Height; ++row)
        {
            for (int col = 0; col < frame.Width; ++col)
            {
                CVRgbPixel pixel = frame[row, col]; 	//get current pixel
                byte bwValue = pixel.BwValue;   	//get the gray color
                frame[row, col] = new CVRgbPixel(bwValue, bwValue, bwValue);//set current
								// pixel to gray
            }
        }
        int pixelnum = 0; // number of different pixel
        for (int row = 0; row < frame.Height; ++row)
        {
            for (int col = 0; col < frame.Width; ++col)
            {
                if (Math.Abs(frame[row, col].BwValue - 
			backgnd[row, col].BwValue) > threshold)
                {                       
                    pixelnum++;
                }
                backgnd[row, col] = frame[row, col];  	// save current frame to 
						// background frame for next time
            }
        } 
         pictureBox1.Image = frame.ToBitmap();  //display image
         int objsize = 500;  	// depends on Size of Object to be captured
                             	// if number of different pixels > 
				// objsize then save image to hard drive
        if (pixelnum > objsize)
        {
            using (CVImage frame2 = capture.QueryFrame())
            {                              
                // Capture color image
                filename++;                      	//change file name   
                string path = "D:/webcam/";        	// Path to save file
                path += DateTime.Today.Day.ToString()+"."+
                	filename.ToString() + ".jpg";   	// Full path - file name with 
						// added day saved         
                frame2.ToBitmap().Save(path, ImageFormat.Jpeg);   // Save image file 
						// with max resolution of webcam
                frame2.Resize(160, 120);               // Resize image to show on 
						// picturebox3
                pictureBox3.Image = frame2.ToBitmap();	//Show captured image
            }
        }      
         label5.Text = "Pictures: " + filename.ToString();   	// Show number of 
							// captured images
    }
    GC.Collect();
}

Points of Interest

This code runs well. You can run the program for a month if you want.
The program will save only important images with file named by saving day.

The program when run: (remember to create a folder: D:\Webcam):

02.PNG

And the result (saved files):

03.PNG

History

I tried to improve it. Now it does not cost memory any more.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Vietel Corporation
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugHelp Pin
tuning100122-Mar-13 5:49
tuning100122-Mar-13 5:49 
Questionplease help! Exception error about OpenCVdotnet! Pin
zaeemi22-Aug-11 9:44
zaeemi22-Aug-11 9:44 
Generali hope you help me! Pin
hoangtuep24-Apr-11 8:19
hoangtuep24-Apr-11 8:19 
GeneralRe: i hope you help me! Pin
ngoxuandang17-Jul-11 23:50
professionalngoxuandang17-Jul-11 23:50 
Generalgood Pin
Dev. RoOo75-Feb-11 23:08
Dev. RoOo75-Feb-11 23:08 
Generalwebcam picture capture Pin
jhonbuker@yahoo.com23-Aug-10 8:05
jhonbuker@yahoo.com23-Aug-10 8:05 
GeneralRe: webcam picture capture Pin
Ash_shaymaa30-Nov-11 11:10
Ash_shaymaa30-Nov-11 11:10 
Generalhelp Pin
chiquest 19-Mar-10 2:18
chiquest 19-Mar-10 2:18 
QuestionWhat files are required to deploy the exe to a fresh pc? Pin
samprog18-Feb-10 1:14
samprog18-Feb-10 1:14 
Generalhelp Pin
gaptek17-Oct-09 11:19
gaptek17-Oct-09 11:19 
AnswerRe: help Pin
samprog18-Feb-10 1:22
samprog18-Feb-10 1:22 
Generalconsider Emgu over OpenCVdotnet Pin
xoox27-Jul-09 10:50
xoox27-Jul-09 10:50 

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.