Ejemplo n.º 1
0
        public static CaptureData ProcessFrame(CaptureCalc capCalc, CaptureData capData)
        {
            if (capData == null || capCalc == null)
                return new CaptureData();

            capCalc.CaptureC.Retrieve(capData.CapturedImage);

            capCalc.ForegroundDetectorC.Apply(capData.CapturedImage, capData.ForegroundImage);

            //update the motion history
            capCalc.MotionHistoryC.Update(capData.ForegroundImage);

            #region get a copy of the motion mask and enhance its color
            double[] minValues, maxValues;
            Point[] minLoc, maxLoc;
            capCalc.MotionHistoryC.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc);
            Mat motionMask = new Mat();
            using (ScalarArray sa = new ScalarArray(255.0 / maxValues[0]))
                CvInvoke.Multiply(capCalc.MotionHistoryC.Mask, sa, motionMask, 1, DepthType.Cv8U);
            //Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0 / maxValues[0]);
            #endregion

            //create the motion image
            //Mat motionImage = new Mat(motionMask.Size.Height, motionMask.Size.Width, DepthType.Cv8U, 3);
            capData.MotionImage = new Mat(motionMask.Size.Height, motionMask.Size.Width, DepthType.Cv8U, 3);

            //display the motion pixels in blue (first channel)
            //motionImage[0] = motionMask;
            CvInvoke.InsertChannel(motionMask, capData.MotionImage, (int)Constants.ColorChannel.Blue);

            //Threshold to define a motion area, reduce the value to detect smaller motion
            double minArea = Constants.MIN_DECTIVED_AREA;

            //storage.Clear(); //clear the storage
            //Rectangle[] rects;

            Mat segMask = new Mat();
            using (VectorOfRect boundingRect = new VectorOfRect())
            {
                capCalc.MotionHistoryC.GetMotionComponents(segMask, boundingRect);
                capData.CapturedMotionData.Rects = boundingRect.ToArray();
            }

            double[] angles = new double[capData.CapturedMotionData.Rects.Length];
            double[] motionPixelArr = new double[capData.CapturedMotionData.Rects.Length];
            uint index = 0;
            //iterate through each of the motion component
            foreach (Rectangle comp in capData.CapturedMotionData.Rects)
            {
                int area = comp.Width * comp.Height;
                //reject the components that have small area;
                if (area < minArea)
                    continue;

                // find the angle and motion pixel count of the specific area
                double angle = 0;
                double motionPixelCount = 0;
                capCalc.MotionHistoryC.MotionInfo(capData.ForegroundImage, comp, out angle, out motionPixelCount);

                //reject the area that contains too few motion
                if (motionPixelCount < area * Constants.MIN_MOTION_PIXEL_RATIO_IN_AN_AREA)
                    continue;

                angles[index] = angle;
                motionPixelArr[index] = motionPixelCount;
                index++;
                //Draw each individual motion in red
                DrawMotion(capData.MotionImage, comp, angle, new Bgr(Color.Red));
            }

            // find and draw the overall motion angle
            double overallAngle, overallMotionPixelCount;

            capCalc.MotionHistoryC.MotionInfo(capData.ForegroundImage, new Rectangle(Point.Empty, motionMask.Size), out overallAngle, out overallMotionPixelCount);
            capData.CapturedMotionData.OverallAngle = overallAngle;
            capData.CapturedMotionData.OverallMotionPixelCount = overallMotionPixelCount;
            capData.CapturedMotionData.Angles = angles;
            capData.CapturedMotionData.MotionPixelCountArray = motionPixelArr;

            DrawMotion(capData.MotionImage, new Rectangle(Point.Empty, motionMask.Size), capData.CapturedMotionData.OverallAngle, new Bgr(Color.Green));

            return capData;
        }
