Why is this variable NULL?

  • Thread starter Thread starter Ulf Christenson
  • Start date Start date
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);
}
}
}
 
After serious thinking Ulf Christenson wrote :
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:
....
namespace FaceDetection
{
public partial class MainForm : Form
{ ....
private HaarCascade face;

public MainForm()
{ ...
HaarCascade face = new
HaarCascade("haarcascade_frontalface_alt2.xml");


You are not assigning to the "class-level" variable 'face', but to a
private variable that happens to have the same name and hides the one
you meant to use...

So change the "HaarCascade face" into just "face" within your
constructor.

Hans Kesting
 
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 therectangle 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);
       }
    }



}- Hide quoted text -

- Show quoted text -

As Ulf pointed out, the locally declared variable 'face' is being
confused for the public declaration.

That should come up as a compiler warning, no?


Regards,

Sphere 10 Software (www.sphere10.com)
File Server Migrator - Copy shares with security
http://www.sphere10.com/fileservermigrator/
 
Back
Top