U
Ulf Christenson
Hello! Can anybody tell me why "face" is null in the second void,
although I successfully initiated/ loaded (or whatever it may be called)
in the InitializeComponent event?
This is the line where the error is thrown:
Rectangle<double>[][] facesDetected = gray.DetectHaarCascade(face);
//Here error is thrown
And this is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.Util;
using System.Threading;
namespace FaceDetection
{
public partial class MainForm : Form
{
private Capture _capture;
private Thread _captureThread;
private HaarCascade face;
public MainForm()
{
InitializeComponent();
//Read the HaarCascade object
HaarCascade face = new
HaarCascade("haarcascade_frontalface_alt2.xml");
//try to create the capture
if (_capture == null)
{
try
{
_capture = new Capture();
}
catch (NullReferenceException excpt)
{ //show errors if there is any
MessageBox.Show(excpt.Message);
}
}
if (_capture != null) //if camera capture has been
successfully created
{
_captureThread = new Thread(ProcessImage);
_captureThread.Start();
}
}
public void ProcessImage()
{
while (true)
{
Image<Bgr, Byte> image = _capture.QueryFrame();
//capturedImageBox.Image = image;
Image<Gray, Byte> gray = image.Convert<Gray, Byte>();
//Convert it to Grayscale
//normalizes brightness and increases contrast of the image
gray._EqualizeHist();
//Detect the faces from the gray scale image and store
the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in
the specific channel
Rectangle<double>[][] facesDetected =
gray.DetectHaarCascade(face); //Here error is thrown
foreach (Rectangle<double> f in facesDetected[0])
{
//draw all the faces detected in the 0th (gray)
channel with blue color
image.Draw(f, new Bgr(Color.Blue), 2);
}
//display the image
imageBox1.Image = image;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (_captureThread != null) _captureThread.Abort();
base.Dispose(disposing);
}
}
}
although I successfully initiated/ loaded (or whatever it may be called)
in the InitializeComponent event?
This is the line where the error is thrown:
Rectangle<double>[][] facesDetected = gray.DetectHaarCascade(face);
//Here error is thrown
And this is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.Util;
using System.Threading;
namespace FaceDetection
{
public partial class MainForm : Form
{
private Capture _capture;
private Thread _captureThread;
private HaarCascade face;
public MainForm()
{
InitializeComponent();
//Read the HaarCascade object
HaarCascade face = new
HaarCascade("haarcascade_frontalface_alt2.xml");
//try to create the capture
if (_capture == null)
{
try
{
_capture = new Capture();
}
catch (NullReferenceException excpt)
{ //show errors if there is any
MessageBox.Show(excpt.Message);
}
}
if (_capture != null) //if camera capture has been
successfully created
{
_captureThread = new Thread(ProcessImage);
_captureThread.Start();
}
}
public void ProcessImage()
{
while (true)
{
Image<Bgr, Byte> image = _capture.QueryFrame();
//capturedImageBox.Image = image;
Image<Gray, Byte> gray = image.Convert<Gray, Byte>();
//Convert it to Grayscale
//normalizes brightness and increases contrast of the image
gray._EqualizeHist();
//Detect the faces from the gray scale image and store
the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in
the specific channel
Rectangle<double>[][] facesDetected =
gray.DetectHaarCascade(face); //Here error is thrown
foreach (Rectangle<double> f in facesDetected[0])
{
//draw all the faces detected in the 0th (gray)
channel with blue color
image.Draw(f, new Bgr(Color.Blue), 2);
}
//display the image
imageBox1.Image = image;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
if (_captureThread != null) _captureThread.Abort();
base.Dispose(disposing);
}
}
}