2014-08-28 149 views
-1
//---------------------------------------------------------------------------- 
// Copyright (C) 2004-2013 by EMGU. All rights reserved.  
//---------------------------------------------------------------------------- 

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

namespace MotionDetection 
{ 
    public partial class Form1 : Form 
    { 
     private Capture _capture; 
     private MotionHistory _motionHistory; 
     private IBGFGDetector<Bgr> _forgroundDetector; 

     public Form1() 
     { 
     InitializeComponent(); 

     //try to create the capture 
     if (_capture == null) 
     { 
      try 
      { 
       _capture = new Capture(); 
      } 
      catch (NullReferenceException excpt) 
      { //show errors if there is any 
       MessageBox.Show(excpt.Message); 
      } 
     } 

     if (_capture != null) //if camera capture has been successfully created 
     { 
      _motionHistory = new MotionHistory(
       1.0, //in second, the duration of motion history you wants to keep 
       0.05, //in second, maxDelta for cvCalcMotionGradient 
       0.5); //in second, minDelta for cvCalcMotionGradient 

      _capture.ImageGrabbed += ProcessFrame; 
      _capture.Start(); 
     } 
     } 

     private void ProcessFrame(object sender, EventArgs e) 
     { 
     using (Image<Bgr, Byte> image = _capture.RetrieveBgrFrame()) 
     using (MemStorage storage = new MemStorage()) //create storage for motion components 
     { 
      if (_forgroundDetector == null) 
      { 
       //_forgroundDetector = new BGCodeBookModel<Bgr>(); 
       _forgroundDetector = new FGDetector<Bgr>(Emgu.CV.CvEnum.FORGROUND_DETECTOR_TYPE.FGD); 
       //_forgroundDetector = new BGStatModel<Bgr>(image, Emgu.CV.CvEnum.BG_STAT_TYPE.FGD_STAT_MODEL); 
      } 

      _forgroundDetector.Update(image); 

      capturedImageBox.Image = image; 

      //update the motion history 
      _motionHistory.Update(_forgroundDetector.ForegroundMask); 

      forgroundImageBox.Image = _forgroundDetector.ForegroundMask; 

      #region get a copy of the motion mask and enhance its color 
      double[] minValues, maxValues; 
      Point[] minLoc, maxLoc; 
      _motionHistory.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc); 
      Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0/maxValues[0]); 
      #endregion 

      //create the motion image 
      Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMask.Size); 
      //display the motion pixels in blue (first channel) 
      motionImage[0] = motionMask; 

      //Threshold to define a motion area, reduce the value to detect smaller motion 
      double minArea = 100; 

      storage.Clear(); //clear the storage 
      Seq<MCvConnectedComp> motionComponents = _motionHistory.GetMotionComponents(storage); 

      //iterate through each of the motion component 
      foreach (MCvConnectedComp comp in motionComponents) 
      { 
       //reject the components that have small area; 
       if (comp.area < minArea) continue; 

       // find the angle and motion pixel count of the specific area 
       double angle, motionPixelCount; 
       _motionHistory.MotionInfo(comp.rect, out angle, out motionPixelCount); 

       //reject the area that contains too few motion 
       if (motionPixelCount < comp.area * 0.05) continue; 

       //Draw each individual motion in red 
       DrawMotion(motionImage, comp.rect, angle, new Bgr(Color.Red)); 
      } 

      // find and draw the overall motion angle 
      double overallAngle, overallMotionPixelCount; 
      _motionHistory.MotionInfo(motionMask.ROI, out overallAngle, out overallMotionPixelCount); 
      DrawMotion(motionImage, motionMask.ROI, overallAngle, new Bgr(Color.Green)); 

      //Display the amount of motions found on the current image 
      UpdateText(String.Format("Total Motions found: {0}; Motion Pixel count: {1}", motionComponents.Total, overallMotionPixelCount)); 

      //Display the image of the motion 
      motionImageBox.Image = motionImage; 
     } 
     } 

     private void UpdateText(String text) 
     { 
     if (InvokeRequired && !IsDisposed) 
     { 
      Invoke((Action<String>)UpdateText, text); 
     } 
     else 
     { 
      label3.Text = text; 
     } 
     } 

     private static void DrawMotion(Image<Bgr, Byte> image, Rectangle motionRegion, double angle, Bgr color) 
     { 
     float circleRadius = (motionRegion.Width + motionRegion.Height) >> 2; 
     Point center = new Point(motionRegion.X + motionRegion.Width >> 1, motionRegion.Y + motionRegion.Height >> 1); 

     CircleF circle = new CircleF(
      center, 
      circleRadius); 

     int xDirection = (int)(Math.Cos(angle * (Math.PI/180.0)) * circleRadius); 
     int yDirection = (int)(Math.Sin(angle * (Math.PI/180.0)) * circleRadius); 
     Point pointOnCircle = new Point(
      center.X + xDirection, 
      center.Y - yDirection); 
     LineSegment2D line = new LineSegment2D(center, pointOnCircle); 

     image.Draw(circle, color, 1); 
     image.Draw(line, color, 2); 
     } 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 

     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 

     base.Dispose(disposing); 
     } 

     private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
     { 
     _capture.Stop(); 
     } 
    } 
} 

我編emgu CV例如爲什麼emgu cv example does not work?

他的名字是MotionDetection

但是當我編譯該示例中,結果是錯誤,他給我的消息

爲的類型初始值'Emgu.CV.CvInvoke'引發了一個異常。

我被困在這個_capture = new Capture();

我引用添加到項目 和我有一份opencv_core290.dll,opencv_highgui290.dll和opencv_imgproc290.dll到項目文件夾 但它仍然沒有工作

該消息仍然相同 'Emgu.CV.CvInvoke'的類型初始值設定項引發異常。

我使用Visual Studio 2012 Ultimate和Win 8.1 任何人都可以幫助我嗎?

謝謝:)

+0

如果有人可以回答這個,如果有問題發佈的代碼,這將是一個恥辱。 – Adam 2014-08-28 13:25:48

回答

0

The type initializer for 'Emgu.CV.CvInvoke' threw an exception意味着你需要看無論是在類的靜態構造函數,或在類的任何靜態成員的初始化。最重要的是,我會看看你拋出異常的InnerException屬性。

我敢打賭,你有靜態的方法或元素被錯誤地或非法地使用。

這裏採取甘德有關TypeInitializationExceptionhttp://msdn.microsoft.com/en-us/library/system.typeinitializationexception(v=vs.110).aspx

+0

它來自示例 我無法編譯它 。教程說只是打開示例項目並編譯它 。它必須工作, 但它沒有爲我 – user3094912 2014-08-28 15:44:21

0

爲「Emgu.CV.CvInvoke」的類型初始值引發了異常其意味着EMGU.CV文件不是由代碼dectecting。這是由於系統依賴性。你需要進入可視stdio中的配置管理器窗口,並選擇你需要的平臺,然後根據你的平臺複製相應的dll文件(x64的64個dll文件和x86的32位dll可以從emgu網站下載)。現在複製它沒有像emgu.cv.invoke錯誤