private void squareDilateToolStripMenuItem_Click(object sender, EventArgs e) { Dilation filter = new Dilation(square); Image = filter.Apply(Image); }
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); }