2015-08-13 10 views
1

我製作的項目可以使用Visual Studio C#在帶有bootcamp的Macbook pro上使用Aforge框架跟蹤對象。問題是當我運行代碼並關閉它時。然後重新運行它,代碼將不再在我的相機上顯示圖像。另外,在代碼停止後,我的攝像頭上的綠燈仍然亮着。Macbook相機不會關閉,並有一個小小的C#aforge問題

我的第二個問題是,當我點擊「跟蹤對象按鈕」並將濾鏡應用於圖像時,由於某些原因,它將濾鏡應用於兩個圖片框而不是一個。我需要幫助調試我的代碼。我嘗試重新安排應用過濾器的線條,但我似乎無法做到正確。

這裏是我的代碼

namespace VideoProcessing1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     Graphics g; 
     Bitmap video; 
     bool OnOff; 
     int mode; 
     int thoigianddemnguoc; 

     private FilterInfoCollection CaptureDevice; 
     private VideoCaptureDevice FinalFrame; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
      foreach (FilterInfo Device in CaptureDevice) 
      { 
       comboBox1.Items.Add(Device.Name); 

      } 
      comboBox1.SelectedIndex = 0; 
      FinalFrame = new VideoCaptureDevice(); 
      //FinalFrame.Stop(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString); 
      FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame); 
      FinalFrame.Start(); 

     } 

     void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) 
     { 
      video = (Bitmap)eventArgs.Frame.Clone(); 
      Bitmap video2 = (Bitmap)eventArgs.Frame.Clone(); 
      g = Graphics.FromImage(video2); 
      g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2)); 
      g.Dispose(); 

      if (mode == 1) 
      { 
       ColorFiltering colorfilter = new ColorFiltering(); 
       colorfilter.Red = new AForge.IntRange(0, 255); 
       colorfilter.Green = new AForge.IntRange(0, 75); 
       colorfilter.Green = new AForge.IntRange(0, 75); 
       colorfilter.ApplyInPlace(video2); 
       BlobCounter blobcounter = new BlobCounter(); 
       blobcounter.MinHeight = 20; 
       blobcounter.MaxWidth = 20; 
       blobcounter.ObjectsOrder = ObjectsOrder.Size; 
       blobcounter.ProcessImage(video2); 
       Rectangle[] rect = blobcounter.GetObjectsRectangles(); 

       if (rect.Length > 0) 
       { 
        Rectangle objec = rect[0]; 
        Graphics graphic = Graphics.FromImage(video2); 
        using (Pen pen = new Pen(Color.White, 3)) 
        { 
         graphic.DrawRectangle(pen, objec); 
        } 
        graphic.Dispose(); 
       } 
       pictureBox2.Image = video2; 

      } 
      pictureBox1.Image = video2; 
     } 

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      if(FinalFrame.IsRunning==true) 
      { 
       FinalFrame.Stop(); 
      } 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      mode = 1; 
     } 
    } 
} 

回答

0

呼叫您的視頻捕獲設備上Stop不推薦。根據docs:「停止相機的正確方法是通知它停止,然後等待後臺線程完成。」

你應該嘗試使用這樣的東西,止損:

FinalFrame.SignalToStop(); 
FinalFrame.WaitForStop(); 

線程的行爲可以獲得不可測的,如果不正確使用,我相信你的問題就出在這裏。

關於你的第二個問題,這個問題行是

pictureBox1.Image = video2; 

你已經存儲在pictureBox2.Imagevideo2所以我假設你不想這樣做,其他的PictureBox。

+0

感謝您的回覆。我仍然無法關閉相機。我使用了你建議的代碼,甚至爲此目的添加了一個按鈕,但無濟於事。 – rorivy15