Ejemplo n.º 1
5
        public void FilterTiles(Mat image, Mat modifiedMat)
        {
            CvInvoke.Imshow("0", image);
            
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            Mat laplaced = new Mat();
            CvInvoke.CvtColor(image, laplaced, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
            Mat greyResult = laplaced.Clone();
            Mat greySource = laplaced.Clone();

            Mat cannySrc = new Mat();

            //if not half inch, do canny and subtract to separate tiles better. Basically "sharpens" the edge
            if (scan.TileSettings.CannyEdges)
            {
                //create canny image, these parameters could be adjusted probably?
                CvInvoke.Canny(greySource, greyResult, 50, 150);
                //dilate canny                

                CvInvoke.Dilate(greyResult, greyResult, null, new System.Drawing.Point(1, 1), scan.TileSettings.CannyDilate, BorderType.Default, CvInvoke.MorphologyDefaultBorderValue);
                CvInvoke.Erode(greyResult, greyResult, null, new System.Drawing.Point(1, 1), scan.TileSettings.CannyDilate, BorderType.Default, CvInvoke.MorphologyDefaultBorderValue);
                
                CvInvoke.Imshow("1a", greyResult);

                //subtract dilated canny from source to get separation
                CvInvoke.Subtract(greySource, greyResult, greyResult);
                greySource = greyResult.Clone();
                CvInvoke.Imshow("1b", greyResult);
            }

            if (scan.TileSettings.ThresholdEdges)
            {
                Mat edges = new Mat();
                CvInvoke.Threshold(greyResult, edges, (float)thresholdTrackbar.Value, 0, ThresholdType.ToZero);
                CvInvoke.Subtract(greySource, edges, greyResult);
                CvInvoke.Erode(greyResult, greyResult, null, new System.Drawing.Point(1, 1), 2, BorderType.Default, CvInvoke.MorphologyDefaultBorderValue);
                CvInvoke.Imshow("pres-1c", greyResult);
             }
            //perform distance transform
            CvInvoke.DistanceTransform(greyResult, greyResult, null, DistType.L2, 5);
            //normalize the image to bring out the peaks
            CvInvoke.Normalize(greyResult, greyResult, 0, 1, NormType.MinMax);
            CvInvoke.Imshow("2", greyResult);

            //threshold the image, different thresholds for different tiles

            CvInvoke.Threshold(greyResult, greyResult, scan.TileSettings.ThresholdVal, 1, ThresholdType.Binary);

            CvInvoke.Imshow("3", greyResult);

            //erode to split the blobs
            CvInvoke.Erode(greyResult, greyResult, null, new System.Drawing.Point(-1, -1), scan.TileSettings.ThresholdErode, BorderType.Default, CvInvoke.MorphologyDefaultBorderValue);

            //convert to 8 bit unsigned needed for canny
            greyResult.ConvertTo(greyResult, DepthType.Cv8U);

            VectorOfVectorOfPoint markers = new VectorOfVectorOfPoint();

            //create 32bit, single channel image for result of markers
            Mat markerImage = new Mat(greyResult.Size, DepthType.Cv32S, 1);

            //set image to 0
            markerImage.SetTo(new MCvScalar(0, 0, 0));

            //find the contours
            CvInvoke.FindContours(greyResult, markers, null, RetrType.External, ChainApproxMethod.LinkRuns);

            //label the markers from 1 -> n, the rest of the image should remain 0
            for (int i = 0; i < markers.Size; i++)
                CvInvoke.DrawContours(markerImage, markers, i, new MCvScalar(i + 1, i + 1, i + 1), -1);

            ScalarArray mult = new ScalarArray(5000);
            Mat markerVisual = new Mat();

            CvInvoke.Multiply(markerImage, mult, markerVisual);

            CvInvoke.Imshow("4", markerVisual);

            //draw the background marker
            CvInvoke.Circle(markerImage,
                new System.Drawing.Point(5, 5),
                3,
                new MCvScalar(255, 255, 255),
                -1);

            //convert to 3 channel
            Mat convertedOriginal = new Mat();
            
            //use canny modified if 3/4", or use the gray image for others

            CvInvoke.CvtColor(greySource, convertedOriginal, ColorConversion.Gray2Bgr);

            //watershed!!
            CvInvoke.Watershed(convertedOriginal, markerImage);
            //visualize
            CvInvoke.Multiply(markerImage, mult, markerVisual);
            CvInvoke.Imshow("5", markerVisual);

            //get contours to get the actual tiles now that they are separate...
            VectorOfVectorOfPoint tilesContours = new VectorOfVectorOfPoint();

            markerVisual.ConvertTo(markerVisual, DepthType.Cv8U);
          
            CvInvoke.BitwiseNot(markerVisual, markerVisual);
            CvInvoke.Imshow("6", markerVisual);
            CvInvoke.Dilate(markerVisual, markerVisual, null, new System.Drawing.Point(1, 1), 2, BorderType.Default, CvInvoke.MorphologyDefaultBorderValue);

            CvInvoke.FindContours(markerVisual, tilesContours, null, RetrType.External, ChainApproxMethod.LinkRuns);
            
            List<System.Drawing.Point> tiles = new List<System.Drawing.Point>();
            for (int i = 0; i < tilesContours.Size; i++)
            {
                using(VectorOfPoint c = tilesContours[i])
                using (VectorOfPoint approx = new VectorOfPoint())
                {
                    //epsilon = arclength * .05 to get rid of convex areas
                    CvInvoke.ApproxPolyDP(c, approx, CvInvoke.ArcLength(c, true) * .05, true);
                    double area = CvInvoke.ContourArea(approx);
                  
                    //filter out the small contours...
                    if (area > scan.TileSettings.MinArea && area < scan.TileSettings.MaxArea)
                    {
                        //match the shape to the square
                        double ratio = CvInvoke.MatchShapes(_square, approx, Emgu.CV.CvEnum.ContoursMatchType.I3);

                        if (ratio < .05)
                        {
                            var M = CvInvoke.Moments(c);
                            int cx = (int)(M.M10 / M.M00);
                            int cy = (int)(M.M01 / M.M00);

                            //filter out any that are too close 
                            if (!tiles.Any(x => Math.Abs(x.X - cx) < 50 && Math.Abs(x.Y - cy) < 50))
                            {
                                tiles.Add(new System.Drawing.Point(cx, cy));
                                for (int j = 0; j < approx.Size; j++)
                                {
                                    int second = j+1 == approx.Size ? 0 : j + 1;

                                    //do some detection for upsidedown/right side up here....

                                    CvInvoke.Line(image,
                                        new System.Drawing.Point(approx[j].X, approx[j].Y),
                                        new System.Drawing.Point(approx[second].X, approx[second].Y),
                                        new MCvScalar(255, 255, 255,255), 4);
                                }
                            }
                        }
                    }
                }
            }
            sw1.Stop();

            dataTextBox.AppendText(String.Format("Took {0} ms to detect {1} tiles{2}", sw1.ElapsedMilliseconds, tiles.Count, Environment.NewLine));

       //     dataTextBox.AppendText(String.Format("Found {0} tiles{1}", tiles.Count, Environment.NewLine));
         
            this.originalBox.Image = image;
            resultBox.Image = markerVisual;
        }
Ejemplo n.º 2
0
      /// <summary>
      /// Compute the red pixel mask for the given image. 
      /// A red pixel is a pixel where:  20 &lt; hue &lt; 160 AND saturation &gt; 10
      /// </summary>
      /// <param name="image">The color image to find red mask from</param>
      /// <param name="mask">The red pixel mask</param>
      private static void GetRedPixelMask(IInputArray image, IInputOutputArray mask)
      {
         bool useUMat;
         using (InputOutputArray ia = mask.GetInputOutputArray())
            useUMat = ia.IsUMat;

         using (IImage hsv = useUMat ? (IImage)new UMat() : (IImage)new Mat())
         using (IImage s = useUMat ? (IImage)new UMat() : (IImage)new Mat())
         {
            CvInvoke.CvtColor(image, hsv, ColorConversion.Bgr2Hsv);
            CvInvoke.ExtractChannel(hsv, mask, 0);
            CvInvoke.ExtractChannel(hsv, s, 1);

            //the mask for hue less than 20 or larger than 160
            using (ScalarArray lower = new ScalarArray(20))
            using (ScalarArray upper = new ScalarArray(160))
               CvInvoke.InRange(mask, lower, upper, mask);
            CvInvoke.BitwiseNot(mask, mask);

            //s is the mask for saturation of at least 10, this is mainly used to filter out white pixels
            CvInvoke.Threshold(s, s, 10, 255, ThresholdType.Binary);
            CvInvoke.BitwiseAnd(mask, s, mask, null);

         }
      }
Ejemplo n.º 3
0
      public void TestMatToFileStorage()
      {
         //create a matrix m with random values
         Mat m = new Mat(120, 240, DepthType.Cv8U, 1);
         using (ScalarArray low = new ScalarArray(0))
         using (ScalarArray high = new ScalarArray(255))
         CvInvoke.Randu(m, low, high);

         //Convert the random matrix m to yml format, good for matrix that contains values such as calibration, homography etc.
         String mStr;
         using (FileStorage fs = new FileStorage(".yml", FileStorage.Mode.Write | FileStorage.Mode.Memory))
         {
            fs.Write(m, "m");
            mStr = fs.ReleaseAndGetString();
         }

         //Treat the Mat as image data and convert it to png format.
         using (VectorOfByte bytes = new VectorOfByte())
         {
            CvInvoke.Imencode(".png", m, bytes);

            byte[] rawData =  bytes.ToArray();
         }


      }
Ejemplo n.º 4
0
      public void TestFileStorage2()
      {
         Mat m = new Mat(40, 30, DepthType.Cv8U, 3);
         
         using (ScalarArray lower = new ScalarArray(new MCvScalar(0, 0, 0)))
         using (ScalarArray higher = new ScalarArray(new MCvScalar(255, 255, 255)))
            CvInvoke.Randu(m, lower, higher );

         int intValue = 10;
         float floatValue = 213.993f;
         double doubleValue = 32.314;

         using (FileStorage fs = new FileStorage(".xml", FileStorage.Mode.Write | FileStorage.Mode.Memory))
         {
            fs.Write(m, "m");
            fs.Write(intValue, "int");
            fs.Write(floatValue, "float");
            fs.Write(doubleValue, "double");
            string s = fs.ReleaseAndGetString();

            using (FileStorage fs2 = new FileStorage(s, FileStorage.Mode.Read | FileStorage.Mode.Memory))
            {
               
               using (FileNode node = fs2.GetFirstTopLevelNode())
               {
                  Mat m2 = new Mat();
                  node.ReadMat(m2);
                  EmguAssert.IsTrue(m.Equals(m2));
               }

               using (FileNode node = fs2.GetNode("m"))
               {
                  Mat m2 = new Mat();
                  node.ReadMat(m2);
                  EmguAssert.IsTrue(m.Equals(m2));
               }

               using (FileNode node = fs2.GetNode("int"))
               {
                  EmguAssert.IsTrue(intValue.Equals(node.ReadInt()));   
               }

               using (FileNode node = fs2.GetNode("float"))
               {
                  EmguAssert.IsTrue(floatValue.Equals(node.ReadFloat()));
               }

               using (FileNode node = fs2.GetNode("double"))
               {
                  EmguAssert.IsTrue(doubleValue.Equals(node.ReadDouble()));
               }
            }
         }
      }
Ejemplo n.º 5
0
      public void TestGrabCut2()
      {
         Image<Bgr, Byte> img = EmguAssert.LoadImage<Bgr, Byte>("pedestrian.png");
         HOGDescriptor desc = new HOGDescriptor();
         desc.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());

         MCvObjectDetection[] humanRegions = desc.DetectMultiScale(img);

         Image<Gray, byte> pedestrianMask = new Image<Gray, byte>(img.Size);
         foreach (MCvObjectDetection rect in humanRegions)
         {
            //generate the mask where 3 indicates foreground and 2 indicates background 
            using (Image<Gray, byte> mask = img.GrabCut(rect.Rect, 2))
            {
               //get the mask of the foreground
               using (ScalarArray ia = new ScalarArray(3))
                  CvInvoke.Compare(mask, ia, mask, Emgu.CV.CvEnum.CmpType.Equal);

               pedestrianMask._Or(mask);
            }
         }
      }
