private static Convolution2D Convolute(Convolution2D conv, ToVectorInstructions instr) { Convolution2D retVal = conv; if (retVal.Width != retVal.Height) { int max = Math.Max(retVal.Width, retVal.Height); retVal = Convolutions.ExtendBorders(retVal, max, max); // make it square } if (instr.ShouldNormalize) { retVal = Convolutions.Normalize(retVal); } if (instr.Convolution != null) { retVal = Convolutions.Convolute(retVal, instr.Convolution); } retVal = Convolutions.MaxPool(retVal, instr.ToSize, instr.ToSize); retVal = Convolutions.Abs(retVal); return(retVal); }
private static double[] GetTrainingImage(FeatureRecognizer_Image image, ConvolutionBase2D kernel) { // Enlarge the initial image by the kernel's reduction so that after convolution, it is the desired size VectorInt reduction = kernel.GetReduction(); if (reduction.X != reduction.Y) { throw new ApplicationException(string.Format("Kernel should be square: {0}x{1}", reduction.X, reduction.Y)); } BitmapSource bitmap = new BitmapImage(new Uri(image.Filename)); bitmap = UtilityWPF.ResizeImage(bitmap, IMAGESIZE + reduction.X, true); Convolution2D retVal = UtilityWPF.ConvertToConvolution(bitmap, 1d); if (retVal.Width != retVal.Height) { retVal = Convolutions.ExtendBorders(retVal, IMAGESIZE + reduction.X, IMAGESIZE + reduction.X); //NOTE: width or height is already the desired size, this will just enlarge the other to make it square } retVal = Convolutions.Convolute(retVal, kernel); retVal = Convolutions.Abs(retVal); // It looks better when it's black on white double[] inverted = retVal.Values. Select(o => 1d - o). ToArray(); return(inverted); }
private void AddDefaultKernels_Edges_Small() { AddKernel(Convolutions.GetEdge_Sobel(true)); AddKernel(Convolutions.GetEdge_Sobel(false)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Sobel(true), true)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Sobel(false), true)); AddKernel(Convolutions.GetEdge_Prewitt(true)); AddKernel(Convolutions.GetEdge_Prewitt(false)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Prewitt(true), true)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Prewitt(false), true)); AddKernel(Convolutions.GetEdge_Compass(true)); AddKernel(Convolutions.GetEdge_Compass(false)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Compass(true), true)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Compass(false), true)); AddKernel(Convolutions.GetEdge_Kirsch(true)); AddKernel(Convolutions.GetEdge_Kirsch(false)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Kirsch(true), true)); AddKernel(Convolutions.Rotate_45(Convolutions.GetEdge_Kirsch(false), true)); AddKernel(Convolutions.GetEdge_Laplacian(true)); AddKernel(Convolutions.GetEdge_Laplacian(false)); }
private void AddKernel(ConvolutionBase2D kernel) { Border border = Convolutions.GetThumbnail(kernel, 40, _kernelContextMenu); //if (!string.IsNullOrEmpty(tooltipHeader)) //{ // // For simple (not composite) kernels, it's the image that gets the tooltip. So if this is one of those, add to the tooltip // if (border.Child is Image) // { // string existingTooltip = ((Image)border.Child).ToolTip as string; // if (!string.IsNullOrEmpty(existingTooltip)) // { // ((Image)border.Child).ToolTip = tooltipHeader + "\r\n" + existingTooltip; // } // else // { // border.ToolTip = tooltipHeader; // } // } // else // { // border.ToolTip = tooltipHeader; // } //} // Store them panelKernels.Children.Add(border); _kernels.Add(kernel); }
public HopfieldNetwork() { InitializeComponent(); _trainKernel = Convolutions.GetEdgeSet_GuassianThenEdge(5); _initialized = true; }
private void AddKernel(ConvolutionBase2D kernel) { Border border = Convolutions.GetThumbnail(kernel, 40, null); // Store them panelKernels.Children.Add(border); _kernels.Add(kernel); }
private void ApplyFilter(ConvolutionBase2D kernel) { // Convert the original image to grayscale Convolution2D image = GetOriginalImageGrays(); if (image == null) { // The original image is empty return; } Convolution2D filtered = null; if (kernel is Convolution2D) { #region Single Convolution2D kernelSingle = (Convolution2D)kernel; // This window builds kernels without gain or iterations, so make a clone with those tacked on Convolution2D kernelFinal = new Convolution2D( kernelSingle.Values, kernelSingle.Width, kernelSingle.Height, kernelSingle.IsNegPos, trkGain.Value, Convert.ToInt32(trkIterations.Value), chkExpandBorder.IsChecked.Value); filtered = Convolutions.Convolute(image, kernelFinal); if (chkSubtract.IsChecked.Value) { filtered = Convolutions.Subtract(image, filtered); } #endregion } else if (kernel is ConvolutionSet2D) { #region Set ConvolutionSet2D kernelSet = (ConvolutionSet2D)kernel; filtered = Convolutions.Convolute(image, kernelSet); #endregion } else { throw new ArgumentException("Unknown type of kernel: " + kernel.GetType().ToString()); } // Show Filtered modifiedImage.Source = Convolutions.GetBitmap(filtered, (ConvolutionResultNegPosColoring)cboEdgeColors.SelectedValue); }
private static Convolution2D GetRandomFilter_Positive(bool isSquare) { Random rand = StaticRandom.GetRandomForThread(); var size = GetFilterSize(rand, isSquare); double[] values = Enumerable.Range(0, size.Item1 * size.Item2). Select(o => rand.NextDouble()). ToArray(); values = Convolutions.ToUnit(values); return(new Convolution2D(values, size.Item1, size.Item2, false)); }
private void AddDefaultKernels_Gaussian() { int[] sizes = new[] { 3, 4, 5, 6, 7, 9, 15, 20, 30 }; foreach (int size in sizes) { AddKernel(Convolutions.GetGaussian(size, 1)); } foreach (int size in sizes) { AddKernel(Convolutions.GetGaussian(size, 2)); } }
private void InsertKernel(ConvolutionBase2D kernel, int index = -1) { Border border = Convolutions.GetThumbnail(kernel, 80, _kernelContextMenu); if (index < 0) { panel.Children.Add(border); _kernels.Add(kernel); } else { panel.Children.Insert(index, border); _kernels.Insert(index, kernel); } }
private void AddDefaultKernels_Composite() { // Gaussian Subtract AddKernel(Convolutions.GetEdgeSet_GaussianSubtract()); // MaxAbs Sobel foreach (int gain in new[] { 1, 2, 4 }) { AddKernel(Convolutions.GetEdgeSet_Sobel(gain)); } // Gausian then edge foreach (int size in new[] { 3, 5, 7 }) { AddKernel(Convolutions.GetEdgeSet_GuassianThenEdge(size)); } }
private static Convolution2D GetRandomFilter_PosNeg(bool isSquare) { Random rand = StaticRandom.GetRandomForThread(); var size = GetFilterSize(rand, isSquare); double[] values = Enumerable.Range(0, size.Item1 * size.Item2). Select(o => rand.NextDouble(-1, 1)). ToArray(); if (values.All(o => o > 0) || values.All(o => o < 0)) { // They are all positive or all negative. Flip one of them values[rand.Next(values.Length)] *= -1d; } values = Convolutions.ToUnit(values); return(new Convolution2D(values, size.Item1, size.Item2, true)); }
private void Timer_Tick(object sender, EventArgs e) { try { if (!chkContinuous.IsChecked.Value) { _timer.IsEnabled = false; } int width = trkWidth.Value.ToInt_Round(); int height = trkHeight.Value.ToInt_Round(); Convolution2D conv = Convolutions.ExtendBorders(_origConv, width, height); ShowConvolution(conv); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
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 ShowConvolution(Convolution2D conv) { byte[][] colors = Convolutions.GetColors(conv, ConvolutionResultNegPosColoring.BlackWhite); _image.Source = UtilityWPF.GetBitmap(colors, conv.Width, conv.Height); }
public MineralIdentifier() { InitializeComponent(); this.Background = SystemColors.ControlBrush; #region Tab: Single Image // Camera Trackball _trackball = new TrackBallRoam(_camera); _trackball.EventSource = grdViewPort; //NOTE: If this control doesn't have a background color set, the trackball won't see events (I think transparent is ok, just not null) _trackball.AllowZoomOnMouseWheel = true; _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.MouseComplete)); _trackball.MouseWheelScale *= .1; //_trackball.GetOrbitRadius += new GetOrbitRadiusHandler(Trackball_GetOrbitRadius); // Mineral Types foreach (MineralType mineral in Enum.GetValues(typeof(MineralType))) { cboMineral.Items.Add(mineral); } cboMineral.SelectedIndex = 0; #endregion #region Tab: Training Data #region Mineral Types foreach (MineralType mineral in Enum.GetValues(typeof(MineralType))) { CheckBox mineralCheckbox = new CheckBox() { Content = mineral.ToString(), Tag = mineral, Margin = new Thickness(2), }; pnlMineralSelections.Children.Add(mineralCheckbox); } #endregion #region Convolutions // Gaussian Subtract AddKernel(new ConvolutionSet2D(new[] { Convolutions.GetGaussian(3, 1) }, SetOperationType.Subtract)); // MaxAbs Sobel Convolution2D vert = Convolutions.GetEdge_Sobel(true); Convolution2D horz = Convolutions.GetEdge_Sobel(false); var singles = new[] { new Convolution2D(vert.Values, vert.Width, vert.Height, vert.IsNegPos, 1), new Convolution2D(horz.Values, horz.Width, horz.Height, horz.IsNegPos, 1), }; ConvolutionSet2D set = new ConvolutionSet2D(singles, SetOperationType.MaxOf); AddKernel(set); #endregion #endregion _initialized = true; cboMineral_SelectionChanged(this, null); ResetCamera_Click(this, null); }