Beispiel #1
0
        protected void ProcessFrame(SpeedometerFrame frame)
        {
            SpeedMeasurement result        = speedMeasurer.Measure(frame);
            decimal          PixelsPerHour = result == null ? 0 : result.SpeedPPH;
            decimal          MetersPerHour = settings.PixelsPerMeter <= 0 ? 0 : PixelsPerHour / settings.PixelsPerMeter;

            SpeedKph = MetersPerHour / 1000;

            SpeedUpdated(this, SpeedKph);
            if (lastReportedSpeedKPH == null || SpeedKph != lastReportedSpeedKPH)
            {
                lastReportedSpeedKPH = SpeedKph;
                SpeedChanged(this, SpeedKph);
            }
        }
        /// <summary>
        /// Returns the speed of the largest moving object between this frame and the last. May return null.
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public SpeedMeasurement Measure(SpeedometerFrame frame)
        {
            float      motion;
            BitmapData data = frame.Frame.LockBits(ImageLockMode.ReadWrite);

            try
            {
                using (UnmanagedImage unmanagedImage = new UnmanagedImage(data))
                    motion = detector.ProcessFrame(unmanagedImage);
            }
            finally
            {
                frame.Frame.UnlockBits(data);
            }

            Rectangle?       largest = GetLargestRectangle(blobCounter.ObjectRectangles);
            SpeedMeasurement result  = null;

            if (largest != null && lastLargest != null)
            {
                decimal distLeftPx  = largest.Value.Left - lastLargest.Value.Left;
                decimal distRightPx = largest.Value.Right - lastLargest.Value.Right;

                decimal absDistLeftPx  = Math.Abs(distLeftPx);
                decimal absDistRightPx = Math.Abs(distRightPx);
                decimal greater        = absDistLeftPx > absDistRightPx ? distLeftPx : distRightPx;
                if (greater != 0)
                {
                    result = new SpeedMeasurement(greater > 0 ? MovementDirection.Left : MovementDirection.Right, Math.Abs(greater), frame.Timestamp - lastFrameTime.Value);
                }
                else
                {
                    decimal dx = largest.Value.Center().X - lastLargest.Value.Center().X;
                    if (dx > 0)
                    {
                        result = new SpeedMeasurement(MovementDirection.Right, dx, frame.Timestamp - lastFrameTime.Value);
                    }
                    else if (dx < 0)
                    {
                        result = new SpeedMeasurement(MovementDirection.Left, -dx, frame.Timestamp - lastFrameTime.Value);
                    }
                }
            }
            lastFrameTime = frame.Timestamp;
            lastLargest   = largest;
            return(result);
        }