已尝试读取或写入受保护的内存.同时尝试在图像变量中接收帧

本文关键字:变量 图像 内存 读取 受保护 | 更新日期: 2023-09-27 18:32:05

>"尝试读取或写入受保护的内存。这通常表明其他内存已损坏。 当我尝试通过调用 RetrieveBgrFrame() 方法存储帧时出现此错误。实际上我正在尝试打开一个中间文件,当 ImageGrabbed 事件得到文件时,尝试将其存储在图像变量中以便进一步处理文件类型虚拟.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();
  }

}

已尝试读取或写入受保护的内存.同时尝试在图像变量中接收帧

我认为您需要在使用RetrieveBgrFrame之前执行Grab()(请参阅该函数的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官方网站上找到这个例子。