Pregunta

I am using AForge for motion detection and I know that motion regions can be set. Is it possible to make that it triggers only when there are motions in ALL the defined regions? If the above functionality is not readily available, I am thinking of writing it.

Currently, my understanding is that the regions are set to the zoneFrame in the MotionDetector.cs in Vision Library. I am thinking of doing this for each region but it seems not efficient.

What is the most efficient way to do this?

Could someone please explain me the below code?

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;
                    }
                }
            }
        }
    }
¿Fue útil?

Solución

What is the most efficient way to do this? just do for each region. I do not think there will be noticeable performance penalty (but I could be wrong)

Well, the code you enclosed does the following:

1) checks whether the constraints whether the motionZones image is created 2) masks regions with white color:

//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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top