below is my code: i am getting an error null reference exception was unhandled on following highlighted line
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
namespace CameraCapture
{
public partial class CameraCapture : Form
{
//declaring global variables
private Capture capture; //takes images from camera as image frames
private HaarCascade haar; //the viola-jones classifier (detector)
public CameraCapture()
{
InitializeComponent();
}
//------------------------------------------------------------------------------//
//Process Frame() below is our user defined function in which we will create an EmguCv
//type image called ImageFrame. capture a frame from camera and allocate it to our
//ImageFrame. then show this image in ourEmguCV imageBox
//------------------------------------------------------------------------------//
private void ProcessFrame(object sender, EventArgs arg)
{
//Read an image from hard disk at location e.g 'E:\IMAGES\'
//Image InputImg = Image.FromFile(@"E:\IMAGES\MyPic.jpg");
//Image<Bgr, byte> MyImage = new Image<Bgr, byte>(new Bitmap(InputImg));
//fetch the frame captured by web camera
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
//Detect Faces from the ImageFrame
if (ImageFrame != null) // confirm that image is valid
{
//convert the image to gray scale
Image<Gray, byte> grayframe = ImageFrame.Convert<Gray, byte>();
//detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]'
var faces = grayframe.DetectHaarCascade(haar, 1.4, 4,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(25, 25))[0];
//draw a green rectangle on each detected face in image
foreach (var face in faces)
{
ImageFrame.Draw(face.rect, new Bgr(Color.Green), 3);
}
}
//show the image in the EmguCV ImageBox
CamImageBox.Image = ImageFrame;
//To show the image in windows forms PictureBox called pictureBox1
//pictureBox1.Image= ImageFrame.ToBitmap();
}
//btnStart_Click() function is the one that handles our "Start!" button's click
//event. it creates a new capture object if its not created already. e.g at first time
//starting. once the capture is created, it checks if the capture is still in progress,
private void btnStart_Click(object sender, EventArgs e)
{
if (capture != null)
{
if (btnStart.Text == "Pause")
{ //if camera is getting frames then pause the capture and set button Text to
// "Resume" for resuming capture
btnStart.Text = "Resume"; //
Application.Idle -= ProcessFrame;
}
else
{
//if camera is NOT getting frames then start the capture and set button
// Text to "Pause" for pausing capture
btnStart.Text = "Pause";
Application.Idle += ProcessFrame;
}
}
}
private void ReleaseData()
{
if (capture != null)
capture.Dispose();
}
/// <summary>
/// This function is the Selected Index changed event of the comboBox
/// It allows the user to select a desired camera and starts it for him/her
/// </summary>
private void cbCamIndex_SelectedIndexChanged(object sender, EventArgs e)
{
//Set the camera number to the one selected via combo box
int CamNumber = -1;
CamNumber = int.Parse(cbCamIndex.Text);
//Start the selected camera
#region if capture is not created, create it now
if (capture == null)
{
try
{
capture = new Capture(CamNumber);
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
//Start showing the stream from camera
btnStart_Click(sender, e);
btnStart.Enabled = true;
}
private void CameraCapture_Load(object sender, EventArgs e)
{
// adjust path to find your xml at loading
haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
}
}
}