Search code examples
c#emgucv

Why does RetrieveBgrFrame() return null?


object reference not set to an instance of an object

My problem is that I try to use CV on a videofile (to simulate a camera) and I can't handle the frames, because RetrieveBgrFrame() doesn't return an image. Instead it gives the above error. My code is:

http://pastebin.com/DNEVwij8

Please tell me if you need additional details.


Solution

  • Your problem is that the field capture is null, because it is never initialized. The code should be as follows:

    public partial class Form1 : Form
        {
            private Image<Bgr, Byte> imgStat = null;
            private Capture capture = null;
            private bool _captureInProgress = false;
         //   private bool captureInProgress;
    
            public Form1()
            {
                InitializeComponent();
                imgStat = null;
             }
    
            public string selectFile()
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();
                String s = ofd.FileName.Normalize();
                return s;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.capture = new Capture(selectFile());                    
                capture.ImageGrabbed += ProcessFrame;
                capture.Start();
            }
    
            private void ProcessFrame(object sender, EventArgs e)
            {
                try
                {
                    //capture.Grab(); //doesnt help
                   // Image<Bgr, byte> beeldje = capture.QueryFrame(); //doesnt work as well
                    Image<Bgr, byte> beeldje = capture.RetrieveBgrFrame();
    
                        DisplayImage(beeldje.ToBitmap());
                }
                catch (Exception er)
                {
                   Console.WriteLine(er.Message);
                }
            }
    
             private delegate void DisplayImageDelegate(Bitmap Image);
             private void DisplayImage(Bitmap Image)
             {
                 if (pictureBox1.InvokeRequired)
                 {
                     try
                     {
                         DisplayImageDelegate DI = new DisplayImageDelegate(DisplayImage);
                         this.BeginInvoke(DI, new object[] {Image});
                     }
                     catch (Exception ex)
                     {
                     }
                 }
                 else
                 {
                     pictureBox1.Image = Image;
                 }
             }
        }
    }