Ejemplo n.º 6
0
      public void TestGrabCut1()
      {
         Image<Bgr, Byte> img = EmguAssert.LoadImage<Bgr, Byte>("lena.jpg");

         Rectangle rect = new Rectangle(new Point(50, 50), new Size(400, 400));
         Matrix<double> bgdModel = new Matrix<double>(1, 13 * 5);
         Matrix<double> fgdModel = new Matrix<double>(1, 13 * 5);
         Image<Gray, byte> mask = new Image<Gray, byte>(img.Size);

         CvInvoke.GrabCut(img, mask, rect, bgdModel, fgdModel, 0, Emgu.CV.CvEnum.GrabcutInitType.InitWithRect);
         CvInvoke.GrabCut(img, mask, rect, bgdModel, fgdModel, 2, Emgu.CV.CvEnum.GrabcutInitType.Eval);
         using (ScalarArray ia = new ScalarArray(3))
            CvInvoke.Compare(mask, ia, mask, CvEnum.CmpType.Equal);
         //Emgu.CV.UI.ImageViewer.Show(img.ConcateHorizontal( mask.Convert<Bgr, Byte>()));
      }
Ejemplo n.º 7
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.º 8
0
 public void SetRandNormal(MCvScalar mean, MCvScalar std)
 {
     using (ScalarArray saMean = new ScalarArray(mean))
         using (ScalarArray saStd = new ScalarArray(std))
             CvInvoke.Randn(this.Mat, saMean, saStd);
 }
