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); }