Why does RetrieveBgrFrame() return null?

半腔热情 提交于 2019-12-11 20:25:12

问题


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.


回答1:


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;
             }
         }
    }
}


来源:https://stackoverflow.com/questions/19182299/why-does-retrievebgrframe-return-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!