public static Bitmap ConvertToGrayScale(this Bitmap me)
    {
        if (me == null)
        {
            return(null);
        }
        int radius = 20, x = me.Width / 2, y = me.Height / 2;

        // Generate a two-dimensional `byte` array that has the same size as the source image, which will be used as the mask.
        byte[,] mask = new byte[me.Height, me.Width];
        // Initialize all its elements to the value 0xFF (255 in decimal).
        Initialize(mask, 0xFF);
        // "Draw" a circle in the `byte` array setting the positions inside the circle with the value 0.
        DrawCircle(mask, x, y, radius, 0);
        var grayFilter = new Grayscale(0.2125, 0.7154, 0.0721);
        var rgbFilter  = new GrayscaleToRGB();
        var maskFilter = new ApplyMask(mask);
        // Apply the `Grayscale` filter to everything outside the circle, convert the resulting image back to RGB
        Bitmap img = rgbFilter.Apply(grayFilter.Apply(maskFilter.Apply(me)));

        // Invert the mask
        Invert(mask);
        // Get only the cirle in color from the original image
        Bitmap circleImg = new ApplyMask(mask).Apply(me);

        // Merge both the grayscaled part of the image and the circle in color in a single one.
        return(new Merge(img).Apply(circleImg));
    }
 public IMaskedGameImage GetMaskedFrame(PixelSplitterSettings settings)
 {
     lock (frameLock)
     {
         var mask      = settings.GetImageBitMask(this.frame.Width, this.frame.Height);
         var applyMask = new ApplyMask(mask);
         return(new MaskedGameImage(applyMask.Apply(this.frame)));
     }
 }
            public void SaveFrame(string path)
            {
                lock (frameLock)
                {
                    var settings = SettingsProvider.Get();
                    if (settings.ImageMatchMask != null && settings.ImageMatchMask.Count > 0)
                    {
                        var mask      = settings.GetImageBitMask(this.backframe.Width, this.backframe.Height);
                        var applyMask = new ApplyMask(mask);

                        using (var masked = applyMask.Apply(this.backframe))
                        {
                            masked.Save(path, ImageFormat.Png);
                        }
                    }
                    else
                    {
                        this.backframe.Save(path, ImageFormat.Png);
                    }
                }
            }
