Пример #1
0
        public VideoProcessor()
        {
            background = null;

            pixelateFilter = new Pixellate();
            pixelateFilter.PixelSize = 10;

            differenceFilter = new Difference();
            thresholdFilter = new Threshold(15);
            grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            erosionFilter = new Erosion();

            moveTowardsFilter = new MoveTowards();

            filters1 = new FiltersSequence();
            filters1.Add(pixelateFilter);
            filters1.Add(grayscaleFilter);

            filters2 = new FiltersSequence();

            filters2.Add(differenceFilter);
            filters2.Add(thresholdFilter);
            filters2.Add(erosionFilter);

            rat1 = new Tracker(640 / 2, 480 / 2, Color.Red);

            rat2 = new Tracker(400, 300, Color.Green);

            counter = 0;
        }
Пример #2
0
        public static Bitmap PixelDiff(Bitmap a, Bitmap b)
        {
            var difference = new Difference(a);
            var dif =  difference.Apply(b);

            gaussianBlur.ApplyInPlace(dif);

            Bitmap clone = Grayscale.CommonAlgorithms.Y.Apply(dif);

            threshold.ApplyInPlace(clone);
            fillHoles.ApplyInPlace(clone);

            return clone;
        }
Пример #3
0
 /// <summary>
 /// processes Frame for Motion Detection based on frame comparison
 /// </summary>
 /// <param name="frame">
 /// Takes in 2 Bitmap parameters, currentFrame and backgroundFrame
 /// </param>
 /// <returns>
 /// frame in which motion is marked
 /// </returns>
 public Bitmap processFrame(params Bitmap[] frame)
 {
     Bitmap currentFrame = frame[0];
     // create grayscale filter (BT709)
     Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
     Bitmap GScurrentFrame = filter.Apply(currentFrame);
     if (this.backgroundFrame == null)
     {
         this.backgroundFrame = (Bitmap)GScurrentFrame.Clone();
         GScurrentFrame.Dispose();
         return currentFrame;
     }
     else
     {
         Difference differenceFilter = new Difference();
         IFilter thresholdFilter = new Threshold(15);
         // set backgroud frame as an overlay for difference filter
         differenceFilter.OverlayImage = backgroundFrame;
         Bitmap tmp = thresholdFilter.Apply(differenceFilter.Apply(GScurrentFrame));
         //reduce noise
         IFilter erosionFilter = new Erosion();
         Bitmap tmp1 = erosionFilter.Apply(tmp);
         tmp.Dispose();
         // Highlight Motions
         IFilter extractChannel = new ExtractChannel(RGB.G);
         Bitmap redChannel = extractChannel.Apply(currentFrame);
         Merge mergeFilter = new Merge();
         mergeFilter.OverlayImage = tmp1;
         Bitmap t3 = mergeFilter.Apply(redChannel);
         ReplaceChannel rc = new ReplaceChannel(RGB.G, t3);
         t3 = rc.Apply(currentFrame);
         this.backgroundFrame = (Bitmap)GScurrentFrame.Clone();
         redChannel.Dispose();
         tmp1.Dispose();
         GScurrentFrame.Dispose();
         return t3;
     }
 }
Пример #4
0
        private void InitFilters()
        {
            L_brownFilter = new ColorFiltering();
            D_brownFilter = new ColorFiltering();

            L_brownFilter.Red = new IntRange(125, 140);
            L_brownFilter.Green = new IntRange(95, 110);
            L_brownFilter.Blue = new IntRange(110, 130);

            D_brownFilter.Red = new IntRange(55, 85);
            D_brownFilter.Green = new IntRange(45, 75);
            D_brownFilter.Blue = new IntRange(45, 75);

            blobFilter = new BlobsFiltering();
            blobFilter.CoupledSizeFiltering = true;
            blobFilter.MinWidth = 70;
            blobFilter.MinHeight = 70;

            diffFilter = new Difference();
            diffFilter.OverlayImage = back;

            thresholdFilter = new Threshold(40);

            erosionFilter = new Erosion();

            edgeFilter = new Edges();

            openFilter = new Opening();

            pixelFilter = new Pixellate();

            morphFilter = new Morph();
            morphFilter.SourcePercent = 0.9;

            towardsFilter = new MoveTowards();
            towardsFilter.StepSize = 10;

            blobCounter = new BlobCounter();
            blobGrabber = new ExtractBiggestBlob();
        }
