|
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
namespace Objcap
{
public partial class Form1 : Form
{
private CVCapture capture; //buffer for captured image from webcam
private CVImage backgnd; //buffer for previous image, subtract's result and captured image
private byte threshold=30; //threshold value = 30
private bool only_first = false; //flag
private int filename = 0;
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
}
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 backgroun frame for next time
}
}
pictureBox1.Image = frame.ToBitmap(); //display image
int objsize = 500; //depend on Size of Object to be captured
// if number of different pixel > 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 imagea
}
GC.Collect();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.