Accord.Vision.Motion.MotionDetector.ProcessFrame C# (CSharp) Method

ProcessFrame() public method

Process new video frame.

The method first of all applies motion detection algorithm to the specified video frame to calculate motion level and motion frame. After this it applies motion processing algorithm (if it was set) to do further post processing, like highlighting motion areas, counting moving objects, etc.

In the case if MotionZones property is set, this method will perform motion filtering right after motion algorithm is done and before passing motion frame to motion processing algorithm. The method does filtering right on the motion frame, which is produced by motion detection algorithm. At the same time the method recalculates motion level and returns new value, which takes motion zones into account (but the new value is not set back to motion detection algorithm' IMotionDetector.MotionLevel property).

public ProcessFrame ( UnmanagedImage videoFrame ) : float
videoFrame Accord.Imaging.UnmanagedImage Video frame to process (detect motion in).
return float
        public float ProcessFrame( UnmanagedImage videoFrame )
        {
            lock ( sync )
            {
                if ( detector == null )
                    return 0;

                videoWidth  = videoFrame.Width;
                videoHeight = videoFrame.Height;

                float motionLevel = 0;
                // call motion detection
                detector.ProcessFrame( videoFrame );
                motionLevel = detector.MotionLevel;

                // check if motion zones are specified
                if ( motionZones != null )
                {
                    if ( zonesFrame == null )
                    {
                        CreateMotionZonesFrame( );
                    }

                    if ( ( videoWidth == zonesFrame.Width ) && ( videoHeight == zonesFrame.Height ) )
                    {
                        unsafe
                        {
                            // pointers to background and current frames
                            byte* zonesPtr  = (byte*) zonesFrame.ImageData.ToPointer( );
                            byte* motionPtr = (byte*) detector.MotionFrame.ImageData.ToPointer( );

                            motionLevel = 0;

                            for ( int i = 0, frameSize = zonesFrame.Stride * videoHeight; i < frameSize; i++, zonesPtr++, motionPtr++ )
                            {
                                *motionPtr &= *zonesPtr;
                                motionLevel += ( *motionPtr & 1 );
                            }
                            motionLevel /= ( videoWidth * videoHeight );
                        }
                    }
                }

                // call motion post processing
                if ( ( processor != null ) && ( detector.MotionFrame != null ) )
                {
                    processor.ProcessFrame( videoFrame, detector.MotionFrame );
                }

                return motionLevel;
            }
        }

Same methods

MotionDetector::ProcessFrame ( Bitmap videoFrame ) : float
MotionDetector::ProcessFrame ( BitmapData videoFrame ) : float