Пример #5
0
        private void ParseImage(int imageId)
        {
            DateTime startTime = DateTime.Now;
            labelDetection.Text = "";

            string difImagePath = "";

            if(imageId >= 0) difImagePath = DifBoardImages[imageId];

            Bitmap orig;
            Bitmap dif;

            try
            {
                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);

                if (imageId >= 0)
                    dif = (Bitmap)Bitmap.FromFile(difImagePath);
                else
                    dif = (Bitmap)pictureBoxGenerated.Image;
            }
            catch {
                // kill any exception due to missing files
                return;
            }

            pictureBoxOrig.Image = dif;

            Difference filter = new Difference(orig);
            dif = filter.Apply(dif);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.ProcessImage(dif);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // create Graphics object to draw on the image and a pen
            Graphics g = Graphics.FromImage(dif);
            Pen redPen = new Pen(Color.Red, 3);
            Pen bluePen = new Pen(Color.Blue, 1);
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            // check each object and draw circle around objects, which
            // are recognized as circles
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                labelDetection.Text = string.Format("{0} points", edgePoints.Count);

                if (edgePoints.Count <= 1)
                    continue;

                List<IntPoint> points = new List<IntPoint>();

                if (shapeChecker.IsQuadrilateral(edgePoints))
                    labelDetection.Text += ", quadrilateral";
                else if (shapeChecker.IsConvexPolygon(edgePoints, out points))
                    labelDetection.Text += ", convex poligon";
                else if (shapeChecker.IsTriangle(edgePoints, out points))
                    labelDetection.Text += ", triangle";

                Pen usePen = shapeChecker.IsQuadrilateral(edgePoints) ? redPen : bluePen;

                int centerX = edgePoints[0].X;
                int centerY = edgePoints[0].Y;
                int minX = centerX;
                int minY = centerY;
                int maxX = centerX;
                int maxY = centerY;
                for (int j = 0; j < edgePoints.Count - 1; j++)
                {
                    centerX += edgePoints[j + 1].X;
                    centerY += edgePoints[j + 1].Y;

                    if (edgePoints[j + 1].X < minX) minX = edgePoints[j + 1].X;
                    if (edgePoints[j + 1].Y < minY) minY = edgePoints[j + 1].Y;
                    if (edgePoints[j + 1].X > maxX) maxX = edgePoints[j + 1].X;
                    if (edgePoints[j + 1].Y > maxX) maxX = edgePoints[j + 1].Y;

                    g.DrawLine(usePen, edgePoints[j].X, edgePoints[j].Y, edgePoints[j + 1].X, edgePoints[j + 1].Y);
                }

                g.DrawLine(usePen
                    , edgePoints[0].X, edgePoints[0].Y
                    , edgePoints[edgePoints.Count - 1].X, edgePoints[edgePoints.Count - 1].Y);

                labelDetectedPosition.Text = string.Format("{0}, {1}"
                    , new object[] {
                        (centerX / edgePoints.Count), (centerY / edgePoints.Count)
                        //, maxX - minX, maxY - minY
                    });
                labelDetectionOffset.Text = string.Format("{0}, {1}"
                    , Grigore.Position.X - (centerX / edgePoints.Count)
                    , Grigore.Position.Y - (centerY / edgePoints.Count)
                );
            }

            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();

            pictureBoxRoboBoard.Image = dif;

            labelDetection.Text = string.Format("{1}, {0} sec", (DateTime.Now - startTime), labelDetection.Text);
        }
Пример #6
0
 private void InitFilters()
 {
     gsFilter = new Grayscale(0.33, 0.33, 0.33);
     diffFilter = new Difference();
     motionFilter = new FiltersSequence();
     motionFilter.Add(new Threshold(THRESHOLD));
     motionFilter.Add(new BlobsFiltering(MIN_BLOB, MIN_BLOB, MAX_BLOB, MAX_BLOB, true));
     morphFilter = new Morph();
     morphFilter.SourcePercent = MORPH_PERCENT;
     blobCount = new BlobCounter();
     blobCount.MinHeight = MIN_BLOB;
     blobCount.MaxHeight = MAX_BLOB;
 }