Ejemplo n.º 2
0
        private void UpdateDestImages(CaptureData capData)
        {
            capturedDest.Image = capData.CapturedImage;

            if(_isDebugging)
            {
                foregroundDest.Image = capData.ForegroundImage;

                //Display the amount of motions found on the current image
                UpdateText(String.Format("移动物体数: {0}; 移动像素数: {1}", capData.CapturedMotionData.Rects.Length, capData.CapturedMotionData.OverallMotionPixelCount));

                //Display the image of the motion
                motionDest.Image = capData.MotionImage;
            }

            if (this.Disposing || this.IsDisposed)
                return;
        }
Ejemplo n.º 3
0
        private void UpdateOrigImages(CaptureData capData)
        {
            capturedOrig.Image = capData.CapturedImage;

            if (_isDebugging)
            {
                foregroundOrig.Image = capData.ForegroundImage;

                //Display the amount of motions found on the current image
                //UpdateText(String.Format("Total Motions found: {0}; Motion Pixel count: {1}", capData.Rects.Length, capData.OverallMotionPixelCount));

                //Display the image of the motion
                motionOrig.Image = capData.MotionImage;
            }

            if (this.Disposing || this.IsDisposed)
                return;
        }
Ejemplo n.º 4
0
        private void startButton_Click(object sender, EventArgs e)
        {
            _destVideoFilePath = string.Empty;
            _log.Error("destVideo_Click");
            if (!chkUseCamera.Checked)
            {
                OpenFileDialog fileDialog = new OpenFileDialog
                {
                    Multiselect = false,
                    Title = "请选择文件",
                    Filter = "所有文件(*.*)|*.*"
                };

                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    _destVideoFilePath = fileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            _capCalcOrig = new CaptureCalc();
            _capCalcDest = new CaptureCalc();

            _capDataOrig = new CaptureData();
            _capDataDest = new CaptureData();

            _redis = ConnectionMultiplexer.Connect(_redisHost);
            if (_redis == null)
                return;

            _db = _redis.GetDatabase();

            if (_db == null)
                return;

            _db.StringSet(Constants.REDIS_KEY_CAPTURED_DATA, "");
            _db.StringSet(Constants.REDIS_KEY_OIRG_DATA, "");
            //RedisValue rv= _db.StringGet("CapturedData");
            //try to create the capture
            if (_capCalcDest.CaptureC == null)
            {
                try
                {
                    _capCalcDest.CaptureC = !string.IsNullOrEmpty(_destVideoFilePath) ? new Capture(_destVideoFilePath) : new Capture();
                }
                catch (NullReferenceException ex)
                {
                    //show errors if there is any
                    MessageBox.Show(ex.Message);
                }
            }
            if (_capCalcOrig.CaptureC == null)
            {
                try
                {
                    if (!string.IsNullOrEmpty(_origVideoFilePath))
                        _capCalcOrig.CaptureC = new Capture(_origVideoFilePath);
                }
                catch (NullReferenceException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            if (_capCalcDest.CaptureC != null) //if camera capture has been successfully created
            {
                _capCalcDest.ForegroundDetectorC = new BackgroundSubtractorMOG2();

                _capCalcDest.MotionHistoryC = new MotionHistory(
                    1.0, //in second, the duration of motion history you wants to keep
                    0.05, //in second, maxDelta for cvCalcMotionGradient
                    0.5); //in second, minDelta for cvCalcMotionGradient

                _capCalcDest.CaptureC.ImageGrabbed += ProcessCapturedFrame;
                _capCalcDest.CaptureC.Start();
            }

            if (_capCalcOrig.CaptureC != null) //if camera capture has been successfully created
            {
                _capCalcOrig.ForegroundDetectorC = new BackgroundSubtractorMOG2();

                _capCalcOrig.MotionHistoryC = new MotionHistory(
                    1.0, //in second, the duration of motion history you wants to keep
                    0.05, //in second, maxDelta for cvCalcMotionGradient
                    0.5); //in second, minDelta for cvCalcMotionGradient

                _capCalcOrig.CaptureC.ImageGrabbed += ProcessComparedVideoFrame;
                _capCalcOrig.CaptureC.Start();
            }
        }