Ejemplo n.º 9
0
      public void TestCompare()
      {
         Matrix<float> f1 = new Matrix<float>(1, 380);
         f1.SetValue(0.8);
         Matrix<float> f2 = new Matrix<float>(f1.Size);
         f2.SetValue(1.0);
         Matrix<byte> mask1 = new Matrix<byte>(f1.Size);
         CvInvoke.Compare(f1, f2, mask1, CvEnum.CmpType.LessEqual);
         int total1 = CvInvoke.CountNonZero(mask1);

         EmguAssert.IsTrue(total1 == f1.Width * f1.Height);

         Matrix<Byte> mask2 = new Matrix<byte>(f1.Size);
         using (ScalarArray ia = new ScalarArray(1.0))
         {
            CvInvoke.Compare(f1, ia, mask2, CvEnum.CmpType.LessEqual);
            int total2 = CvInvoke.CountNonZero(mask2);
            EmguAssert.IsTrue(total1 == total2);
         }
      }
Ejemplo n.º 10
0
        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;
        }
Ejemplo n.º 11
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;

      }
Ejemplo n.º 12
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;         
      }
Ejemplo n.º 13
0
 public void _Max(double value)
 {
     using (ScalarArray ia = new ScalarArray(value))
         CvInvoke.Max(this, ia, this);
 }
