示例#1
0
 public void ApplyOverlay(UnmanagedImage videoFrame)
 {
     if ((_processor != null) && (_detector?.MotionFrame != null))
     {
         _processor.ProcessFrame(videoFrame, _detector.MotionFrame);
     }
 }
示例#2
0
        /// <summary>
        /// Process new video frame.
        /// </summary>
        ///
        /// <param name="videoFrame">Video frame to process (detect motion in).</param>
        ///
        /// <returns>Returns amount of motion, which is provided <see cref="IMotionDetector.MotionLevel"/>
        /// property of the <see cref="MotionDetectionAlgorithm">motion detection algorithm in use</see>.</returns>
        ///
        /// <remarks><para>The method first of all applies motion detection algorithm to the specified video
        /// frame to calculate <see cref="IMotionDetector.MotionLevel">motion level</see> and
        /// <see cref="IMotionDetector.MotionFrame">motion frame</see>. After this it applies motion processing algorithm
        /// (if it was set) to do further post processing, like highlighting motion areas, counting moving
        /// objects, etc.</para>
        ///
        /// <para><note>In the case if <see cref="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' <see cref="IMotionDetector.MotionLevel"/> property).
        /// </note></para>
        /// </remarks>
        ///
        public float ProcessFrame(UnmanagedImage videoFrame)
        {
            lock ( _sync )
            {
                if (_detector == null)
                {
                    return(0);
                }

                _videoWidth  = videoFrame.Width;
                _videoHeight = videoFrame.Height;

                if (_area == 0)
                {
                    _area = _videoWidth * _videoHeight;
                }

                // call motion detection
                _detector.ProcessFrame(videoFrame);
                var motionLevel = _detector.MotionLevel;

                // check if motion zones are specified
                if (_detector.MotionFrame != null && _motionZones != null)
                {
                    if (_zonesFrame == null)
                    {
                        CreateMotionZonesFrame();
                    }

                    if (_zonesFrame != null && (_videoWidth == _zonesFrame.Width) && (_videoHeight == _zonesFrame.Height))
                    {
                        unsafe
                        {
                            // pointers to background and current frames
                            var zonesPtr  = (byte *)_zonesFrame.ImageData.ToPointer();
                            var 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 /= _area;
                        }
                    }
                }

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

                return(motionLevel);
            }
        }
示例#3
0
        /// <summary>
        /// Process new video frame.
        /// </summary>
        ///
        /// <param name="videoFrame">Video frame to process (detect motion in).</param>
        ///
        /// <returns>Returns amount of motion, which is provided <see cref="IMotionDetector.MotionLevel"/>
        /// property of the <see cref="MotionDetectionAlgorithm">motion detection algorithm in use</see>.</returns>
        ///
        /// <remarks><para>The method first of all applies motion detection algorithm to the specified video
        /// frame to calculate <see cref="IMotionDetector.MotionLevel">motion level</see> and
        /// <see cref="IMotionDetector.MotionFrame">motion frame</see>. After this it applies motion processing algorithm
        /// (if it was set) to do further post processing, like highlighting motion areas, counting moving
        /// objects, etc.</para>
        ///
        /// <para><note>In the case if <see cref="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' <see cref="IMotionDetector.MotionLevel"/> property).
        /// </note></para>
        /// </remarks>
        ///
        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);
            }
        }
        public float ProcessFrame(UnmanagedImage videoFrame)
        {
            lock (sync)
            {
                if (detector == null)
                {
                    return(0);
                }

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

                float motionLevel = 0;

                detector.ProcessFrame(videoFrame);
                motionLevel = detector.MotionLevel;

                if (motionZones != null)
                {
                    if (zonesFrame == null)
                    {
                        CreateMotionZonesFrame();
                    }

                    if ((videoWidth == zonesFrame.Width) && (videoHeight == zonesFrame.Height))
                    {
                        unsafe
                        {
                            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);
                        }
                    }
                }

                if ((processor != null) && (detector.MotionFrame != null))
                {
                    processor.ProcessFrame(videoFrame, detector.MotionFrame);
                }

                return(motionLevel);
            }
        }