Ejemplo n.º 1
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++;
        }
Ejemplo n.º 2
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++;
        }
Ejemplo n.º 3
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++;
        }
Ejemplo n.º 4
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");
            }
        }