Esempio n. 4
0
        void SensorFrameReady(AllFramesReadyEventArgs e)
        {
            // if the window is displayed, show the depth buffer image
            Bitmap mask = null;

            using (DepthImageFrame frame = e.OpenDepthImageFrame())
            {
                if (frame != null)
                {
                    _depthBitmap = CreateBitMapFromDepthFrame(frame);
                    ColorFiltering filter = new ColorFiltering();
                    filter.Red   = new IntRange(50, 150);
                    filter.Green = new IntRange(200, 255);
                    filter.Blue  = new IntRange(150, 255);
                    // apply the filter
                    _depthBitmap = AForge.Imaging.Image.Clone(_depthBitmap, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    mask         = filter.Apply(_depthBitmap);


                    Grayscale gray = new Grayscale(0.7, 0.3, 0.1);
                    mask = gray.Apply(mask);

                    Threshold threshold = new Threshold(100);
                    threshold.ApplyInPlace(mask);

                    Invert invert = new Invert();
                    invert.ApplyInPlace(mask);
                }
            }

            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame != null)
                {
                    _bitmap = _drawBitmap = ImageToBitmap(frame);
                    if (mask != null)
                    {
                        ApplyMask subtract = new ApplyMask(mask);
                        _bitmap = subtract.Apply(_bitmap);
                    }

                    HSLFiltering hsl = new HSLFiltering(new IntRange(330, 30), new Range(0.5f, 1), new Range(0.1f, 1));
                    hsl.ApplyInPlace(_bitmap);

                    Grayscale gray = new Grayscale(1, 0.8, 0.8);
                    _bitmap = gray.Apply(_bitmap);


                    Mean meanFilter = new Mean();
                    meanFilter.ApplyInPlace(_bitmap);


                    BlobsFiltering blob = new BlobsFiltering();
                    blob.CoupledSizeFiltering = false;
                    blob.MinHeight            = 15;
                    blob.MinWidth             = blob.MinHeight;
                    blob.MaxHeight            = blob.MaxWidth = 300;
                    blob.ApplyInPlace(_bitmap);

                    // locate objects using blob counter
                    BlobCounter blobCounter = new BlobCounter();
                    blobCounter.ProcessImage(_bitmap);
                    Blob[] blobs = blobCounter.GetObjectsInformation();

                    Pen redPen    = new Pen(Color.Red, 2);
                    Pen greenPen  = new Pen(Color.Green, 2);
                    Pen purplePen = new Pen(Color.OrangeRed, 3);
                    Pen pen;
                    // check each object and draw circle around objects, which
                    // are recognized as circles
                    int    maxBlob     = -1;
                    double maxFullness = -1;
                    for (int i = 0, n = blobs.Length; i < n; i++)
                    {
                        float hw_ratio = (float)(blobs[i].Rectangle.Height) / blobs[i].Rectangle.Width;

                        if (hw_ratio > 0.65 && hw_ratio < 1.5 && blobs[i].Area > 200 && blobs[i].Fullness > 0.35)
                        {
                            if (blobs[i].Area > maxFullness)
                            {
                                maxBlob     = i;
                                maxFullness = blobs[i].Area;
                            }
                        }
                    }

                    System.Console.WriteLine("MAX BLOB: " + maxBlob.ToString());


                    //draw to screen!
                    Grayscale gray2 = new Grayscale(0.2125, 0.7154, 0.072);
                    _drawBitmap = gray2.Apply(_drawBitmap);
                    _drawBitmap = AForge.Imaging.Image.Clone(_drawBitmap, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(_drawBitmap);

                    AForge.Point ballPos = new AForge.Point(-1, -1);
                    if (maxBlob >= 0)
                    {
                        ballPos = blobs[maxBlob].CenterOfGravity;
                    }
                    game.DrawGridAndScore(g, ballPos);

                    for (int i = 0, n = blobs.Length; i < n; i++)
                    {
                        float hw_ratio = (float)(blobs[i].Rectangle.Height) / blobs[i].Rectangle.Width;

                        if (hw_ratio > 0.65 && hw_ratio < 1.5 && blobs[i].Area > 200 && blobs[i].Fullness > 0.35)
                        {
                            AForge.Point center = blobs[i].CenterOfGravity;
                            if (maxBlob == i)
                            {
                                pen = purplePen;
                            }
                            else if (blobs[i].Fullness > 0.35)
                            {
                                continue;//pen = greenPen;
                            }
                            else
                            {
                                continue;// pen = redPen;
                            }
                            float radius = blobs[i].Rectangle.Width / 2;
                            g.DrawEllipse(pen,
                                          (int)(center.X - radius),
                                          (int)(center.Y - radius),
                                          (int)(radius * 2),
                                          (int)(radius * 2));
                            //g.DrawString(hw_ratio.ToString(), new Font("Arial", 16), new SolidBrush(Color.Yellow),
                            //   new System.Drawing.Point((int)center.X, (int)center.Y));
                        }
                    }

                    redPen.Dispose();
                    greenPen.Dispose();
                    purplePen.Dispose();
                    g.Dispose();

                    if (game.hasWon)
                    {
                        if (player == game.winningPlayer)
                        {
                            label1.Text = "You Won!!! Congrats :)";
                        }
                        else
                        {
                            label1.Text = "You Lost... :(";
                        }
                    }

                    this.Refresh();
                }
            }
        }
        private void JpegLiveSource1LiveNotificationEvent(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                if (OnMainThread)
                {
                    LiveContentEventArgs args = e as LiveContentEventArgs;
                    if (args != null && args.LiveContent != null)
                    {
                        // UI thread is too busy - discard this frame from display
                        args.LiveContent.Dispose();
                    }
                    return;
                }
                OnMainThread = true;
                // Make sure we execute on the UI thread before updating UI Controls
                BeginInvoke(new EventHandler(JpegLiveSource1LiveNotificationEvent), new[] { sender, e });
            }
            else
            {
                LiveContentEventArgs args = e as LiveContentEventArgs;
                if (args != null)
                {
                    if (args.LiveContent != null)
                    {
                        // Display the received JPEG
                        //textBoxLength.Text = "" + args.LiveContent.Content.Length;

                        int width  = args.LiveContent.Width;
                        int height = args.LiveContent.Height;

                        MemoryStream ms = new MemoryStream(args.LiveContent.Content);
                        //Bitmap newBitmap = testBox();
                        Bitmap newBitmap = new Bitmap(ms);

                        if (referenceBitmap == null)
                        {
                            referenceBitmap = newBitmap;
                        }

                        textBoxResolution.Text = "" + width + "x" + height;

                        if (pictureBoxOriginal.Size.Width != 0 && pictureBoxOriginal.Size.Height != 0)
                        {
                            if ((newBitmap.Width != pictureBoxOriginal.Width || newBitmap.Height != pictureBoxOriginal.Height))
                            {
                                pictureBoxOriginal.Image = new Bitmap(newBitmap, pictureBoxOriginal.Size);
                            }
                            else
                            {
                                pictureBoxOriginal.Image = newBitmap;
                            }
                        }

                        textBoxDecodingStatus.Text = args.LiveContent.HardwareDecodingStatus;



                        ms.Close();
                        ms.Dispose();

                        _count++;
                        textBoxCount.Text = "" + _count;

                        args.LiveContent.Dispose();



                        /// Star processing frame
                        grayImage            = gfilter.Apply(newBitmap);
                        pictureBoxGray.Image = grayImage;

                        try
                        {
                            if (counter == 4000)
                            {
                                counter    = 0;
                                background = null;
                                analyticsImageProcessing.resetBackground();
                            }

                            if (background == null)
                            {
                                background = analyticsImageProcessing.GetBackGound(grayImage);

                                /// Show mensaje
                                Bitmap   bitmap = new Bitmap(320, 240);
                                Graphics g      = Graphics.FromImage(bitmap);
                                g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                                g.DrawString("Processing Background...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                                g.Dispose();
                                pictureBoxBackgound.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                                bitmap.Dispose();
                            }
                            else
                            {
                                pictureBoxBackgound.Image = background;
                                background2 = gfilter.Apply(background);

                                Bitmap backgroundMask = analyticsImageProcessing.diff(grayImage, background2);
                                pictureBox1.Image = backgroundMask;


                                // create filter
                                //   Median filter = new Median();
                                // apply the filter
                                //   Bitmap backgroundMask2 = filter.Apply(backgroundMask);

                                //     pictureBox2.Image = backgroundMask2;

                                // create filter
                                Pixellate pxfilter = new Pixellate(20);
                                // apply the filter
                                Bitmap result = pxfilter.Apply(backgroundMask);
                                //      pictureBox3.Image = result;

                                // create filter
                                Threshold thfilter = new Threshold(1);
                                // apply the filter
                                Bitmap result2 = thfilter.Apply(result);

                                pictureBox4.Image = result2;

                                ApplyMask appmask = new ApplyMask(result2);
                                foreground = appmask.Apply(newBitmap);
                            }


                            /// PocessImage
                            if (foreground != null)
                            {
                                Blob[] blobs = analyticsImageProcessing.GetBlobs(foreground, blobCounter);
                                textBoxMetadata.Text = metadataHandler.SendMetadataBox(blobs, _jpegLiveSource.Width, _jpegLiveSource.Height);
                                PaintHeatMap(blobs);
                                pictureBoxHeatmap.Image = bitmapHeatMap;

                                /// Debug tool


                                if (blobs[0] != null && blobs.Length > 0)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[0], false);
                                    pictureBoxBlob1.Image = blobs[0].Image.ToManagedImage();
                                    textBoxAreaBlob1.Text = blobs[0].Area.ToString();
                                    textBoxXBlob1.Text    = blobs[0].CenterOfGravity.X.ToString();
                                    textBoxYBlob1.Text    = blobs[0].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob1.Image = null;
                                }

                                if (blobs[1] != null && blobs.Length > 1)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[1], false);
                                    pictureBoxBlob2.Image = blobs[1].Image.ToManagedImage();
                                    textBoxAreaBlob2.Text = blobs[1].Area.ToString();
                                    textBoxXBlob2.Text    = blobs[1].CenterOfGravity.X.ToString();
                                    textBoxYBlob2.Text    = blobs[1].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob2.Image = null;
                                }

                                if (blobs[2] != null && blobs.Length > 2)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[2], false);
                                    pictureBoxBlob3.Image = blobs[2].Image.ToManagedImage();
                                    textBoxAreaBlob3.Text = blobs[2].Area.ToString();
                                    textBoxXBlob3.Text    = blobs[2].CenterOfGravity.X.ToString();
                                    textBoxYBlob3.Text    = blobs[2].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob3.Image = null;
                                }

                                if (blobs[3] != null && blobs.Length > 3)
                                {
                                    blobCounter.ExtractBlobsImage(foreground, blobs[3], false);
                                    pictureBoxBlob4.Image = blobs[3].Image.ToManagedImage();
                                    textBoxAreaBlob4.Text = blobs[3].Area.ToString();
                                    textBoxXBlob4.Text    = blobs[3].CenterOfGravity.X.ToString();
                                    textBoxYBlob4.Text    = blobs[3].CenterOfGravity.Y.ToString();
                                }
                                else
                                {
                                    pictureBoxBlob4.Image = null;
                                }
                            }
                        }
                        catch (Exception r)
                        {
                            Console.WriteLine(r.Message);
                        }
                    }
                    else if (args.Exception != null)
                    {
                        // Handle any exceptions occurred inside toolkit or on the communication to the VMS

                        Bitmap   bitmap = new Bitmap(320, 240);
                        Graphics g      = Graphics.FromImage(bitmap);
                        g.FillRectangle(System.Drawing.Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);
                        g.DrawString("Connection lost to server ...", new Font(FontFamily.GenericMonospace, 12), Brushes.White, new PointF(20, pictureBoxOriginal.Height / 2 - 20));
                        g.Dispose();
                        pictureBoxOriginal.Image = new Bitmap(bitmap, pictureBoxOriginal.Size);
                        bitmap.Dispose();
                    }
                }
                OnMainThread = false;
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Called when videoPlayer receives a new frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="image"></param>
        private void videoPlayer_NewFrame(object sender, ref Bitmap frame)
        {
            if (previousFrame != null)
            {
                // find the thresholded euclidian difference between two subsequent frames
                ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(40);
                threshold.OverlayImage = previousFrame;
                var difference = threshold.Apply(frame);

                // only keep big blobs
                var filter = new BlobsFiltering();
                filter.CoupledSizeFiltering = true;
                filter.MinHeight            = 50;
                filter.MinWidth             = 50;
                filter.ApplyInPlace(difference);

                // dilate remaining blobs
                var dilate = new BinaryDilation3x3();
                dilate.ApplyInPlace(difference);
                dilate.ApplyInPlace(difference);
                dilate.ApplyInPlace(difference);
                dilate.ApplyInPlace(difference);

                // put this image in the thresholded picturebox
                thresholdedBox.Image = difference.Clone() as Bitmap;

                // use this as a mask for the current frame
                var mask        = new ApplyMask(difference);
                var maskedFrame = mask.Apply(frame);

                // put this image in the masked picturebox
                maskedBox.Image = maskedFrame.Clone() as Bitmap;

                // now find all moving blobs
                if (frameIndex % 10 == 0)
                {
                    var counter = new BlobCounter();
                    counter.ProcessImage(difference);

                    // only keep blobs that:
                    //     - do not overlap with known cars
                    //     - do not overlap with other blobs
                    //     - have crossed the middle of the frame
                    //     - are at least 100 pixels tall
                    var blobs    = counter.GetObjectsRectangles();
                    var newBlobs = from r in counter.GetObjectsRectangles()
                                   where !trackers.Any(t => t.Tracker.TrackingObject.Rectangle.IntersectsWith(r)) &&
                                   !blobs.Any(b => b.IntersectsWith(r) && b != r) &&
                                   r.Top >= 240 && r.Bottom <= 480 &&
                                   r.Height >= 100
                                   select r;

                    // set up new camshift trackers for each detected blob
                    foreach (var rect in newBlobs)
                    {
                        trackers.Add(new TrackerType(rect, frameIndex, ++carIndex));
                    }
                }

                // now kill all car trackers that have expanded by too much
                trackers.RemoveAll(t => t.Tracker.TrackingObject.Rectangle.Height > 360);

                // and kill all trackers that have lived for 30 frames
                trackers.RemoveAll(t => frameIndex - t.StartIndex > 30);

                // let all remaining trackers process the current frame
                var img = UnmanagedImage.FromManagedImage(maskedFrame);
                trackers
                .ForEach(t => t.Tracker.ProcessFrame(img));

                // remember this frame for next iteration
                previousFrame.Dispose();
                previousFrame = frame.Clone() as Bitmap;

                // draw labels on all tracked cars
                var outputFrame = frame.Clone() as Bitmap;
                trackers
                .FindAll(t => !t.Tracker.TrackingObject.IsEmpty)
                .ForEach(t => DrawCarLabel(outputFrame, t.Tracker.TrackingObject.Rectangle, t.CarNumber));

                // return the processed frame to the video
                frame = outputFrame;
            }

            // or else just remember this frame for next iteration
            else
            {
                previousFrame = frame.Clone() as Bitmap;
            }

            frameIndex++;
        }
