public ForegroundDetector(BlobImage image)
 {
     Finished = true;
     Stop = false;
     _foregroundDetector = new BackgroundSubtractorMOG2(200, 400, false);
     _bImage = image;
 }
示例#2
0
 public ObjectTracker(
     TrackerSettings settings)
 {
     _settings = settings;
     _foregroundDetector = new BackgroundSubtractorMOG2(_settings.BackgroundSubtractorHistory.Value, _settings.BackgroundSubtractorMaxComponents.Value, false);
     _blobDetector = new CvBlobDetector();
     _blobs = new CvBlobs();
     _tracks = new CvTracks();
     _trackedObjectIdentities = new Dictionary<uint, TrackedObject>();
 }
示例#3
0
 public void Dispose()
 {
     if (_foregroundDetector == null) return;
     try
     {
         _blobDetector.Dispose();
         _blobs.Dispose();
         _tracks.Dispose();
         ((IDisposable)_foregroundDetector).Dispose();
     }
     catch (Exception ex)
     {
         Log.Error("Exception disposing foreground detector", ex);
     }
     _blobDetector = null;
     _blobs = null;
     _tracks = null;
     _foregroundDetector = null;
 }
        private MotionInfo GetMotionInfo(Mat image)
        {
            Mat _forgroundMask = new Mat();
            Mat _segMask = new Mat();
            MotionInfo motionInfoObj = new MotionInfo();
            double minArea, angle, objectCount, totalPixelCount;
            double overallangle = 0;
            double  motionPixelCount =0;
            int motionArea =0;
            totalPixelCount = 0;
            objectCount = 0;
            minArea = 800;

            if (foregroundDetector == null)
            {
                foregroundDetector = new BackgroundSubtractorMOG2();
            }

            foregroundDetector.Apply(image, _forgroundMask);

            _motionHistory.Update(_forgroundMask);

            ImageForeGroundMaskLast = _forgroundMask.ToImage<Bgr, byte>();

            #region get a copy of the motion mask and enhance its color
            double[] minValues, maxValues;
            Point[] minLoc, maxLoc;
            _motionHistory.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(_motionHistory.Mask, sa, motionMask, 1, DepthType.Cv8U);
            //Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0 / maxValues[0]);
            #endregion

            //create the motion image
            Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMask.Size);
            //display the motion pixels in blue (first channel)
            //motionImage[0] = motionMask;
            CvInvoke.InsertChannel(motionMask, motionImage, 0);

            //Threshold to define a motion area, reduce the value to detect smaller motion
            minArea = 100;
             //storage.Clear(); //clear the storage
             Rectangle[] rects;

             using (VectorOfRect boundingRect = new VectorOfRect())
             {
             _motionHistory.GetMotionComponents(_segMask, boundingRect);
             rects = boundingRect.ToArray();
             }

             //iterate through each of the motion component
             foreach (Rectangle comp in rects)
             {
            int area = comp.Width * comp.Height;
            //reject the components that have small area;
            _motionHistory.MotionInfo(_forgroundMask, comp, out angle, out motionPixelCount);
            if (area < minArea) continue;
            else
            {
                overallangle = overallangle + angle;
                totalPixelCount = totalPixelCount + motionPixelCount;
                objectCount = objectCount + 1;
                motionArea = motionArea + area;
            }

            // find the angle and motion pixel count of the specific area

            ////Draw each individual motion in red
            //DrawMotion(motionImage, comp, angle, new Bgr(Color.Red));
             }
            motionInfoObj.MotionArea = motionArea;
            motionInfoObj.OverallAngle = overallangle;
            motionInfoObj.BoundingRect = rects;
            motionInfoObj.TotalMotions = rects.Length;
            motionInfoObj.MotionObjects = objectCount;
            motionInfoObj.MotionPixels = totalPixelCount;
            averagetotalPixelCount = 0.75 * averagetotalPixelCount + 0.25 * totalPixelCount;
            if ( Math.Abs(averagetotalPixelCount - totalPixelCount) / averagetotalPixelCount > 0.59)
                Console.WriteLine(" GetMotionInfo - Total Motions found: " + rects.Length + "; Motion Pixel count: " + totalPixelCount);
             return motionInfoObj;
        }
示例#5
0
      private void ProcessFrame(object sender, EventArgs e)
      {
         Mat image = new Mat();

         _capture.Retrieve(image);
         if (_forgroundDetector == null)
         {
            _forgroundDetector = new BackgroundSubtractorMOG2();
         }

         _forgroundDetector.Apply(image, _forgroundMask);

         //update the motion history
         _motionHistory.Update(_forgroundMask);         

         #region get a copy of the motion mask and enhance its color
         double[] minValues, maxValues;
         Point[] minLoc, maxLoc;
         _motionHistory.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(_motionHistory.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);
         //display the motion pixels in blue (first channel)
         //motionImage[0] = motionMask;
         CvInvoke.InsertChannel(motionMask, motionImage, 0);

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

         //storage.Clear(); //clear the storage
         Rectangle[] rects;
         using (VectorOfRect boundingRect = new VectorOfRect())
         {
            _motionHistory.GetMotionComponents(_segMask, boundingRect);
            rects = boundingRect.ToArray();
         }

         //iterate through each of the motion component
         foreach (Rectangle comp in 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, motionPixelCount;
            _motionHistory.MotionInfo(_forgroundMask, comp, out angle, out motionPixelCount);

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

            //Draw each individual motion in red
            DrawMotion(motionImage, comp, angle, new Bgr(Color.Red));
         }

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

         _motionHistory.MotionInfo(_forgroundMask, new Rectangle(Point.Empty, motionMask.Size), out overallAngle, out overallMotionPixelCount);
         DrawMotion(motionImage, new Rectangle(Point.Empty, motionMask.Size), overallAngle, new Bgr(Color.Green));

         if (this.Disposing || this.IsDisposed)
            return;

         capturedImageBox.Image = image;
         forgroundImageBox.Image = _forgroundMask;

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

         //Display the image of the motion
         motionImageBox.Image = motionImage;

      }
