public mDifferenceEuclidean(Bitmap UnderlayBitmap, Bitmap OverlayBitmap, int ThresholdValue) { Threshold = ThresholdValue; BitmapUnder = new mSetFormat(UnderlayBitmap, mFilter.BitmapTypes.Rgb24bpp).ModifiedBitmap; BitmapOver = new mSetFormat(OverlayBitmap, mFilter.BitmapTypes.Rgb24bpp).ModifiedBitmap; ModifiedBitmap = UnderlayBitmap; Effect = new ThresholdedEuclideanDifference(Threshold); Effect.OverlayImage = BitmapOver; ModifiedBitmap = Effect.Apply(BitmapUnder); }
/// <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++; }
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) { video = (Bitmap)eventArgs.Frame.Clone(); video2 = (Bitmap)eventArgs.Frame.Clone(); //Color Tracking if (trackchoice == 0) { if (radioButton1.Checked == true) { HSLFiltering filter = new HSLFiltering(); filter.Hue = new IntRange((int)HueMin, (int)HueMax); filter.Saturation = new Range(SatMin, SatMax); filter.Luminance = new Range(LumMin, LumMax); filter.ApplyInPlace(video2); objec(video2); } if (radioButton2.Checked == true) { // create filter EuclideanColorFiltering filter = new EuclideanColorFiltering(); // set center colol and radius filter.CenterColor = rgb; filter.Radius = range; // apply the filter filter.ApplyInPlace(video2); objec(video2); } } // motion Tracking if (trackchoice == 1) { currentImage = (Bitmap)eventArgs.Frame.Clone(); Bitmap tmpimage = Grayscale.CommonAlgorithms.BT709.Apply(currentImage), copyCurrentImage = currentImage; if (backgroundImage != null) { ThresholdedEuclideanDifference thd = new ThresholdedEuclideanDifference(30); thd.OverlayImage = backgroundImage; tmpimage = thd.Apply(tmpimage); } BlobCounter blobcounter = new BlobCounter(); blobcounter.MinHeight = minheight; blobcounter.MinWidth = minwidth; blobcounter.FilterBlobs = true; blobcounter.ObjectsOrder = ObjectsOrder.Area; blobcounter.ProcessImage(tmpimage); Rectangle[] rects = blobcounter.GetObjectsRectangles(); foreach (Rectangle rect in rects) { if (rects.Length >= 5) { Rectangle objectRect = rects[0]; g = Graphics.FromImage(currentImage); using (Pen pen = new Pen(Color.Red, 6)) { g.DrawRectangle(pen, objectRect); } g.Dispose(); } } backgroundImage = Grayscale.CommonAlgorithms.BT709.Apply(copyCurrentImage); pictureBox1.Image = copyCurrentImage; pictureBox2.Image = tmpimage; } if (trackchoice == 2) { if (radioButton1.Checked == true) { HSLFiltering filter = new HSLFiltering(); filter.Hue = new IntRange((int)HueMin, (int)HueMax); filter.Saturation = new Range(SatMin, SatMax); filter.Luminance = new Range(LumMin, LumMax); filter.ApplyInPlace(video2); //objec(video2); } if (radioButton2.Checked == true) { // create filter EuclideanColorFiltering filter = new EuclideanColorFiltering(); // set center colol and radius filter.CenterColor = rgb; filter.Radius = range; //filter.Radius = 100; // apply the filter filter.ApplyInPlace(video2); // objec(video2); } BlobCounter blobCounter = new BlobCounter(); blobCounter.MinWidth = minwidth; blobCounter.MinHeight = minheight; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size; blobCounter.ProcessImage(video2); Rectangle[] recs = blobCounter.GetObjectsRectangles(); pictureBox2.Image = video2; foreach (Rectangle rect in recs) { if (recs.Length >= 0) { Rectangle objectRect = recs[0]; x1 = objectRect.X; y1 = objectRect.Y; wdth = objectRect.Width; hgth = objectRect.Height; } } currentImage = (Bitmap)eventArgs.Frame.Clone(); Bitmap tmpimage = Grayscale.CommonAlgorithms.BT709.Apply(currentImage), copyCurrentImage = currentImage; if (backgroundImage != null) { ThresholdedEuclideanDifference thd = new ThresholdedEuclideanDifference(50); thd.OverlayImage = backgroundImage; tmpimage = thd.Apply(tmpimage); BlobCounter blobcounter = new BlobCounter(); blobcounter.MinHeight = minheight; blobcounter.MinWidth = minwidth; blobcounter.FilterBlobs = true; blobcounter.ObjectsOrder = ObjectsOrder.Area; blobcounter.ProcessImage(tmpimage); Rectangle[] rects = blobcounter.GetObjectsRectangles(); foreach (Rectangle rect in rects) { if (rects.Length >= 5) // { Rectangle objectRect = rects[0]; x2 = objectRect.X; y2 = objectRect.Y; if ((x1 - 50 < x2) && (x2 < x1 + 50)) { if ((y1 - 50 < y2) && (y2 < y1 + 50)) { track = true; counter = 25; } } else { counter--; } if (counter == 0) { track = false; } if (track == true && counter > 0) { g = Graphics.FromImage(copyCurrentImage); using (Pen pen = new Pen(Color.FromArgb(252, 3, 26), 6)) { g.DrawRectangle(pen, x1, y1, wdth, hgth); } g.Dispose(); } if (CheckCordinates.Checked) { this.Invoke((MethodInvoker) delegate { richTextBox1.Text = "X=[" + x1 + "], Y=[" + y1 + "]" + "\n" + richTextBox1.Text + "\n";; }); } } } } backgroundImage = Grayscale.CommonAlgorithms.BT709.Apply(copyCurrentImage); pictureBox1.Image = copyCurrentImage; } }
/// <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++; }
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"); } }