Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# EMGU EMGUCV
I am doing face detection using haar detector from emgu cv open library.
Using the language C#.
 
public Form1()
{
    InitializeComponent();
    try
    {
        haar = new HaarCascade("C:\\Program Files\\emgucv2.1.0.793\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml");
        
        capture = new Capture(0);
    }
    catch
    {
    }
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    {
        Image<bgr,> image = capture.QueryFrame();
    Bitmap original = image.ToBitmap(pictureBox1.Width,pictureBox1.Height);
 
It is giving error at run time.
It does not show image in the picture box when it runs and gives null reference exception in line:
 Image<bgr,> image = capture.QueryFrame();
I have used breakpoints to detect errors, it is going to the Haar line but it not entering in capture line capture= new capture(0).
 
Can anyone tell me the error why image is not been captured in picture box at run time?
I tried this using emgucv liberary in win XP as well as window 7.
Posted 3-Jan-11 21:29pm
Edited 29-Sep-11 9:05am
v3
Comments
JF2015 at 4-Jan-11 2:31am
   
Edited to fix code formatting.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Empty catch blocks are always wrong.
Catch the error as it happens, otherwise you'll never know the real problem.
 
Cheers
  Permalink  
Comments
OriginalGriff at 4-Jan-11 5:35am
   
Not always, sometimes they are your only way to do things. For example, what do you do when there is a problem in your error reporting / logging module? Any attempt to log the problem will likely make the situation worse...
They are an anathema though, and need to be well commented with why they are empty.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I don't know anything about the library you are using on face detection, but I think your problem is with the timer. Asign the tick event handler after the line of code capture = new Capture(0), because probably tick runs before you initialize capture
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
and remove the above line form designer...
 
or
set in designer Enabled = false
and in code after capture = new capture set it to enabled = true
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is an educated guess (given I have not met the EMGUCV library), but from what you say :
"I have used breakpoints to detect errors, it is going to the Haar line but it not entering in capture line capture= new capture(0)."
It would seem that creating a new HaarCascade does not return immediately, so you are not reaching the capture instance creation.
 
Since you are working in a timer, the way I would handle it is:
1) Remove the capture and cascade creation from the form constructor.
2) On the first timer tick, suspend the timer, display a "please wait" message and create the cascade. Re-enable the timer when the cascade is complete, and remove the message. Then create the capture instance and continue.
 
I can't check what should happen with teh cascade, because the EMGU CV website is not responding, but it should have samples there anyway. Have a look and see what they do.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
 
namespace CameraCapture
{
    public partial class CameraCapture : Form
    {
        Image<Bgr, Byte> image;
        private Capture _capture;
        private bool _captureInProgress;
 
        public CameraCapture()
        {
            InitializeComponent();
        }
 
        private void ProcessFrame(object sender, EventArgs arg)
        {
            image = _capture.QueryFrame();
 
            Run();
 

            captureImageBox.Image = image;
 
        }
        public void Run()
        {
 
            Image<Gray, Byte> gray = image.Convert<Gray, Byte>(); //Convert it to Grayscale

 
            //normalizes brightness and increases contrast of the image
            gray._EqualizeHist();
 
            //Read the HaarCascade objects
           HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
             //HaarCascade eye = new HaarCascade("haarcascade_eye.xml");

            //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
            MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
               face,
               1.1,
               10,
               Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
               new Size(20, 20));
 
            foreach (MCvAvgComp f in facesDetected[0])
            {
                //draw the face detected in the 0th (gray) channel with blue color
                image.Draw(f.rect, new Bgr(Color.Blue), 2);
 

 

            }
 

        }
        private void captureButtonClick(object sender, EventArgs e)
        {
            #region if capture is not created, create it now
            if (_capture == null)
            {
                try
                {
                    _capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
            #endregion
 
            if (_capture != null)
            {
                if (_captureInProgress)
                {  //stop the capture
                    captureButton.Text = "Start Capture";
                    Application.Idle -= ProcessFrame;
                }
                else
                {
                    //start the capture
                    captureButton.Text = "Stop";
                    Application.Idle += ProcessFrame;
                }
 
                _captureInProgress = !_captureInProgress;
            }
        }
 
        private void ReleaseData()
        {
            if (_capture != null)
                _capture.Dispose();
        }
    }
}
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 BillWoodruff 330
2 OriginalGriff 310
3 Shweta N Mishra 244
4 Mathew Soji 225
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,638
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,710


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 29 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100