Пример #7
0
        private Bitmap XXX(Bitmap bmpBefore, Bitmap bmpAfter)
        {
            var filter = new Grayscale(0.2125, 0.7154, 0.0721);
            bmpBefore = filter.Apply(bmpBefore);
            bmpAfter = filter.Apply(bmpAfter);

            // create filters
            var differenceFilter = new Difference();
            IFilter thresholdFilter = new Threshold(15);
            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = bmpBefore;
            // apply the filters
            Bitmap tmp1 = differenceFilter.Apply(bmpAfter);
            Bitmap tmp2 = thresholdFilter.Apply(tmp1);
            IFilter erosionFilter = new Erosion();
            // apply the filter
            Bitmap tmp3 = erosionFilter.Apply(tmp2);

            IFilter pixellateFilter = new Pixellate();
            // apply the filter
            Bitmap tmp4 = pixellateFilter.Apply(tmp3);

            return tmp4;
        }
Пример #8
0
        private void ParseImage(int imageId)
        {
            DateTime startTime = DateTime.Now;
            Console.WriteLine(string.Format("Start parsing {0}", startTime));

            string difImagePath = "";

            if (imageId >= 0) difImagePath = DifBoardImages[imageId];

            Bitmap orig;
            Bitmap dif;

            try
            {
                Console.WriteLine("Load files");

                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);

                if (imageId >= 0)
                    dif = (Bitmap)Bitmap.FromFile(difImagePath);
                else
                    dif = (Bitmap)internalImageContainer;
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERR: " + ex.Message);
                Console.WriteLine(ex.StackTrace);

                // kill any exception due to missing files
                return;
            }

            Difference filter = new Difference(orig);
            dif = filter.Apply(dif);
            Console.WriteLine("Apply difference filter");

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.ProcessImage(dif);
            Console.WriteLine("Blob counting");
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // create Graphics object to draw on the image and a pen
            Graphics g = Graphics.FromImage(dif);
            Pen redPen = new Pen(Color.Red, 3);
            Pen bluePen = new Pen(Color.Blue, 1);
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            // check each object and draw circle around objects, which
            // are recognized as circles
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);

                Console.Write(string.Format("Detected {0} points", edgePoints.Count));

                if (edgePoints.Count <= 1)
                    continue;

                List<IntPoint> points = new List<IntPoint>();

                if (shapeChecker.IsQuadrilateral(edgePoints))
                    Console.Write(", quadrilateral");
                else if (shapeChecker.IsConvexPolygon(edgePoints, out points))
                    Console.Write(", convex poligon");
                else if (shapeChecker.IsTriangle(edgePoints, out points))
                    Console.Write(", triangle");

                Pen usePen = shapeChecker.IsQuadrilateral(edgePoints) ? redPen : bluePen;

                int centerX = edgePoints[0].X;
                int centerY = edgePoints[0].Y;

                for (int j = 0; j < edgePoints.Count - 1; j++)
                {
                    centerX += edgePoints[j + 1].X;
                    centerY += edgePoints[j + 1].Y;

                    g.DrawLine(usePen, edgePoints[j].X, edgePoints[j].Y, edgePoints[j + 1].X, edgePoints[j + 1].Y);
                }

                // add last point
                centerX += edgePoints[edgePoints.Count - 1].X;
                centerY += edgePoints[edgePoints.Count - 1].Y;

                g.DrawLine(usePen
                    , edgePoints[0].X, edgePoints[0].Y
                    , edgePoints[edgePoints.Count - 1].X, edgePoints[edgePoints.Count - 1].Y);

                Console.WriteLine(string.Format("\nOriginal (x, y): {0}, {1}"
                    , new object[] {
                        (centerX / edgePoints.Count), (centerY / edgePoints.Count)
                    }));
                Console.WriteLine(string.Format("Detected (x, y): {0}, {1}"
                    , (centerX / edgePoints.Count)
                    , (centerY / edgePoints.Count)
                ));
            }

            redPen.Dispose();
            bluePen.Dispose();
            g.Dispose();

            //DifI = dif;
            dif.Save(dirPath + "processed-dif.jpg");

            Console.WriteLine(string.Format("Duration: {0} sec", (DateTime.Now - startTime)));
        }
