public static WriteableBitmap GetMiddleGaussianImage(WriteableBitmap writeableBitmap) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var height = writeableBitmap.PixelHeight; var width = writeableBitmap.PixelWidth; var cropedWriteableBitmap = writeableBitmap.Crop(new Rect(new Point(0, height / 3), new Point(width, height / 3 * 2))); WriteableBitmap resizedWriteableBitmap = cropedWriteableBitmap.Resize(width / 8, height / 8, WriteableBitmapExtensions.Interpolation.Bilinear); WriteableBitmap gaussianBackground = resizedWriteableBitmap.Convolute(WriteableBitmapExtensions.KernelGaussianBlur5x5); stopwatch.Stop(); Debug.WriteLine("GetMiddleGaussianImage time:" + stopwatch.ElapsedMilliseconds.ToString()); return gaussianBackground; }
private WriteableBitmap FindPlate(IEnumerable<Rect> rects, WriteableBitmap image) { WriteableBitmap bestCandidate = null; foreach (var rect in rects) { var croppedImage = image.Crop(rect); var edgeFilter = new CannyEdgeDetector(); var smoothFilter = new Median(); var grayFilter = new Grayscale(0.2125, 0.7154, 0.0721); var blobCounter = new BlobCounter(); var cutTop = croppedImage.PixelHeight * 0.3; croppedImage = croppedImage.Crop(new Rect(0, cutTop, croppedImage.PixelWidth, croppedImage.PixelHeight)); var bitmap = (Bitmap)croppedImage; var grayImage = grayFilter.Apply(bitmap); bitmap = smoothFilter.Apply(grayImage); edgeFilter.ApplyInPlace(bitmap); blobCounter.ProcessImage(bitmap); var blobs = blobCounter.GetObjectsInformation(); var possibleChars = new List<Rectangle>(); foreach (var blob in blobs) { var objRectangle = blob.Rectangle; var ratio = (double)objRectangle.Height / (double)objRectangle.Width; if (ratio >= 1.16d && ratio <= 6.3d) { possibleChars.Add(objRectangle); } } if (possibleChars.Count == 0) { continue; } bestCandidate = croppedImage; } return bestCandidate; }
private async void CropImage(bool wide = false) { IsCropEnabled = false; try { var img = wide ? _previewImageWide : _previewImageNormal; WriteableBitmap resizedBitmap = new WriteableBitmap(CropWidth, CropHeight); //if (img.UriSource == null) //await resizedBitmap.LoadAsync(_originaPickedStorageFile); /*else*/ if (!img.UriSource.ToString().Contains("ms-appdata")) { var imgFile = await SaveImage(img, wide); await resizedBitmap.LoadAsync(imgFile); } else await resizedBitmap.LoadAsync(await StorageFile.GetFileFromApplicationUriAsync(img.UriSource)); if (wide) resizedBitmap = resizedBitmap.Crop(CropLeftWide, CropTopWide, CropWidthWide + CropLeftWide, CropTopWide + CropHeightWide); else resizedBitmap = resizedBitmap.Crop(CropLeft, CropTop, CropWidth + CropLeft, CropTop + CropHeight); var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync($"_cropTemp{(wide ? "Wide" : "")}.png", CreationCollisionOption.GenerateUniqueName); if (wide) _lastCroppedFileNameWide = file.Name; else _lastCroppedFileName = file.Name; await resizedBitmap.SaveAsync(file, BitmapEncoder.PngEncoderId); if (wide) { PreviewImageWide = new BitmapImage(new Uri($"ms-appdata:///temp/{file.Name}")); } else { PreviewImageNormal = new BitmapImage(new Uri($"ms-appdata:///temp/{file.Name}")); UndoCropVisibility = Visibility.Visible; } } catch (Exception) { UWPUtilities.GiveStatusBarFeedback("An error occured..."); } IsCropEnabled = true; }
private WriteableBitmap CropIfNeeded(WriteableBitmap writeableBitmap) { var height = writeableBitmap.PixelHeight; var width = writeableBitmap.PixelWidth; var xCenter = width/2; var yCenter = height/2; if (width > height) { return writeableBitmap.Crop(xCenter - height/2, 0, height, height); } if (width < height) { return writeableBitmap.Crop(0, yCenter - width/2, width, width); } return writeableBitmap; }
/// <summary> /// Gets the current preview frame as a SoftwareBitmap, displays its properties in a TextBlock, and can optionally display the image /// in the UI and/or save it to disk as a jpg /// </summary> /// <returns>Task representing the async event status</returns> private async Task GetPreviewFrameAsSoftwareBitmapAsync() { // Get information about the preview VideoEncodingProperties previewProperties = this._mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; // Create the video frame to request a SoftwareBitmap preview frame VideoFrame videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height); // Capture the preview frame using (VideoFrame currentFrame = await this._mediaCapture.GetPreviewFrameAsync(videoFrame)) { // Collect the resulting frame SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap; // Show the frame information Debug.WriteLine("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat); tempWriteableBitmap = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight); previewFrame.CopyToBuffer(tempWriteableBitmap.PixelBuffer); // Crop to a square, based on the smallest side int minEdge = Math.Min(tempWriteableBitmap.PixelWidth, tempWriteableBitmap.PixelHeight); tempWriteableBitmap = tempWriteableBitmap .Crop(0, 0, minEdge, minEdge) .Resize(App.LedMatrix.PixelWidth, App.LedMatrix.PixelHeight, WriteableBitmapExtensions.Interpolation.Bilinear); WriteableBitmap previewFrameImageSource = tempWriteableBitmap.Rotate(90).Resize( (int)this.postViewbox.Height, (int)this.postViewbox.Width, WriteableBitmapExtensions.Interpolation.NearestNeighbor); this.previewFrameImage.Source = previewFrameImageSource; } }