Esempio n. 7
0
        /// <summary>
        /// Called when videoPlayer receives a new frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="image"></param>
        private void videoPlayer_NewFrame(object sender, ref Bitmap frame)
        {
            // use an rgb filter to remove everything but mario's outfit
            var filter = new ColorFiltering(
                new IntRange(220, 240),
                new IntRange(200, 220),
                new IntRange(160, 180)
                );
            var filteredFrame = filter.Apply(frame);

            // convert the image to grayscale
            var gray          = new GrayscaleBT709();
            var trackingFrame = gray.Apply(filteredFrame);

            // use a threshold to make the image black & white
            var threshold = new Threshold(1);

            threshold.ApplyInPlace(trackingFrame);

            // apply agressive dilation
            var dilation = new BinaryDilation3x3();

            dilation.ApplyInPlace(trackingFrame);
            dilation.ApplyInPlace(trackingFrame);
            dilation.ApplyInPlace(trackingFrame);

            // use image to mask the original frame
            var mask        = new ApplyMask(trackingFrame);
            var maskedFrame = mask.Apply(frame);

            // is the camshift tracker still tracking mario?
            //     - the tracking object cannot be empty
            //     - the tracking rectangle has to be small
            bool isTracking = tracker.TrackingObject?.IsEmpty == false &&
                              tracker.TrackingObject?.Rectangle.Width <= 50;

            if (!isTracking)
            {
                // no - so do a blob search
                var blobSearch = new BlobCounter();
                blobSearch.FilterBlobs = true;
                blobSearch.MinHeight   = 25;
                blobSearch.MinWidth    = 25;
                blobSearch.ProcessImage(trackingFrame);
                if (blobSearch.ObjectsCount > 0)
                {
                    // assume the first blog we find is mario
                    var rectangles = blobSearch.GetObjectsRectangles();
                    var rect       = rectangles.First();

                    // use camshift to track mario from now on
                    var img = UnmanagedImage.FromManagedImage(maskedFrame);
                    tracker.SearchWindow = rect;
                    tracker.ProcessFrame(img);
                }
            }

            else
            {
                // track mario using camshift
                var img = UnmanagedImage.FromManagedImage(maskedFrame);
                tracker.ProcessFrame(img);

                // draw the reticle where mario is
                var rect = tracker.TrackingObject.Rectangle;
                rect.Inflate(30, 30);
                DrawReticle(frame, rect);
            }

            // update pictureboxes
            thresholdedBox.Image = trackingFrame;
            maskedBox.Image      = maskedFrame;

            frameIndex++;
        }
Esempio n. 8
0
        /// <summary>
        /// Called when videoPlayer receives a new frame.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="image"></param>
        private void videoPlayer_NewFrame_1(object sender, ref Bitmap frame)
        {
            //if (cmb_fuente.SelectedIndex==1)
            //{
            //    RotateBicubic filter = new RotateBicubic(90, true);
            //    frame = filter.Apply(frame);
            //}

            if (previousFrame != null)
            {
                // find the thresholded euclidian difference between two subsequent frames
                //ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(40);
                ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(thr);
                threshold.OverlayImage = previousFrame;
                var difference = threshold.Apply(frame);

                // only keep big blobs
                var filter = new BlobsFiltering();
                filter.CoupledSizeFiltering = true;
                filter.MinHeight            = int.Parse(txt_tamanio2.Text);
                filter.MinWidth             = int.Parse(txt_tamanio2.Text);
                filter.ApplyInPlace(difference);



                //var sobl = new SobelEdgeDetector();
                //sobl.ApplyInPlace(difference);

                //erode image
                var erode = new Erosion3x3();
                for (int i = 0; i < int.Parse(txt_ers.Text); i++)
                {
                    erode.ApplyInPlace(difference);
                    //erode.ApplyInPlace(difference);
                    //erode.ApplyInPlace(difference);
                }

                // dilate remaining blobs
                var dilate = new BinaryDilation3x3();
                for (int i = 0; i < int.Parse(txt_dils.Text); i++)
                {
                    dilate.ApplyInPlace(difference);
                    //dilate.ApplyInPlace(difference);
                    //dilate.ApplyInPlace(difference);
                    //dilate.ApplyInPlace(difference);
                }

                // put this image in the thresholded picturebox
                thresholdedBox.Image = difference.Clone() as Bitmap;

                // use this as a mask for the current frame
                var mask        = new ApplyMask(difference);
                var maskedFrame = mask.Apply(frame);

                // put this image in the masked picturebox
                maskedBox.Image = maskedFrame.Clone() as Bitmap;

                // now find all moving blobs
                if (frameIndex % 10 == 0)
                {
                    var counter = new BlobCounter();
                    counter.ProcessImage(difference);

                    // only keep blobs that:
                    //     - do not overlap with known cars
                    //     - do not overlap with other blobs
                    //     - have crossed the middle of the frame
                    //     - are at least 100 pixels tall
                    var blobs    = counter.GetObjectsRectangles();
                    var newBlobs = from r in counter.GetObjectsRectangles()
                                   where !trackers.Any(t => t.Tracker.TrackingObject.Rectangle.IntersectsWith(r)) &&
                                   !blobs.Any(b => b.IntersectsWith(r) && b != r) &&
                                   r.Top >= 240 && r.Bottom <= 480 &&
                                   r.Height >= int.Parse(txt_tamanio.Text)
                                   select r;

                    // set up new camshift trackers for each detected blob
                    foreach (var rect in newBlobs)
                    {
                        trackers.Add(new TrackerType(rect, frameIndex, ++carIndex));
                    }
                }

                // now kill all car trackers that have expanded by too much
                trackers.RemoveAll(t => t.Tracker.TrackingObject.Rectangle.Height > 360);

                // and kill all trackers that have lived for 30 frames
                //trackers.RemoveAll(t => frameIndex - t.StartIndex > 30);
                trackers.RemoveAll(t => frameIndex - t.StartIndex > frames);

                // let all remaining trackers process the current frame
                var img = UnmanagedImage.FromManagedImage(maskedFrame);
                trackers
                .ForEach(t => t.Tracker.ProcessFrame(img));

                // remember this frame for next iteration
                previousFrame.Dispose();
                previousFrame = frame.Clone() as Bitmap;


                //escribir etiqueta para cada vehiculo
                var outputFrame = frame.Clone() as Bitmap;
                trackers
                .FindAll(t => !t.Tracker.TrackingObject.IsEmpty)
                .ForEach(t => DrawCarLabel(outputFrame, t.Tracker.TrackingObject.Rectangle, t.CarNumber));


                // regresar frame procesado
                frame = outputFrame;
            }
            else
            {   // recordar para siguiente iteracion
                previousFrame = frame.Clone() as Bitmap;
            }

            frameIndex++;

            //if (previousFrame != null)
            //{
            //    // find the thresholded euclidian difference between two subsequent frames
            //    ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(40);
            //    threshold.OverlayImage = previousFrame;
            //    var difference = threshold.Apply(frame);

            //    // only keep big blobs
            //    var filter = new BlobsFiltering();
            //    filter.CoupledSizeFiltering = true;
            //    filter.MinHeight = 50;
            //    filter.MinWidth = 50;
            //    filter.ApplyInPlace(difference);

            //    // dilate remaining blobs
            //    var dilate = new BinaryDilation3x3();
            //    dilate.ApplyInPlace(difference);
            //    //dilate.ApplyInPlace(difference);
            //    //dilate.ApplyInPlace(difference);
            //    //dilate.ApplyInPlace(difference);

            //    // put this image in the thresholded picturebox
            //    thresholdedBox.Image = difference.Clone() as Bitmap;

            //    // use this as a mask for the current frame
            //    var mask = new ApplyMask(difference);
            //    var maskedFrame = mask.Apply(frame);

            //    // put this image in the masked picturebox
            //    maskedBox.Image = maskedFrame.Clone() as Bitmap;

            //    // now find all moving blobs
            //    if (frameIndex % 10 == 0)
            //    {
            //        var counter = new BlobCounter();
            //        counter.ProcessImage(difference);

            //        // only keep blobs that:
            //        //     - do not overlap with known cars
            //        //     - do not overlap with other blobs
            //        //     - have crossed the middle of the frame
            //        //     - are at least 100 pixels tall
            //        var blobs = counter.GetObjectsRectangles();
            //        var newBlobs = from r in counter.GetObjectsRectangles()
            //                       where !trackers.Any(t => t.Tracker.TrackingObject.Rectangle.IntersectsWith(r))
            //                          && !blobs.Any(b => b.IntersectsWith(r) && b != r)
            //                          && r.Top >= 240 && r.Bottom <= 480
            //                          && r.Height >= 100
            //                       select r;

            //        // set up new camshift trackers for each detected blob
            //        foreach (var rect in newBlobs)
            //        {
            //            trackers.Add(new TrackerType(rect, frameIndex, ++carIndex));
            //        }
            //    }

            //    // now kill all car trackers that have expanded by too much
            //    trackers.RemoveAll(t => t.Tracker.TrackingObject.Rectangle.Height > 360);

            //    // and kill all trackers that have lived for 30 frames
            //    trackers.RemoveAll(t => frameIndex - t.StartIndex > 30);

            //    // let all remaining trackers process the current frame
            //    var img = UnmanagedImage.FromManagedImage(maskedFrame);
            //    trackers
            //        .ForEach(t => t.Tracker.ProcessFrame(img));

            //    // remember this frame for next iteration
            //    previousFrame.Dispose();
            //    previousFrame = frame.Clone() as Bitmap;


            //    // draw labels on all tracked cars
            //    var outputFrame = frame.Clone() as Bitmap;
            //    trackers
            //        .FindAll(t => !t.Tracker.TrackingObject.IsEmpty)
            //        .ForEach(t => DrawCarLabel(outputFrame, t.Tracker.TrackingObject.Rectangle, t.CarNumber));


            //    // return the processed frame to the video
            //    frame = outputFrame;
            //}

            //// or else just remember this frame for next iteration
            //else
            //    previousFrame = frame.Clone() as Bitmap;

            //frameIndex++;
        }
