public Image <Bgr, Byte> Draw(KeyFrame keyFrame, Image <Bgr, Byte> observedImage, List <KeyFrame> keyframes = null) { //FindMatch(modelImage, observedImage, keyframes); if (matches.Size == mask.Rows) { Features2DToolbox.DrawMatches(keyFrame.Frame, keyFrame.KeyPoints, observedImage, observedKeyPoints, matches, result, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 255), mask, Features2DToolbox.KeypointDrawType.NotDrawSinglePoints); } else { Features2DToolbox.DrawMatches(keyFrame.Frame, keyFrame.KeyPoints, observedImage, observedKeyPoints, matches, result, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 255), null, Features2DToolbox.KeypointDrawType.NotDrawSinglePoints); } #region draw the projected region on the image if (homography != null) { //draw a rectangle along the projected model Rectangle rect = new Rectangle(Point.Empty, keyFrame.Frame.Size); PointF[] pts = new PointF[] { new PointF(rect.Left, rect.Bottom), new PointF(rect.Right, rect.Bottom), new PointF(rect.Right, rect.Top), new PointF(rect.Left, rect.Top) }; //pts = CvInvoke.PerspectiveTransform(pts, homography); Point[] points = Array.ConvertAll <PointF, Point>(pts, Point.Round); using (VectorOfPoint vp = new VectorOfPoint(points)) { CvInvoke.Polylines(result, vp, true, new MCvScalar(255, 0, 0, 255), 5); } } #endregion //modelImage.Dispose(); return(result.ToImage <Bgr, Byte>()); }
private static void withKeyframes(bool isFast, string videoSource = null) { ImageViewer viewer = new ImageViewer(); try { Capture capture = videoSource != null ? new Capture(videoSource) : new Capture(0); Image <Bgr, Byte> result = null; KeyFrame keyFrame = null; Application.Idle += new EventHandler(delegate(object sender, EventArgs e) { counter++; if (keyFrames.Count < 15) { IDrawer drawer; if (isFast) { drawer = new FastDrawer(); } else { drawer = new SURFDrawer(); } try { Mat frame = capture.QueryFrame(); if (frame.Width > 720 || frame.Height > 480) { double width = 720.0 / frame.Width; double height = 480.0 / frame.Height; CvInvoke.Resize(frame, frame, new Size(), width, height, Inter.Linear); } Image <Bgr, Byte> framebuffer = frame.ToImage <Bgr, Byte>(); if (keyFrames.Count == 0) { keyFrames.Add(new KeyFrame() { Frame = framebuffer }); } for (int i = keyFrames.Count - 1; i >= 0; i--) { KeyFrame kf = keyFrames[i]; drawer.FindMatch(kf, framebuffer, keyFrames); if (counter == 65) { //homography ile camera pozu ayarla, nesneyi koy } if (drawer.homography != null) { KeyFrame buffer_kf = keyFrames[keyFrames.Count - 1]; if (kf != buffer_kf) { keyFrames[i] = buffer_kf; keyFrames[keyFrames.Count - 1] = kf; } keyFrame = kf; break; } if (i == 0) { if (drawer.homography == null) { keyFrames.Add(new KeyFrame() { Frame = framebuffer, KeyPoints = drawer.observedKeyPoints, Descriptors = drawer.observedDescriptors }); } } drawer.Clear(); } result = frame.ToImage <Bgr, Byte>(); if (keyFrame != null) { result = drawer.Draw(keyFrame, framebuffer); } viewer.Width = frame.Width * 2 + 50; viewer.Image = result; //result.Dispose(); } catch (Exception ex1) { } finally { result.Dispose(); drawer.Dispose(); } } else { keyFrames.RemoveRange(0, keyFrames.Count / 2); } }); } catch { } viewer.ShowDialog(); }