private void ExtractRawRect_Click(object sender, RoutedEventArgs e) { try { // Convert the original image to grayscale Convolution2D image = GetOriginalImageGrays(); if (image == null) { // The original image is empty MessageBox.Show("Please load an image first", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning); return; } // Rectangle RectInt rect = Convolutions.GetExtractRectangle(new VectorInt(image.Width, image.Height), false); // Extract AddKernel(image.Extract(rect, ConvolutionExtractType.RawUnit)); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private void grdOrigImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { try { if (_selectionStartPoint == null) { return; } // Clear selection grdOrigImage.ReleaseMouseCapture(); selectionBox.Visibility = Visibility.Collapsed; Point start = _selectionStartPoint.Value; _selectionStartPoint = null; Convolution2D image = GetOriginalImageGrays(); if (image == null) { return; } // Screen coords Point mousePos = e.GetPosition(grdOrigImage); double minX = Math.Min(start.X, mousePos.X); double minY = Math.Min(start.Y, mousePos.Y); double maxX = Math.Max(start.X, mousePos.X); double maxY = Math.Max(start.Y, mousePos.Y); // Convert to convolution coords double scaleX = image.Width / grdOrigImage.ActualWidth; double scaleY = image.Height / grdOrigImage.ActualHeight; int extractX1 = (minX * scaleX).ToInt_Round(); int extractY1 = (minY * scaleY).ToInt_Round(); int extractX2 = (maxX * scaleX).ToInt_Round(); int extractY2 = (maxY * scaleY).ToInt_Round(); if (extractX1 < 0) { extractX1 = 0; } if (extractY1 < 0) { extractY1 = 0; } if (extractX2 < 0) { extractX2 = 0; } if (extractY2 < 0) { extractY2 = 0; } if (extractX1 >= image.Width) { extractX1 = image.Width - 1; } if (extractY1 >= image.Height) { extractY1 = image.Height - 1; } if (extractX2 >= image.Width) { extractX2 = image.Width - 1; } if (extractY2 >= image.Height) { extractY2 = image.Height - 1; } int extractWidth = extractX2 - extractX1; int extractHeight = extractY2 - extractY1; if (extractWidth < 1 || extractHeight < 1) { return; } // Extract AddKernel(image.Extract(new RectInt(extractX1, extractY1, extractWidth, extractHeight), ConvolutionExtractType.Edge)); AddKernel(image.Extract(new RectInt(extractX1, extractY1, extractWidth, extractHeight), ConvolutionExtractType.EdgeSoftBorder)); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }