/// <summary> /// Initializes a new DetectorVideoSink. /// </summary> /// <param name="detector">The CaptureSourceMarkerDetector to use.</param> public UWPMediaSink(CaptureSourceMarkerDetector detector) { this.detector = detector; listener.ConnectionReceived += Listener_ConnectionReceived; displayOrientation = displayInformation.CurrentOrientation; displayInformation.OrientationChanged += DisplayInformation_OrientationChanged; }
void MainPage_Loaded(object sender, RoutedEventArgs e) { captureSource = new CaptureSource { VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice() }; var videoBrush = new VideoBrush(); videoBrush.SetSource(captureSource); Viewport.Fill = videoBrush; markerDetector = new CaptureSourceMarkerDetector(); var marker = Marker.LoadFromResource("Bola.pat", 64, 64, 80); markerDetector.Initialize(captureSource, 1d, 4000d, marker); markerDetector.MarkersDetected += (obj, args) => { Dispatcher.BeginInvoke(() => { var results = args.DetectionResults; if (results.HasResults) { var centerAtOrigin = Matrix3DFactory. CreateTranslation( -Imagem.ActualWidth* 0.5, -Imagem. ActualHeight* 0.5, 0); var scale = Matrix3DFactory.CreateScale (0.5, -0.5, 0.5); var world = centerAtOrigin* scale* results[0]. Transformation; var vp = Matrix3DFactory. CreateViewportTransformation (Viewport.ActualWidth, Viewport.ActualHeight); var m = Matrix3DFactory. CreateViewportProjection (world, Matrix3D.Identity, markerDetector. Projection, vp); Imagem.Projection = new Matrix3DProjection {ProjectionMatrix = m}; } }); }; }
private void Initialize() { try { // Init variables timedRotation = 0; Scale = 0.4; Rotate = 0; // Init capture source captureSource = new CaptureSource(); captureSource.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice(); // Desired format is 320 x 240 (good tracking results and performance) captureSource.VideoCaptureDevice.DesiredFormat = new VideoFormat(PixelFormatType.Unknown, 320, 240, 30); // Init AR markerSlar = Marker.LoadFromResource("data/Marker_SLAR_16x16segments_80width.pat", 16, 16, 80.0, "SLAR"); markerL = Marker.LoadFromResource("data/Marker_L_16x16segments_80width.pat", 16, 16, 80.0, "L"); ArDetector = new CaptureSourceMarkerDetector(captureSource, 1, 4000, new List<Marker> { markerSlar, markerL }); AttachAREvent(); // Init Rest projectionMatrix = Matrix3DFactory.CreatePerspectiveFieldOfViewRH(0.7, ViewportContainer.Width / ViewportContainer.Height, 1, 4000); this.DataContext = this; // Start all SetARObject(); RunUpdate(); } catch (Exception ex) { var builder = new StringBuilder(); foreach (var sf in captureSource.VideoCaptureDevice.SupportedFormats) { builder.AppendFormat("{0}: {1} x {2} @ {3} fps. Stride: {4}\r\n", sf.PixelFormat, sf.PixelWidth, sf.PixelHeight, sf.FramesPerSecond, sf.Stride); } MessageBox.Show("Error during initialization. Please make sure a default webcam was set.\r\nSupported formats:\r\n" + builder.ToString() + "\r\n\r\n" + ex.ToString(), "Error during init.", MessageBoxButton.OK); throw; } }
private void InitializeDetector() { // Initialize the Detector arDetector = new CaptureSourceMarkerDetector(); // Load the marker pattern. It has 16x16 segments and a width of 80 millimeters var marker = Marker.LoadFromResource("Marker_SLAR_16x16segments_80width.pat", 16, 16, 80); // The perspective projection has the near plane at 1 and the far plane at 4000 arDetector.Initialize(captureSource, 1, 4000, new List<Marker> { marker }, adaptive.IsChecked.Value); // Attach the AR detection event handler // The event is fired if at least one marker was detected arDetector.MarkersDetected += (s, me) => { // Change to UI thread in order to manipulate the text control's projection Dispatcher.BeginInvoke(() => { // Calculate the projection matrix var dr = me.DetectionResults; if (dr.HasResults) { // Center at origin of the TextBlock var centerAtOrigin = Matrix3DFactory.CreateTranslation(-Txt.ActualWidth * 0.5, -Txt.ActualHeight * 0.5, 0); // Swap the y-axis and scale down by half var scale = Matrix3DFactory.CreateScale(0.5, -0.5, 0.5); // 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 vp = Matrix3DFactory.CreateViewportTransformation(Viewport.ActualWidth, Viewport.ActualHeight); var m = Matrix3DFactory.CreateViewportProjection(world, Matrix3D.Identity, arDetector.Projection, vp); // Apply the final transformation matrix to the TextBox Txt.Projection = new Matrix3DProjection { ProjectionMatrix = m }; } }); }; }
/// <summary> /// Initializes a new DetectorVideoSink. /// </summary> /// <param name="detector">The CaptureSourceMarkerDetector to use.</param> public DetectorVideoSink(CaptureSourceMarkerDetector detector) : base() { this.detector = detector; }