2016-11-02 32 views
0

我在C#(WindowsForm)中編程。我想使用EmguCV(3.1)來捕獲.avi文件。當我打開一個文件,我看到這個異常:在捕獲時EmguCV(3.x)中的AccessViolationException異常

型「System.AccessViolationException」未處理的異常發生在System.Drawing.dll程序

其他信息:試圖讀取或寫入保護內存。這通常表明其他內存已損壞。

這個異常後,我看到這個窗口:

enter image description here

這裏是我的代碼加載一個文件:

private void LoadVideoFromFile() 
{ 
    OpenFileDialog d = new OpenFileDialog(); 
    d.ShowDialog(); 

    _capture = new Emgu.CV.Capture(d.FileName); 
    _capture.ImageGrabbed += ProcessFrame; 
} 

這裏是我出的avi文件代碼:

private void ProcessFrame(object sender, EventArgs arg) 
{    
    Action a =() => 
     { 
      UMat captured = new UMat(); 
      Boolean cap = _capture.Retrieve(captured); 

      pictureBox1.Image = captured.Bitmap; 
     }; 

    pictureBox1.Invoke(a); 
} 
+0

你是否在調試模式下運行它,你試過版嗎? – Roman

+0

是的,我正在運行調試,但有什麼問題?調試和發佈之間有什麼區別? –

+0

嘗試禁用編輯並繼續功能https://msdn.microsoft.com/en-us/library/ms164926.aspx – Roman

回答

0

你一個如果可用,則使用使用OpenCL的UMat。大多數時候,當我碰到這樣的事情時,這是因爲我忘了爲x64編譯。試試看看是否有幫助。

+0

我編譯我的代碼爲x64。 –

0

好的。我把一個小程序放在一起,看看發生了什麼。請注意,我不是一個WinForms有點貓。我正在使用EmguCV v3.1 x64,爲x64編譯並使用.NET 4.6.1`公共部分類Form1:表格 { VideoCapture _capture; bool run = true; public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog d = new OpenFileDialog(); 
     d.ShowDialog(); 

     tbxFileName.Text = d.FileName; 
     tbxFileName.Refresh(); 
     _capture = new Emgu.CV.VideoCapture(d.FileName); 
     _capture.ImageGrabbed += ProcessFrame; 
     double fps = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps); 
     double frameCount = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount); 

     progressBar1.Maximum = 100; 
     progressBar1.Value = 0; 
     progressBar1.Step = 1; 

     int currentFrame = 1; 

     while(run) 
     { 
      if (!_capture.Grab()) 
       run = false; 

      progressBar1.Value = Convert.ToInt32((++currentFrame/frameCount) * 100); 

      Thread.Sleep(Convert.ToInt32(1000.0/fps)); 

      Application.DoEvents(); 
     } 
    } 
    private void ProcessFrame(object sender, EventArgs arg) 
    { 
     Action a =() => 
     { 
      UMat captured = new UMat(); 
      Boolean cap = _capture.Retrieve(captured); 

      pictureBox1.Image = captured.Bitmap; 
      pictureBox1.Refresh(); 
     }; 

     pictureBox1.Invoke(a); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     run = false; 
    } 
} 

` 我收到的唯一的錯誤是,當我忘了cvextern.dll和其他家屬複製到bin文件夾。

希望這會有所幫助。