2013-10-15 3 views
0

Как сохранить изображение камеры emgu CV в папке и получить. Всякий раз, когда я пытаюсь сохранить ошибку ошибки null reference exception.Как сохранить изображение камеры emgu в папке и получить

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Emgu.CV; 
using Emgu.CV.UI; 
using Emgu.CV.Util; 
using Emgu.CV.Structure; 

namespace camerapart2 
{ 
    public partial class Form1 : Form 
    { 
     private Capture capture; 
     private bool captureinprogress; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void ProcessFrame(object sender, EventArgs arg) 

     Image<Bgr, Byte> ImageFrame = capture.QueryFrame(); 
     cameraimage.Image = ImageFrame; 
     pictureBox1.Image = ImageFrame.ToBitmap(); 
     ImageFrame.Save(@"E:\MyPic.jpg"); 

     private void btnStart_Click(object sender, EventArgs e) 
     { 
      if (capture == null) 
      { 
       try 
       { 
        capture = new Capture(); 
       } 
       catch (NullReferenceException excpt) 
       { 
        MessageBox.Show(excpt.Message); 
       } 
      } 

      if (capture != null) 
      { 
       if (captureinprogress) 
       { //if camera is getting frames then stop the capture and set button Text 
        // "Start" for resuming capture 
        btnstart.Text = "Start!"; // 
        Application.Idle -= ProcessFrame; 
       } 
       else 
       { 
        //if camera is NOT getting frames then start the capture and set button 
        // Text to "Stop" for pausing capture 
        btnstart.Text = "Stop"; 
        Application.Idle += ProcessFrame; 
       } 

       captureinprogress = !captureinprogress; 
      } 
     } 

     private void ReleaseData() 
     { 
      if (capture != null) 
       capture.Dispose(); 
     } 
    } 
} 

В этих двух строках я получаю ошибку nullrefference:

pictureBox1.Image = ImageFrame.ToBitmap(); 
ImageFrame.Save(@"E:\MyPic.jpg"); 
+0

Тогда, очевидно, 'ImageFrame' является' null'. Проверьте документацию, почему 'Capture.QueryFrame()' возвращает 'null'. – CodeCaster

ответ

0

я использовал Imagebox emguCV вместо нативного С # PictureBox и добавил кнопку btnSave на форме

namespace CameraCapture 
{ 
    public partial class CameraCapture : Form 
    { 
     //declaring global variables 
     private Capture capture;  //takes images from camera as image frames 
     private bool captureInProgress; 
     private bool saveToFile; 

     public CameraCapture() 
     { 
      InitializeComponent(); 
     } 
     //------------------------------------------------------------------------------// 
     //Process Frame() below is our user defined function in which we will create an EmguCv 
     //type image called ImageFrame. capture a frame from camera and allocate it to our 
     //ImageFrame. then show this image in ourEmguCV imageBox 
     //------------------------------------------------------------------------------// 
     private void ProcessFrame(object sender, EventArgs arg) 
     { 
      Image<Bgr, Byte> ImageFrame = capture.QueryFrame(); 
      CamImageBox.Image = ImageFrame; 
      if (saveToFile) 
      { 
       ImageFrame.Save(@"D:\MyPic.jpg"); 
       saveToFile = !saveToFile; 
      } 
     } 

     //btnStart_Click() function is the one that handles our "Start!" button' click 
     //event. it creates a new capture object if its not created already. e.g at first time 
     //starting. once the capture is created, it checks if the capture is still in progress, 
     //if so the 
     private void btnStart_Click(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) 
       { //if camera is getting frames then stop the capture and set button Text 
        // "Start" for resuming capture 
        btnStart.Text = "Start!"; // 
        Application.Idle -= ProcessFrame; 
       } 
       else 
       { 
        //if camera is NOT getting frames then start the capture and set button 
        // Text to "Stop" for pausing capture 
        btnStart.Text = "Stop"; 
        Application.Idle += ProcessFrame; 
       } 

       captureInProgress = !captureInProgress; 
      } 
     } 

     private void ReleaseData() 
     { 
      if (capture != null) 
       capture.Dispose(); 
     } 

     private void btnSave_Click(object sender, EventArgs e) 
     { 
      saveToFile = !saveToFile; 
     } 
    } 
} 
0

это тема старая, но если вы хотите использовать emguCV's ImageBox, вы можете использовать этот код

private void ProcessFrame(object sender, EventArgs arg) 
    { 
     Mat frame = new Mat(); 
     capture.Retrieve(frame); 
     CamImageBox.Image = frame; 
    } 
Смежные вопросы