/// <summary> /// Handles a frame arrived event and renders the frame to the screen. /// </summary> private void ColorFrameReader_FrameArrivedAsync(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { var frame = sender.TryAcquireLatestFrame(); if (frame != null) { SoftwareBitmap originalBitmap = null; var inputBitmap = frame.VideoMediaFrame?.SoftwareBitmap; if (inputBitmap != null) { // The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha // The frame reader as configured in this sample gives BGRA8 with straight alpha, so need to convert it originalBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmap outputBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, originalBitmap.PixelWidth, originalBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); // Operate on the image in the manner chosen by the user. if (currentOperation == OperationType.Blur) { operation.Blur(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.HoughLines) { operation.HoughLines(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.Contours) { operation.Contours(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.Canny) { operation.Canny(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.MotionDetector) { operation.MotionDetector(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.Histogram) { #if USEOCVHERLPER App.CvHelper.Histogram(originalBitmap, outputBitmap); #else // MP! Todo: Implement C# version in OcvOp. #endif } // Display both the original bitmap and the processed bitmap. previewRenderer.RenderFrame(originalBitmap); outputRenderer.RenderFrame(outputBitmap); } Interlocked.Increment(ref frameCount); } }
/// <summary> /// Handles a frame arrived event and renders the frame to the screen. /// </summary> private async Task ProcessWithOpenCV(VideoFrame frame) { if (frame != null) { SoftwareBitmap originalBitmap = null; var inputBitmap = frame.SoftwareBitmap; if (inputBitmap != null) { try { // The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha // The frame reader as configured in this sample gives BGRA8 with straight alpha, so need to convert it originalBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); SoftwareBitmap outputBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, originalBitmap.PixelWidth, originalBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); // Operate on the image in the manner chosen by the user. if (currentOperation == OperationType.Blur) { operation.Blur(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.HoughLines) { operation.HoughLines(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.Contours) { operation.Contours(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.Canny) { operation.Canny(originalBitmap, outputBitmap, storeditem); } else if (currentOperation == OperationType.MotionDetector) { } // Display both the original bitmap and the processed bitmap. previewRenderer.RenderFrame(originalBitmap); outputRenderer.RenderFrame(outputBitmap); } catch (Exception ex) { await(new MessageDialog(ex.Message)).ShowAsync(); } } Interlocked.Increment(ref frameCount); } }