Ejemplo n.º 14
0
Archivo: UMat.cs Proyecto: aray2000/TFM
 /// <summary>
 /// Sets all or some of the array elements to the specified value.
 /// </summary>
 /// <param name="value">Assigned scalar value.</param>
 /// <param name="mask">Operation mask of the same size as the umat.</param>
 public void SetTo(MCvScalar value, IInputArray mask = null)
 {
     using (ScalarArray ia = new ScalarArray(value))
         SetTo(ia, mask);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Fills the array with normally distributed random numbers.
 /// </summary>
 /// <param name="dst">Output array of random numbers; the array must be pre-allocated and have 1 to 4 channels.</param>
 /// <param name="mean">Mean value (expectation) of the generated random numbers.</param>
 /// <param name="stddev">Standard deviation of the generated random numbers; it can be either a vector (in which case a diagonal standard deviation matrix is assumed) or a square matrix.</param>
 public static void Randn(IInputOutputArray dst, MCvScalar mean, MCvScalar stddev)
 {
    using (ScalarArray saMean = new ScalarArray(mean))
    using (ScalarArray saStddev = new ScalarArray(stddev))
    {
       Randn(dst, saMean, saStddev);
    }
 }
Ejemplo n.º 16
0
      public void TestWaterShed()
      {
         Image<Bgr, Byte> image = EmguAssert.LoadImage<Bgr, byte>("stuff.jpg");
         Image<Gray, Int32> marker = new Image<Gray, Int32>(image.Width, image.Height);
         Rectangle rect = image.ROI;
         marker.Draw(
            new CircleF(
               new PointF(rect.Left + rect.Width / 2.0f, rect.Top + rect.Height / 2.0f),
            /*(float)(Math.Min(image.Width, image.Height) / 20.0f)*/ 5.0f),
            new Gray(255),
            0);
         Image<Bgr, Byte> result = image.ConcateHorizontal(marker.Convert<Bgr, byte>());
         Image<Gray, Byte> mask = new Image<Gray, byte>(image.Size);
         CvInvoke.Watershed(image, marker);
         using (ScalarArray ia = new ScalarArray(0.5))
            CvInvoke.Compare(marker, ia, mask, CvEnum.CmpType.GreaterThan);

         //ImageViewer.Show(result.ConcateHorizontal(mask.Convert<Bgr, Byte>()));
      }
Ejemplo n.º 17
0
 /// <summary>
 /// Generates a single uniformly-distributed random number or an array of random numbers.
 /// </summary>
 /// <param name="dst">Output array of random numbers; the array must be pre-allocated.</param>
 /// <param name="low">Inclusive lower boundary of the generated random numbers.</param>
 /// <param name="high">Exclusive upper boundary of the generated random numbers.</param>
 public static void Randu(IInputOutputArray dst, MCvScalar low, MCvScalar high)
 {
    using (ScalarArray iaLow = new ScalarArray(low))
    using (ScalarArray iaHigh = new ScalarArray(high))
    {
       Randu(dst, iaLow, iaHigh);
    }
 }
Ejemplo n.º 18
0
 public void SetRandUniform(MCvScalar floorValue, MCvScalar ceilingValue)
 {
     using (ScalarArray saLow = new ScalarArray(floorValue))
         using (ScalarArray saHigh = new ScalarArray(ceilingValue))
             CvInvoke.Randu(this.Mat, saLow, saHigh);
 }