private void Detect() { //Here is where we try to detect the marker if (isDetecting || !isInitialized) { return; } isDetecting = true; try { // Update buffer size var pixelWidth = photoCamera.PreviewResolution.Width; var pixelHeight = photoCamera.PreviewResolution.Height; if (buffer == null || buffer.Length != pixelWidth * pixelHeight) { buffer = new byte[System.Convert.ToInt32(pixelWidth * pixelHeight)]; } // Grab snapshot for the marker detection photoCamera.GetPreviewBufferY(buffer); //Detect the markers arDetector.Threshold = 100; DetectionResults dr = arDetector.DetectAllMarkers(buffer, System.Convert.ToInt32(pixelWidth), System.Convert.ToInt32(pixelHeight)); GameState.getInstance().Detect(dr); } finally { isDetecting = false; } }
private void Detect() { if (isDetecting || !isInitialized) { return; } if (null == videoCapture) { return; } isDetecting = true; try { byte[] buffer = null; if (config.GetBool("CameraSetup", "Mirror")) { buffer = videoCapture.BufferGray; // the gray buffer is mirrored by default } else { buffer = Utils.FlipHorizontalGrayscale(videoCapture.BufferGray, videoCapture.GrayWidth, videoCapture.GrayHeight); } //Detect the markers var results = arDetector.DetectAllMarkers(buffer, videoCapture.GrayWidth, videoCapture.GrayHeight); DetectionResults = results; } finally { isDetecting = false; } }
private void Detect() { if (isDetecting || !isInitialized) { return; } isDetecting = true; var stopwatch = Stopwatch.StartNew(); try { // Update buffer size var pixelWidth = (int)photoCamera.PreviewResolution.Width; var pixelHeight = (int)photoCamera.PreviewResolution.Height; if (buffer == null || buffer.Length != pixelWidth * pixelHeight) { buffer = new byte[pixelWidth * pixelHeight]; // Create constant transformations instances // Center at origin of the 256x256 controls centerAtOrigin = Matrix3DFactory.CreateTranslation(-128, -128, 0); // Swap the y-axis and scale down by half scale = Matrix3DFactory.CreateScale(0.5, -0.5, 0.5); // Viewport transformation viewport = Matrix3DFactory.CreateViewportTransformation(pixelWidth, pixelHeight); matrix3DProjection = new Matrix3DProjection(); Txt.Projection = matrix3DProjection; Img.Projection = matrix3DProjection; } // Grab snapshot photoCamera.GetPreviewBufferY(buffer); // Detect var dr = arDetector.DetectAllMarkers(buffer, pixelWidth, pixelHeight); // Draw the detected squares //bitmap.Clear(); //ViewportOverlay.Source = bitmap; // Calculate the projection matrix if (dr.HasResults) { // Calculate the complete transformation matrix based on the first detection result var world = centerAtOrigin * scale * dr[0].Transformation; // Calculate the final transformation matrix by using the camera projection matrix var m = Matrix3DFactory.CreateViewportProjection(world, Matrix3D.Identity, arDetector.Projection, viewport); // Apply the final transformation matrix to the TextBox matrix3DProjection.ProjectionMatrix = m; //// Draw the detected squares //foreach (var r in dr) //{ // bitmap.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); //} } } finally { isDetecting = false; stopwatch.Stop(); TxtDiag.Text = string.Format("{0} ms", stopwatch.ElapsedMilliseconds); } }