2011-08-14 91 views
1

我使用AForge進行運動檢測,我知道可以設置運動區域。是否有可能只在有運動時觸發所有定義的區域? 如果上述功能不易獲取,我正在考慮編寫它。Aforge.Net的運動檢測區域

目前,我的理解是區域設置爲視覺庫MotionDetector.cs中的zoneFrame。我正在考慮爲每個地區做這件事,但似乎效率不高。

什麼是最有效的方法來做到這一點?

難道有人請給我解釋下面的代碼嗎?

private unsafe void CreateMotionZonesFrame() 
    { 
     lock (this) 
     { 
      // free previous motion zones frame 
      if (zonesFrame != null) 
      { 
       zonesFrame.Dispose(); 
       zonesFrame = null; 
      } 

      // create motion zones frame only in the case if the algorithm has processed at least one frame 
      if ((motionZones != null) && (motionZones.Length != 0) && (videoWidth != 0)) 
      { 
       zonesFrame = UnmanagedImage.Create(videoWidth, videoHeight, PixelFormat.Format8bppIndexed); 

       Rectangle imageRect = new Rectangle(0, 0, videoWidth, videoHeight); 

       // draw all motion zones on motion frame 
       foreach (Rectangle rect in motionZones) 
       { 
        //Please explain here 
        rect.Intersect(imageRect); 

        // rectangle's dimenstion 
        int rectWidth = rect.Width; 
        int rectHeight = rect.Height; 

        // start pointer 
        //Please explain here 
        int stride = zonesFrame.Stride; 

        //Please explain here 
        byte* ptr = (byte*) zonesFrame.ImageData.ToPointer() + rect.Y * stride + rect.X; 

        for (int y = 0; y < rectHeight; y++) 
        { 
         //Please explain here 
         AForge.SystemTools.SetUnmanagedMemory(ptr, 255, rectWidth); 
         ptr += stride; 
        } 
       } 
      } 
     } 
    } 

回答

1

什麼是最有效的方法呢? 只爲每個地區做。我不認爲會有明顯的性能損失(但我可能是錯的)

那麼,你所包圍的代碼執行以下操作:

1)檢查是否約束motionZones圖像是否創建 2)面具白色區域:

//Please explain here => if the motion region is out of bounds crop it to the image bounds 
rect.Intersect(imageRect); 

//Please explain here => gets the image stride (width step), the number of bytes per row; see: 
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa473780(v=vs.85).aspx 
int stride = zonesFrame.Stride; 

//Please explain here => gets the pointer of the first element in rectangle area 
byte* ptr = (byte*) zonesFrame.ImageData.ToPointer() + rect.Y * stride + rect.X; 

//mask the rectangle area with 255 value. If the image is color every pixel will have the //(255,255, 255) value which is white color 
for (int y = 0; y < rectHeight; y++) 
{ 
    //Please explain here 
    AForge.SystemTools.SetUnmanagedMemory(ptr, 255, rectWidth); 
    ptr += stride; 
}