Пример #9
0
        public MatchingPoint CheckExactMatch(Bitmap bmpSource, Bitmap bmpToFind, Bitmap bmpMask, int minimumCertainty)
        {
            MatchingPoint match = new MatchingPoint();

            match.X = 0;
            match.Y = 0;
            match.MinimumCertainty = minimumCertainty;

            Subtract subFilter = (bmpMask != null) ? new Subtract(bmpMask) : null;

            if (subFilter != null)
                subFilter.ApplyInPlace(bmpToFind);
            Difference filter = new Difference(bmpToFind);

            Bitmap bmpDiff;

            if (subFilter != null)
                subFilter.ApplyInPlace(bmpSource);

            bmpDiff = filter.Apply(bmpSource);

            #region debug
            Bitmap bmpRenderedDiff;
            Bitmap bmpRenderedToFind;
            if (Debug)
            {
                bmpRenderedDiff = bmpSource.Clone(new Rectangle(match.X, match.Y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);
                bmpRenderedToFind = bmpSource.Clone(new Rectangle(match.X, match.Y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);

                using (Graphics g = Graphics.FromImage(bmpRenderedDiff))
                {
                    System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix();
                    cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
                    cm.Matrix43 = 1.0F;

                    System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();

                    ia.SetColorMatrix(cm);

                    g.DrawImage(bmpDiff, new Rectangle(0, 0, bmpDiff.Width, bmpDiff.Height), 0, 0, bmpDiff.Width, bmpDiff.Height, GraphicsUnit.Pixel, ia);
                }

                using (Graphics g = Graphics.FromImage(bmpRenderedToFind))
                {
                    System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix();
                    cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
                    cm.Matrix43 = 1.0F;

                    System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();

                    ia.SetColorMatrix(cm);

                    g.DrawImage(bmpToFind, new Rectangle(0, 0, bmpToFind.Width, bmpToFind.Height), 0, 0, bmpToFind.Width, bmpToFind.Height, GraphicsUnit.Pixel, ia);
                }
            }
            #endregion

            int testRed = 0;
            int testBlue = 0;
            int testGreen = 0;
            ImageStatistics stats = new ImageStatistics(bmpDiff);
            if (Debug)
            {
                ImageStatistics stats2 = new ImageStatistics(bmpRenderedDiff);
                ImageStatistics stats3 = new ImageStatistics(bmpRenderedToFind);
                ImageStatistics stats4 = new ImageStatistics(bmpToFind);
            }
            for (int i = 0; i < 256; i++)
            {
                testRed += stats.Red.Values[i] * i;
                testGreen += stats.Green.Values[i] * i;
                testBlue += stats.Blue.Values[i] * i;
            }

            int testAvg = (int)((testRed + testGreen + testBlue) / 3);

            match.Certainty = testAvg;
            match.Confident = (testAvg <= match.MinimumCertainty);
            match.Resolution = 1;

            return match;
        }
Пример #10
0
        private MatchingPoint FindSingleChannelImaging(Bitmap bmpSource, Bitmap bmpToFind, Bitmap bmpMask, MatchingPoint match, int xN, int yN, int stepSize, int scale = 1, bool bQuickCheck = true)
        {
            Subtract subFilter = (bmpMask != null) ? new Subtract(bmpMask) : null;

            if (subFilter != null)
                subFilter.ApplyInPlace(bmpToFind);
            Difference filter = new Difference(bmpToFind);

            Bitmap bmpDiff;

            // if Debug
            Bitmap bmpBestDiff;
            Bitmap bmpBestMatch;
            Bitmap bmpBestSearch;
            // \if Debug

            int xMin = 0;
            int yMin = 0;
            int maxValue = bmpToFind.Width * bmpToFind.Height * 256 * scale * scale;
            int testMin = maxValue;
            int lastTestAvg = maxValue;

            // Get width of gametitle
            int gWidth = bmpToFind.Width;
            int gHeight = bmpToFind.Height;

            // Get width of fullscreen
            int fWidth = bmpSource.Width;
            int fHeight = bmpSource.Height;

            match.X = Math.Max(match.X, 0);
            match.Y = Math.Max(match.Y, 0);

            if ((bmpSource.Width * bmpSource.Height) < (bmpToFind.Width * bmpToFind.Height * 1.2 * 1.2))
            { // Images are similar size
                stepSize = 1; // force step to each location
            }

            // Determine search limits
            if (xN == 0)
                xN = (fWidth - gWidth) - stepSize;
            else
                xN = Math.Min((fWidth - gWidth) - stepSize, xN);

            if (yN == 0)
                yN = (fHeight - gHeight) - stepSize;
            else
                yN = Math.Min((fHeight - gHeight) - stepSize, yN);

            int xConfDeltaCount = 0;
            int yConfDeltaCount = 0;
            int xSearchBreak = 8;
            int ySearchBreak = 12;

            int prevRoundMax = 0;
            int prevRoundMin = maxValue;

            int missedMinCount = 0;
            double deltaConfidence = 0;
            double maxDeltaConfidence = 0;
            // For each step in x
            for (int x = match.X; x < xN; x++, xConfDeltaCount++)
            {
                if (bQuickCheck && missedMinCount > xSearchBreak)
                    break;

                int currRoundMax = 0;
                int currRoundMin = int.MaxValue;

                //yConfDeltaCount = 0;
                int missedYMinCount = 0;
                // For each step in y
                for (int y = match.Y; y < yN; y++, yConfDeltaCount++)
                {
                    if (bQuickCheck && missedYMinCount > ySearchBreak)
                    {
                        break;
                    }
                    //    break;

                    if (StopRequested)
                        return match;

                    Bitmap bmpSearchArea = bmpSource.Clone(new Rectangle(x, y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);

                    if (subFilter != null)
                        subFilter.ApplyInPlace(bmpSearchArea);

                    bmpDiff = filter.Apply(bmpSearchArea);

                    #region debug
                    Bitmap bmpRenderedDiff;
                    Bitmap bmpRenderedToFind;
                    Bitmap bmpRenderedSearch;
                    if (Debug)
                    {
                        bmpRenderedDiff = bmpSource.Clone(new Rectangle(x, y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);
                        bmpRenderedToFind = bmpSource.Clone(new Rectangle(x, y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);
                        bmpRenderedSearch = bmpSource.Clone(new Rectangle(x, y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);

                        using (Graphics g = Graphics.FromImage(bmpRenderedDiff))
                        {
                            System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix();
                            cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
                            cm.Matrix43 = 1.0F;

                            System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();

                            ia.SetColorMatrix(cm);

                            g.DrawImage(bmpDiff, new Rectangle(0, 0, bmpDiff.Width, bmpDiff.Height), 0, 0, bmpDiff.Width, bmpDiff.Height, GraphicsUnit.Pixel, ia);
                        }

                        using (Graphics g = Graphics.FromImage(bmpRenderedToFind))
                        {
                            System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix();
                            cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
                            cm.Matrix43 = 1.0F;

                            System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();

                            ia.SetColorMatrix(cm);

                            g.DrawImage(bmpToFind, new Rectangle(0, 0, bmpToFind.Width, bmpToFind.Height), 0, 0, bmpToFind.Width, bmpToFind.Height, GraphicsUnit.Pixel, ia);
                        }

                        using (Graphics g = Graphics.FromImage(bmpRenderedSearch))
                        {
                            System.Drawing.Imaging.ColorMatrix cm = new System.Drawing.Imaging.ColorMatrix();
                            cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
                            cm.Matrix43 = 1.0F;

                            System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();

                            ia.SetColorMatrix(cm);

                            g.DrawImage(bmpSearchArea, new Rectangle(0, 0, bmpToFind.Width, bmpToFind.Height), 0, 0, bmpToFind.Width, bmpToFind.Height, GraphicsUnit.Pixel, ia);
                        }
                    }
                    #endregion

                    int testRed = 0;
                    int testBlue = 0;
                    int testGreen = 0;
                    ImageStatistics stats = new ImageStatistics(bmpDiff);
                    if (Debug)
                    {
                        ImageStatistics stats2 = new ImageStatistics(bmpRenderedDiff);
                        ImageStatistics stats3 = new ImageStatistics(bmpRenderedToFind);
                        ImageStatistics stats4 = new ImageStatistics(bmpToFind);
                    }
                    for (int i = 0; i < 256; i++)
                    {
                        testRed += stats.Red.Values[i] * i;
                        testGreen += stats.Green.Values[i] * i;
                        testBlue += stats.Blue.Values[i] * i;
                    }

                    int testAvg = (int)((testRed + testGreen + testBlue) / 3);

                    testAvg = testAvg * scale * scale;
                    this.UpdateCertainty(testAvg, match.MinimumCertainty, maxValue);

                    // Work out positioning
                    currRoundMin = Math.Min(testAvg, currRoundMin);
                    currRoundMax = Math.Max(testAvg, currRoundMax);

                    if (testAvg > currRoundMin)
                        missedYMinCount++;
                    else
                        missedYMinCount = 0;

                    deltaConfidence = (100D - (testAvg * 100F / prevRoundMin));

                    if (prevRoundMin != maxValue)
                    {
                        maxDeltaConfidence = Math.Max(deltaConfidence, maxDeltaConfidence);

                    }
                    else
                        deltaConfidence = 0;

                    this.UpdateDelta(deltaConfidence);
                    if (testAvg < testMin)
                    { // If smaller than previous min, use new values
                        if (Debug)
                        {
                            bmpBestDiff = bmpRenderedDiff;
                            bmpBestMatch = bmpSource.Clone(new Rectangle(x, y, bmpToFind.Width, bmpToFind.Height), bmpToFind.PixelFormat);
                            bmpBestSearch = bmpRenderedSearch;
                        }
                        xMin = x;
                        yMin = y;
                        testMin = testAvg;

                    }
                }

                if (currRoundMin > prevRoundMin)
                    missedMinCount++;

                prevRoundMax = currRoundMax;
                prevRoundMin = currRoundMin;

            }

            match.X = xMin * scale;
            match.Y = yMin * scale;
            match.MaxCertaintyDelta = maxDeltaConfidence;
            match.Certainty = (maxDeltaConfidence > 10) ? testMin : maxValue;
            match.Confident = (testMin <= match.MinimumCertainty);
            match.Resolution = stepSize * scale;

            return match;
        }
Пример #11
0
        private Bitmap getTwoFrameDifference(Bitmap frame)
        {
            if (lastFrame == null)
            {
                return null;
            }

            Difference filter = new Difference(lastFrame);
            Bitmap differenceBitmap = filter.Apply(frame);

            return differenceBitmap;
        }
Пример #12
0
        private Bitmap getThreeFrameDifference(Bitmap frame)
        {
            if (lastFrame == null || lastButOneFrame == null)
            {
                return null;
            }

            Difference filterPrevious = new Difference(lastButOneFrame);
            Bitmap differenceBitmapPrevious = filterPrevious.Apply(lastFrame);

            Difference filterNext = new Difference(lastFrame);
            Bitmap differenceBitmapNext = filterNext.Apply(frame);

            Intersect intersectionFilter = new Intersect(differenceBitmapPrevious);
            Bitmap threeFrameDifferenceBitmap = intersectionFilter.Apply(differenceBitmapNext);

            return threeFrameDifferenceBitmap;
        }
Пример #13
0
        private Bitmap getChangingBackgroundDifference(Bitmap frame)
        {
            if (background == null || lastFrame == null)
            {
                background = frame;
                return null;
            }

            if (background != lastFrame)
            {
                Morph filter = new Morph(background);
                filter.SourcePercent = 0.5;
                background = filter.Apply(lastFrame);
            }

            Difference differenceFilter = new Difference(background);
            Bitmap differenceBitmap = differenceFilter.Apply(frame);

            return differenceBitmap;
        }
 // Process max 200 frames (5 min) in 320x240 resolution. So 76KB memory per frame (grayscale). 1200 frames is max 93 MB of RAM (normally less because of area)
 private void processFilePart()
 {
     int nrofframes = imageStack.Length;
     int i;
     int sum;
     // create filters
     Morph morphFilter = new Morph(); // filter for adapting background
     morphFilter.SourcePercent = 0.8;
     Difference differenceFilter = new Difference(); // filter for subtracting two frames
     Threshold thresholdFilter = new Threshold(); // filter for thresholding
     FiltersSequence filters = new FiltersSequence(); // all filters in one
     filters.Add(morphFilter);
     filters.Add(differenceFilter);
     filters.Add(thresholdFilter);
     thresholdFilter.ThresholdValue = threshold;
     // Process here
     for (i = 0; i < nrofframes; i++)
     {
         // move background towards current frame
         morphFilter.OverlayImage = imageStack[i];
         Bitmap Temp = morphFilter.Apply(backgroundFrame);
         backgroundFrame = Temp.Clone(new Rectangle(0, 0, Temp.Width, Temp.Height), Temp.PixelFormat);
         Temp.Dispose();
         // apply rest of the filters
         differenceFilter.OverlayImage = imageStack[i];
         Bitmap Temp2 = filters.Apply(backgroundFrame);
         sum = 0;
         // Calculate sum of white pixels
         for (int j = 0; j < Temp2.Width; j++)
         {
             for (int k = 0; k < Temp2.Height; k++)
             {
                 if (Temp2.GetPixel(j, k) != Color.FromArgb(255, 0, 0, 0))
                 {
                     sum += 1;
                 }
             }
         }
         Temp2.Dispose();
         if (sum > objectsize)
         {
             tracker.addFrame(currentFrame);
         }
         currentFrame += 1;
     }
     // Discard Array
     for (i = 0; i < nrofframes; i++)
     {
         imageStack[i].Dispose();
     }
 }
Пример #15
0
 /// <summary>
 /// Get a difference image between to images
 /// </summary>
 /// <param name="org"></param>
 /// <param name="act"></param>
 /// <returns></returns>
 public static Bitmap GetDifferenceImage(Bitmap org, Bitmap act)
 {
     var difference = new Difference(org);
     var result = difference.Apply(act);
     return result;
 }
Пример #16
0
        public void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // get new frame
            CheckForIllegalCrossThreadCalls = false;
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
            if (pictureBox1.Image == null)
            {
                bitmap.Save(@"modelo0.jpg");
                bgFrame = bitmap;
            }
            else
            {
                try
                {
                    bgFrame = (Bitmap)pictureBox1.Image.Clone();
                    //bitmap.Save(@"currImage.jpg");
                    currentFrame = (Bitmap)bitmap.Clone();

                    Bitmap gcurrent = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);
                    Bitmap gbground = Grayscale.CommonAlgorithms.BT709.Apply(bgFrame);

                    Difference differenceFilter = new Difference();
                    differenceFilter.OverlayImage = gbground;
                    Bitmap currentImg = differenceFilter.Apply(gcurrent);

                    //bloqueo temporal de current image
                    BitmapData currentData = currentImg.LockBits(new Rectangle(0, 0, framesProcessing.fWidth,
                                        framesProcessing.fHeight), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                    //**Thresholdfilter
                    Threshold thresholdFilter = new Threshold(15);
                    thresholdFilter.ApplyInPlace(currentData);
                    IFilter erosionFilter = new Erosion();
                    Bitmap tmp2 = erosionFilter.Apply(currentData);

                    currentImg.UnlockBits(currentData);
                    i = framesProcessing.CalculateWhitePixels(tmp2);

                    if (i > 300)
                    {
                        mov = mov2 = true;
                    }
                    else
                        mov = false;

                    pictureBox2.Image = currentImg;
                }
                catch{//MessageBox.Show(ex.Message.ToString());
                }

            }
            pictureBox1.Image = bitmap;
        }
        private void DetectMotion()
        {
            if ((_previousFrame != null) && (_currentFrame != null))
            {
                Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
                using (Bitmap frame1GS = grayscaleFilter.Apply(_previousFrame))
                {
                    using (Bitmap frame2GS = grayscaleFilter.Apply(_currentFrame))
                    {
                        Difference differenceFilter = new Difference();
                        IFilter thresholdFilter = new Threshold(15);
                        // set backgroud frame as an overlay for difference filter

                        differenceFilter.OverlayImage = (Bitmap)frame1GS;
                        // apply the filters

                        using (Bitmap tmp1 = differenceFilter.Apply((Bitmap)frame2GS))
                        {
                            using (Bitmap tmp2 = thresholdFilter.Apply(tmp1))
                            {

                                IFilter erosionFilter = new Erosion();
                                // apply the filter

                                using (Bitmap tmp3 = erosionFilter.Apply(tmp2))
                                {
                                    int whitePixelsCount = CalculateWhitePixels(tmp3);

                                    if (whitePixelsCount > 1000)
                                    {
                                        MotionDetectionEvent motionEvent = new MotionDetectionEvent()
                                        {
                                            CameraDevice = _camera,
                                            NumberOfPixelsDetected = whitePixelsCount,
                                            InputDevice = _camera
                                        };
                                        MotionDetectedEventArgs args = new MotionDetectedEventArgs(motionEvent);
                                        OnMotionDetected(args);
                                    }

                                    //// extract red channel from the original image

                                    //IFilter extrachChannel = new ExtractChannel(RGB.R);
                                    //Bitmap redChannel = extrachChannel.Apply(frame2);
                                    ////  merge red channel with motion regions

                                    //Merge mergeFilter = new Merge();
                                    //mergeFilter.OverlayImage = tmp3;
                                    //Bitmap tmp4 = mergeFilter.Apply(redChannel);
                                    //// replace red channel in the original image

                                    //ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R, tmp4);
                                    //replaceChannel.ChannelImage = tmp4;
                                    //Bitmap tmp5 = replaceChannel.Apply(frame2);
                                }
                            }
                        }
                    }
                }
            }
        }