2014-02-24 52 views
0

我正在一個項目中執行線程以處理相機捕獲的圖像。線程放慢相機操作的實時流式傳輸

請在下面的代碼中找到ProcessFrame(),這是由定時器在某段時間間隔後調用的。

private void ProcessFrame() 
{    
      try 
      { 
       Image<Bgr, Byte> ImageFrame; 
       // Get Image from the camera 
       ImageFrame = capture.QueryFrame(); 

       // check if imageFrame is null or not 
       if (ImageFrame == null) 
       { 
    // if null then re- initialize the camera 
        try 
        { 
         capture.Dispose(); 
         capture = new Capture(URL);     
         ImageFrame = capture.QueryFrame();     
        } 
        catch (Exception ex) 
        { 

        } 
       } 
       // resize image to show on Picture control 
       ImageFrame = ImageFrame.Resize(img.Width, img.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); 
    // show image on picture control 
       img.Image = ImageFrame; 
       try 
       { 
    #region Making directory and image in this code 
        string VerifyImageFolderId = VerifyImageFolder + "\\" + "Camera_" + (1) + "\\"; 

        if (!Directory.Exists(VerifyImageFolderId)) 
         Directory.CreateDirectory(VerifyImageFolderId); 

        string VerifyImageFileName = VerifyImageFolderId + "\\" + nSavedImagesCounter + ".jpg"; 
        img.Image.Save(VerifyImageFileName); // Save Image 
        nSavedImagesCounter++; 
        #endregion 

        #region Starting thread For processing Image 
        Thread processImage = new Thread(new ThreadStart(() => ProcessImage(VerifyImageFileName))); 
         processImage.Start();      
        #endregion 

       } 
       catch (Exception ex) 
       { 
        Log(ex.Message); 
        MessageBox.Show(ex.Message); 
       } 
       finally 
       { 
        GC.Collect(); 
       } 
       //#endregion 
      } 
      catch (NullReferenceException e) 
      { 
       Console.Write("Exception:\n" + DateTime.Now.ToString("hhmmss")); 
      } 

     } 

在這裏,是所述第二函數ProcessImage(string ImagePath)這是用來執行某些文件處理操作如下:

private void ProcessImage(string ImagePath) 
    {    
     #region Check for threadSafeFunction 
     if (this.InvokeRequired) 
     { 
      Console.WriteLine("Inside InvokeRequired"); 
      this.Invoke(new MethodInvoker(delegate() { ProcessImage(ImagePath); })); 
     } 
     else 
     { 
1. Detect faces in Image 
    2. Draw Face markers on Image 
    3. Some database based on result of Face Detection 
    4. Delete image File    
     } 

    } 

ProcessFrame()功能的螺紋加工的增加是緩慢的向下後。 ,我無法獲得正在播放的直播內容。

任何人都可以幫助我嗎?

在此先感謝。

回答

1

我建議你使用隊列用於處理圖像 運行這段代碼一旦

 BlockingCollection<string> imageQueue=new BlockingCollection<string>(); 
     new Thread(() => 
     { 
      foreach (string imagePath in imageQueue.GetConsumingEnumerable()) 
      { 
       ProcessImage(imagePath); 
      } 
     }).Start(); 

,改變ProcessFrame喜歡這個

private void ProcessFrame() 
{ 


     ..... 

       #region Starting thread For processing Image 
       imageQueue.Add(VerifyImageFileName); 
       #endregion 

      ... 

    } 
+0

能否請你告訴我,哪裏才能添加第一個代碼? –

+0

這段代碼需要運行一次,你可以把它放在表單加載或如果你有一個初始化的方法,你可以把它放在那裏 – Milad

+0

感謝您的答案,但你能解釋第一部分的答案。爲什麼它只需要intialise一次? –