【问题标题】:EmguCV Attempted to read or write protected memoryEmguCV 试图读取或写入受保护的内存
【发布时间】:2012-04-16 14:05:54
【问题描述】:

我有以下代码使用 EmgucV 在 imagebox 中显示图像:

    Capture capture;
    Image<Bgr, Byte> image;

    public Form1()
    {
        InitializeComponent();
        Application.Idle += new EventHandler(Start);
    }
    void Start(object sender, EventArgs e)
    {
        capture = new Capture();
        image = capture.QueryFrame();
        imageBox1.Image = image;
    }

我得到了异常Attempted to read or write protected memory。我需要做什么来纠正这个问题?

【问题讨论】:

    标签: c# exception exception-handling emgucv


    【解决方案1】:

    这表明可能存在本机内存泄漏

    我认为您的代码有错误。您的 Start 方法将在应用程序生命周期内被多次(非常频繁地)调用。

    看起来你应该在你的应用程序中只使用一个 Capture 对象。

    只需将 Capture 实例化到 Form 构造函数:

    Capture capture;
    
    public Form1()
    {
        InitializeComponent();
        Application.Idle += new EventHandler(Capture);
        capture = new Capture();
    }
    void Capture(object sender, EventArgs e)
    {
        imageBox1.Image = capture.QueryFrame(); 
    }
    

    【讨论】:

      【解决方案2】:

      Emgu CV 版本 3.4.1 使用 Windows Form c# 的当前修复;添加一个按钮,将其称为 btnCapture,添加一个 PictureBox 控件,出于此答案的目的,将名称保留为其默认值。希望此代码示例对您有所帮助

          public VideoCapture capture;
          private void btnCatpure_Click(object sender, EventArgs e)
          {
              // each click a single frame will be capture and then display in the control.
              Mat iframe = new Mat();
              capture.Retrieve(iframe, 0);
              Mat grayFrame = new Mat();
              CvInvoke.CvtColor(iframe, grayFrame, ColorConversion.Bgr2Gray);
      
              pictureBox1.Image =  iframe.Bitmap;
              pictureBox1.Image = grayFrame.Bitmap;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-08
        • 2012-09-21
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多