private void DetectMarkers(WriteableBitmap bmp) { // Init. here because the captureSource.VideoCaptureDevice.DesiredFormat getter is not reliable and throws an Exception if (!isInit) { // Init AR arDetector.Initialize(bmp.PixelWidth, bmp.PixelHeight, Game.Camera.Near, Game.Camera.Far, new List <Marker> { slarMarker }); isInit = true; } // Detect var dr = arDetector.DetectAllMarkers(bmp); // Reused for marker highlighting bmp.Clear(); ViewportOverlay.Source = bmp; if (dr.HasResults) { var transformation = Balder.Math.Matrix.CreateTranslation(0, -5, 0) * Balder.Math.Matrix.CreateRotationX(90) * dr[0].Transformation.ToBalder(); Game.SetWorldMatrix(transformation); // Highlight detected markers var txt = String.Empty; foreach (var r in dr) { bmp.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red); txt += String.Format("{0}.Confidence = {1:0.00} ", r.Marker.Name, r.Confidence); } } }
private void DetectMarkers(WriteableBitmap bmp) { // Init. here because the captureSource.VideoCaptureDevice.DesiredFormat getter is not reliable and throws an Exception if (!isInit) { InitializeDetector(bmp.PixelWidth, bmp.PixelHeight); isInit = true; } // Detect var dr = arDetector.DetectAllMarkers(bmp); // Reused for marker highlighting bmp.Clear(); ViewportOverlay.Source = bmp; if (dr.HasResults) { // Set camera camera.ProjectionTransform = arDetector.Projection.ToXnaMatrix(); // Transform Sun var sunResult = dr.FirstOrDefault(d => d.Marker == lMarker); if (sunResult != null && sunResult.Confidence > MinConfidence) { Transform(Sun, sunResult.Transformation, TxtSun); } else { TxtSun.Visibility = Visibility.Collapsed; Sun.IsVisible = false; } // Transform Earth var earthResult = dr.FirstOrDefault(d => d.Marker == slarMarker); if (sunResult == null && earthResult == null) { earthResult = dr[0]; } if (earthResult != null && earthResult.Confidence > MinConfidence) { Transform(Earth, earthResult.Transformation, TxtEarth); } else { TxtEarth.Visibility = Visibility.Collapsed; Earth.IsVisible = false; } //// Highlight detected markers //var txt = String.Empty; //foreach (var r in dr) //{ // bmp.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red); // txt += String.Format("{0}.Confidence = {1:0.00} ", r.Marker.Name, r.Confidence); //} //Txt.Text = txt; } }
private void GrabFrame() { if (camera != null) { WriteableBitmap wb = new WriteableBitmap(640, 480); camera.GetCurrentFrame(wb); wb.Invalidate(); var sw = new Stopwatch(); sw.Start(); var results = bmdList.DetectAllMarkers(wb); if (results.HasResults) { string direction = displayAR(results); if (direction == "Left") { rightarrow.Visibility = Visibility.Collapsed; okay.Visibility = Visibility.Collapsed; wrong.Visibility = Visibility.Collapsed; leftarrow.Visibility = Visibility.Visible; } else if (direction == "Right") { leftarrow.Visibility = Visibility.Collapsed; okay.Visibility = Visibility.Collapsed; wrong.Visibility = Visibility.Collapsed; rightarrow.Visibility = Visibility.Visible; } else if (direction == "Okay") { rightarrow.Visibility = Visibility.Collapsed; leftarrow.Visibility = Visibility.Collapsed; wrong.Visibility = Visibility.Collapsed; okay.Visibility = Visibility.Visible; } else if (direction == "Wrong") { rightarrow.Visibility = Visibility.Collapsed; leftarrow.Visibility = Visibility.Collapsed; okay.Visibility = Visibility.Collapsed; wrong.Visibility = Visibility.Visible; } } else { rightarrow.Visibility = Visibility.Collapsed; leftarrow.Visibility = Visibility.Collapsed; okay.Visibility = Visibility.Collapsed; wrong.Visibility = Visibility.Collapsed; } sw.Stop(); } Dispatcher.BeginInvoke(() => { GrabFrame(); }); }
private void DetectMarkers(WriteableBitmap bmp) { // Init. here because the captureSource.VideoCaptureDevice.DesiredFormat getter is not reliable and throws an Exception if (arDetector == null) { InitializeDetector(bmp.PixelWidth, bmp.PixelHeight); } // Detect var detectedResults = arDetector.DetectAllMarkers(bmp); // Reused for marker highlighting bmp.Clear(); ViewportOverlay.Source = bmp; if (detectedResults.HasResults) { var trans = detectedResults[0].Transformation.ToBalderMatrix(); if (detectedResults.Count > 1) { // Calculate distance vector wayInterpol += (3f * sign); var trans2 = detectedResults[1].Transformation.ToBalderMatrix(); var p1 = new Vector(trans[3, 0], trans[3, 1], trans[3, 2]); var p2 = new Vector(trans2[3, 0], trans2[3, 1], trans2[3, 2]); var d = p1 - p2; var dn = Vector.Normalize(d); var p = dn * -wayInterpol; // Turning point ? if (p.LengthSquared() > d.LengthSquared()) { sign = -1; } else if (wayInterpol < 0) { sign = 1; } var translate = Matrix.CreateTranslation(p); // Calculate Yaw and align the mesh to it var dot = Vector.Dot(Vector.UnitY, dn); var rotRad = Math.Acos(dot); var rotDeg = MathHelper.ToDegrees((float)rotRad); var yaw = Matrix.CreateRotationZ(150 + rotDeg + (sign > 0 ? 0 : 180)); // Combine matrix trans = yaw * trans * translate; } else { // If no 2nd marker was found only rotate around z axis timedRotation += 2; trans = Matrix.CreateRotationZ(timedRotation) * trans; } // Transform the mesh and change coordinate system ApplyFinalTransformation(trans); // Highlight detected markers using the WriteableBitmapEx foreach (var r in detectedResults) { bmp.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red); } } }