public void trackImgs() { long matchTime = 0; float X_center, Y_center; float theta; Image <Gray, byte> modelImg; Image <Gray, byte> observedImg; modelImg = new Image <Gray, byte>((Bitmap)Image.FromFile(modelPanelImg)); observedImg = new Image <Gray, byte>((Bitmap)Image.FromFile(previewPanelImg)); Mat result = SURFFeatureExample.DrawMatches.Draw(modelImg.Mat, observedImg.Mat, out matchTime, out X_center, out Y_center, out theta); previewPanel.BackgroundImage = (Bitmap)result.Bitmap; this.Invoke(new MethodInvoker(delegate() { if (previewPanel.BackgroundImage != null) { dataGridView1.Rows[0].Cells[0].Value = matchTime.ToString("F") + "ms"; dataGridView1.Rows[0].Cells[1].Value = X_center.ToString(); dataGridView1.Rows[0].Cells[2].Value = Y_center.ToString(); dataGridView1.Rows[0].Cells[3].Value = theta.ToString(); } })); }
private void TrackObject_NewFrame(object sender, NewFrameEventArgs eventArgs) { long matchTime = 0; float X_center, Y_center; float theta; Image <Gray, byte> modelImg; Image <Gray, byte> observedImg; //VectorOfKeyPoint modelKeyPoints = new VectorOfKeyPoint(), observedKeyPoints = new VectorOfKeyPoint(); //VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch(); //Mat mask = new Mat(), homography = new Mat(); modelImg = new Image <Gray, byte>((Bitmap)ModelPreview.BackgroundImage); observedImg = new Image <Gray, byte>((Bitmap)eventArgs.Frame.Clone()); Mat result = SURFFeatureExample.DrawMatches.Draw(modelImg.Mat, observedImg.Mat, out matchTime, out X_center, out Y_center, out theta); previewPanel.BackgroundImage = (Bitmap)result.Bitmap; this.Invoke(new MethodInvoker(delegate() { if (previewPanel.BackgroundImage != null) { dataGridView1.Rows[0].Cells[0].Value = matchTime.ToString("F") + "ms"; dataGridView1.Rows[0].Cells[1].Value = X_center.ToString(); dataGridView1.Rows[0].Cells[2].Value = Y_center.ToString(); dataGridView1.Rows[0].Cells[3].Value = theta.ToString(); } })); selectedCamera.videoSource.NewFrame -= new NewFrameEventHandler(TrackObject_NewFrame); /* * System.Drawing.Image SRCimage = (Bitmap)eventArgs.Frame.Clone(); * Bitmap SRCbmpImage = new Bitmap(SRCimage); * Image<Bgr, byte> source = new Image<Bgr, byte>(SRCbmpImage); * * System.Drawing.Image TMPimage = (Bitmap)ModelPreview.BackgroundImage; * Bitmap TMPbmpImage = new Bitmap(TMPimage); * Image<Bgr, byte> template = new Image<Bgr, byte>(TMPbmpImage); * * Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed); * * double[] minValues, maxValues; * Point[] minLocations, maxLocations; * result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations); * * if ((maxValues[0] * 100) >= (double)accuracyThreshold.Value) * { * Rectangle rect = new Rectangle(maxLocations[0], template.Size); * Graphics graphic = Graphics.FromImage(SRCimage); * Pen pen = new Pen(Color.Green, 2); * * graphic.DrawRectangle(pen, rect); * * this.Invoke(new MethodInvoker(delegate () * { * if (previewPanel.BackgroundImage != null) * { * previewPanel.BackgroundImage.Dispose(); * } * * // Update the form with relevant data * previewPanel.BackgroundImage = SRCimage; * dataGridView1.Rows[0].Cells[0].Value = (maxValues[0] * 100).ToString("F") + "%"; * })); * }else * * if ((maxValues[0] * 100) >= (double)accuracyThreshold.Value - 10) * { * Rectangle rect = new Rectangle(maxLocations[0], template.Size); * Graphics graphic = Graphics.FromImage(SRCimage); * Pen pen = new Pen(Color.Blue, 2); * * graphic.DrawRectangle(pen, rect); * * this.Invoke(new MethodInvoker(delegate () * { * if (previewPanel.BackgroundImage != null) * { * previewPanel.BackgroundImage.Dispose(); * } * * // Update the form with relevant data * previewPanel.BackgroundImage = SRCimage; * dataGridView1.Rows[0].Cells[0].Value = (maxValues[0] * 100).ToString("F") + "%"; * })); * } * else * { * this.Invoke(new MethodInvoker(delegate () * { * if (previewPanel.BackgroundImage != null) * { * previewPanel.BackgroundImage.Dispose(); * } * * // Update the form with relevant data * previewPanel.BackgroundImage = SRCimage; * dataGridView1.Rows[0].Cells[0].Value = (maxValues[0] * 100).ToString("F") + "%"; * })); * } */ }