0

“试图读取或写入受保护的内存。这通常表明其他内存已损坏。” 当我尝试通过调用RetrieveBgrFrame()方法来存储帧时出现此错误。实际上我正在尝试打开一个中间文件,当 ImageGrabbed 事件获取文件时尝试将其存储在图像变量中以进一步处理文件类型 Dummy.avi 这是我的代码:

public partial class CameraCapture : Form
{
  private Capture _capture = null;
  private bool _captureInProgress;


  public CameraCapture()
  {
     InitializeComponent();
     try
     {
        _capture = new Capture(@"D:\imageprocessing\CapturedImages\CameraSample-1.avi");
        _capture.ImageGrabbed += ProcessFrame;
     }
     catch (NullReferenceException excpt)
     {
        MessageBox.Show(excpt.Message);
     }
  }

  private void ProcessFrame(object sender, EventArgs arg)
  {
     **Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();**
     *//Here is the Error*        

     Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
     Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
     Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
     Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(100, 60);

     captureImageBox.Image = frame;
     grayscaleImageBox.Image = grayFrame;
     smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
     cannyImageBox.Image = cannyFrame;
  }

  private void captureButtonClick(object sender, EventArgs e)
  {
     if (_capture != null)
     {
        if (_captureInProgress)
        {  //stop the capture
           captureButton.Text = "Start Capture";
           _capture.Pause();
        }
        else
        {
           //start the capture
           captureButton.Text = "Stop";
           _capture.Start();
        }

        _captureInProgress = !_captureInProgress;
     }
  }

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

}

4

1 回答 1

0

我认为你需要Grab()在使用之前做一个RetrieveBgrFrame(参见函数的 IntelliSense 文档)。有一种更简单的方法可以做到这一点,即使用该QueryFrame()函数。

这是在 EmguCV 中捕获视频的最少代码:

using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using System.Drawing;
using System.Windows.Forms;
...

ImageViewer viewer = new ImageViewer(); //create an image viewer
Capture capture = new Capture(); //create a camera captue
Application.Idle += new EventHandler(delegate(object sender, EventArgs e)
{  //run this until application closed (close button click on image viewer)
   viewer.Image = capture.QueryFrame(); //draw the image obtained from camera
});
viewer.ShowDialog(); //show the image viewer

您可以在EmguCV官方网站上找到此示例。

于 2013-10-01T15:07:02.703 回答