Aforge.Net的运动检测区域

时间:2011-08-14 16:07:00

标签: c# opencv computer-vision motion-detection aforge

我正在使用AForge进行运动检测,我知道可以设置运动区域。只有在所有定义的区域中有动作时才可以触发它吗? 如果上述功能不易获得,我正在考虑编写它。

目前,我的理解是区域设置为Vision Library中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 个答案:

答案 0 :(得分: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;
}