示例#6
0
        private async void ProcessFrame(object sender, EventArgs e)
        {
            List<BsonDocument> tempList = new List<BsonDocument>();
            Mat image = new Mat();
            _capture.Retrieve(image);
            if (_forgroundDetector == null)
            {
                _forgroundDetector = new BackgroundSubtractorMOG2();
            }

            _forgroundDetector.Apply(image, _forgroundMask);

            //update the motion history
            _motionHistory.Update(_forgroundMask);

            #region get a copy of the motion mask and enhance its color
            double[] minValues, maxValues;
            Point[] minLoc, maxLoc;
            _motionHistory.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(_motionHistory.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);
            //display the motion pixels in blue (first channel)
            //motionImage[0] = motionMask;
            CvInvoke.InsertChannel(motionMask, motionImage, 0);

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

            //storage.Clear(); //clear the storage
            System.Drawing.Rectangle[] rects;
            using (VectorOfRect boundingRect = new VectorOfRect())
            {
                _motionHistory.GetMotionComponents(_segMask, boundingRect);
                rects = boundingRect.ToArray();
            }

            int motionCount = 0;
            //iterate through each of the motion component
            foreach (System.Drawing.Rectangle comp in rects)
            {
                int area = comp.Width * comp.Height;

                //reject the components that have small area;
                if (area < minArea) continue;

                if (!CheckArea(comp)) continue;

                //find center point
                Point center = new Point(comp.X + (comp.Width >> 1), comp.Y + (comp.Height >> 1));

                //insert to temp motion list
                var document = new BsonDocument
                {
                    {"Source", Path.GetFileName(videoSource)},
                    {"Time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
                    {"Area", GetAreaCode(center).ToString()},
                    {"AreaX", center.X.ToString()},
                    {"AreaY", center.Y.ToString()},
                    {"MotionCout", 1 }
                };
                tempList.Add(document);

                //create heatmap
                myHeatMap.heatPoints.Add(new HeatPoint(center.X, center.Y, 16));

                // find the angle and motion pixel count of the specific area
                double angle, motionPixelCount;
                _motionHistory.MotionInfo(_forgroundMask, comp, out angle, out motionPixelCount);

                //reject the area that contains too few motion 0.05
                if (motionPixelCount < area * 0.1) continue;

                //Draw each individual motion in red
                DrawMotion(motionImage, comp, angle, new Bgr(Color.Red));
                motionCount++;
            }

            //pictureBox3.BackgroundImage = myHeatMap.CreateHeatmap();
            Bitmap b = (Bitmap)myHeatMap.CreateHeatmap();
            //Image<Gray, Byte> normalizedMasterImage = new Image<Gray, Byte>(b);
            //motionImage = normalizedMasterImage.Mat;
            pictureBox3.BackgroundImage = b;


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

            _motionHistory.MotionInfo(_forgroundMask, new System.Drawing.Rectangle(Point.Empty, motionMask.Size), out overallAngle, out overallMotionPixelCount);
            DrawMotion(motionImage, new System.Drawing.Rectangle(Point.Empty, motionMask.Size), overallAngle, new Bgr(Color.Green));

            if (this.Disposing || this.IsDisposed)
            {
                return;
            }

            //find pedestrian
            //bool tryUseCuda = true;
            //bool tryuseOpenCL = false;
            //long processingTime;
            //System.Drawing.Rectangle[] pedestrianRestult = FindPedestrian.Find(image, tryUseCuda, tryuseOpenCL, out processingTime);
            //foreach (System.Drawing.Rectangle rect in pedestrianRestult)
            //{
            //    CvInvoke.Rectangle(image, rect, new Bgr(Color.Gold).MCvScalar);
            //}

            capturedImageBox.Image = image;
            //forgroundImageBox.Image = _forgroundMask;

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

            //write into db
            if (checkTimer)
            {
                foreach(var doc in tempList)
                {
                    await DataAccess.Insert(doc);
                }             
            }

            motionCount = 0;
            //Display the image of the motion
            motionImageBox.Image = motionImage;         
      }
示例#7
0
        /// <summary>
        /// Motion Detector Constructor
        /// </summary>
        /// <param name="imagingData">Common Image Processing Imaging Data</param>
        public MotionDetector(IImageData imagingData )
        {
            ImagingData = imagingData;

            //Set values for properties
            MinMotionAreaThreshold = DEFAULT_MIN_MOTION_AREA_THRESHOLD;
            MinMotionPixelFactor = DEFAULT_MIN_MOTION_PIXEL_FACTOR;
            GrayThreshold = DEFAULT_GRAY_THRESHOLD;

            //Instantiate private members
            _motionHistory = new MotionHistory(MOTION_HISTORY_DURATION, MOTION_HISTORY_MAX_DELTA, MOTION_HISTORY_MIN_DELTA);
            _forgroundDetector = new BackgroundSubtractorMOG2();
            _segMask = new Mat();
            _foreground = new Mat();

            ComputerVisionMonitors = new Dictionary<eComputerVisionMonitor, IComputerVisionMonitor>();
        }
示例#8
-1
      void Run()
      {
         try
         {
            _cameraCapture = new VideoCapture();
         }
         catch (Exception e)
         {
            MessageBox.Show(e.Message);
            return;
         }

         _fgDetector = new Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
         _blobDetector = new CvBlobDetector();
         _tracker = new CvTracks();

         Application.Idle += ProcessFrame;
      }