Esempio n. 9
0
        private void Cap_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {
                Mat imagen = new Mat();
                cap.Retrieve(imagen);
                //pb_lprptzanalitica.Image = imagen.Bitmap;

                Bitmap frame = new Bitmap(imagen.Bitmap);
                //pb_ipcam.Image = frame.Clone() as Bitmap;

                if (previousFrame != null)
                {
                    // find the thresholded euclidian difference between two subsequent frames
                    //ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(40);
                    ThresholdedEuclideanDifference threshold = new ThresholdedEuclideanDifference(thr);
                    threshold.OverlayImage = previousFrame;
                    var difference = threshold.Apply(frame.Clone() as Bitmap);

                    // only keep big blobs
                    var filter = new BlobsFiltering();
                    filter.CoupledSizeFiltering = true;
                    filter.MinHeight            = int.Parse(txt_tamanio2.Text);
                    filter.MinWidth             = int.Parse(txt_tamanio2.Text);
                    filter.ApplyInPlace(difference);



                    //var sobl = new SobelEdgeDetector();
                    //sobl.ApplyInPlace(difference);

                    //erode image
                    var erode = new Erosion3x3();
                    for (int i = 0; i < int.Parse(txt_ers.Text); i++)
                    {
                        erode.ApplyInPlace(difference);
                        //erode.ApplyInPlace(difference);
                        //erode.ApplyInPlace(difference);
                    }

                    // dilate remaining blobs
                    var dilate = new BinaryDilation3x3();
                    for (int i = 0; i < int.Parse(txt_dils.Text); i++)
                    {
                        dilate.ApplyInPlace(difference);
                        //dilate.ApplyInPlace(difference);
                        //dilate.ApplyInPlace(difference);
                        //dilate.ApplyInPlace(difference);
                    }

                    // put this image in the thresholded picturebox
                    thresholdedBox.Image = difference.Clone() as Bitmap;

                    // use this as a mask for the current frame
                    var mask        = new ApplyMask(difference);
                    var maskedFrame = mask.Apply(frame);

                    // put this image in the masked picturebox
                    maskedBox.Image = maskedFrame.Clone() as Bitmap;

                    // now find all moving blobs
                    if (frameIndex % 10 == 0)
                    {
                        var counter = new BlobCounter();
                        counter.ProcessImage(difference);

                        // only keep blobs that:
                        //     - do not overlap with known cars
                        //     - do not overlap with other blobs
                        //     - have crossed the middle of the frame
                        //     - are at least 100 pixels tall
                        var blobs    = counter.GetObjectsRectangles();
                        var newBlobs = from r in counter.GetObjectsRectangles()
                                       where !trackers.Any(t => t.Tracker.TrackingObject.Rectangle.IntersectsWith(r)) &&
                                       !blobs.Any(b => b.IntersectsWith(r) && b != r) &&
                                       r.Top >= 240 && r.Bottom <= 480 &&
                                       r.Height >= int.Parse(txt_tamanio.Text)
                                       select r;

                        // set up new camshift trackers for each detected blob
                        foreach (var rect in newBlobs)
                        {
                            trackers.Add(new TrackerType(rect, frameIndex, ++carIndex));
                        }
                    }

                    // now kill all car trackers that have expanded by too much
                    trackers.RemoveAll(t => t.Tracker.TrackingObject.Rectangle.Height > 360);

                    // and kill all trackers that have lived for 30 frames
                    //trackers.RemoveAll(t => frameIndex - t.StartIndex > 30);
                    trackers.RemoveAll(t => frameIndex - t.StartIndex > frames);

                    // let all remaining trackers process the current frame
                    var img = UnmanagedImage.FromManagedImage(maskedFrame);
                    trackers
                    .ForEach(t => t.Tracker.ProcessFrame(img));

                    // remember this frame for next iteration
                    previousFrame.Dispose();
                    previousFrame = frame.Clone() as Bitmap;


                    //escribir etiqueta para cada vehiculo
                    var outputFrame = frame.Clone() as Bitmap;
                    trackers
                    .FindAll(t => !t.Tracker.TrackingObject.IsEmpty)
                    .ForEach(t => DrawCarLabel(outputFrame, t.Tracker.TrackingObject.Rectangle, t.CarNumber));


                    // regresar frame procesado
                    frame = outputFrame;
                    //pb_ipcam.Image = outputFrame;
                    pb_lprptzanalitica.Image = outputFrame;
                }
                else
                {   // recordar para siguiente iteracion
                    previousFrame = frame.Clone() as Bitmap;
                }

                frameIndex++;
            }
            catch (Exception)
            {
                MessageBox.Show("Error al decodificar video de PTZ, LPR o ANALITICA");
            }
        }
        private void vspMain_NewFrame(object sender, ref Bitmap image)
        {
            cntFrame++;
            if (cntFrame < 80)
            {
                return;
            }
            colorFiltering.Red   = new Accord.IntRange(230, 245);
            colorFiltering.Green = new Accord.IntRange(200, 220);
            colorFiltering.Blue  = new Accord.IntRange(150, 180);

            var filteringFrame = colorFiltering.Apply(image);
            var afterGray      = grayscaleBT709.Apply(filteringFrame);

            var thersholdedFrame = threshold.Apply(afterGray);

            pbThreshold.Image = afterGray;

            var dilatedFrame = dilation.Apply(thersholdedFrame);

            ApplyMask applyMask   = new ApplyMask(dilatedFrame);
            var       maskedFrame = applyMask.Apply(image);
            var       blobFrame   = maskedFrame.Clone() as Bitmap;

            pbMasked.Image = maskedFrame;

            blobCounter.ProcessImage(blobFrame);
            var info       = blobCounter.GetObjectsInformation();
            var rectangles = info.Where(w => w.Area >= 200).Select(s => s.Rectangle).ToList();

            //var rectangles = blobCounter.GetObjectsRectangles();
            if (rectangles.Count == 0)
            {
                missFrame++;
                return;
            }
            var rectangle = rectangles[0];

            lstHeight.Add(rectangle.Y);

            if (lstHeight.Count >= 2)
            {
                lstHeight.Remove(0);
            }

            //if(cntFrame >=130)
            //    lastRect = rectangle;
            //rectangle = rectangles.Where(w => Math.Abs(lastRect.X - w.X) < 20).FirstOrDefault();
            if (rectangle == null)
            {
                return;
            }
            Graphics g = Graphics.FromImage(image);

            rectangle.Width  = 50;
            rectangle.Height = 100;
            //rectangle.Y = lstHeight.Sum(s => s) / lstHeight.Count;
            g.DrawRectangle(new Pen(Color.Yellow, 3), rectangle);

            var img = UnmanagedImage.FromManagedImage(blobFrame);

            camshift.SearchWindow = rectangle;
            camshift.ProcessFrame(img);
            var trackRect = camshift.TrackingObject.Rectangle;

            if (trackRect == null)
            {
                return;
            }
            trackRect.Inflate(30, 30);
            g.DrawRectangle(new Pen(Color.Aqua, 3), trackRect);
        }