Exemple #1
0
        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);
        }
Exemple #2
0
        private void Browse_Click(object sender, RoutedEventArgs e)
        {
            const double MAXMULT = 2;

            try
            {
                var dialog = new Microsoft.Win32.OpenFileDialog();
                dialog.Multiselect = false;
                dialog.Title       = "Please select an image";
                bool?result = dialog.ShowDialog();
                if (result == null || !result.Value)
                {
                    return;
                }

                _origConv = UtilityWPF.ConvertToConvolution(new BitmapImage(new Uri(dialog.FileName)));

                lblOrigWidth.Text  = _origConv.Width.ToString("N0");
                lblOrigHeight.Text = _origConv.Height.ToString("N0");

                double max = Math.Max(_origConv.Width, _origConv.Height) * MAXMULT;

                trkWidth.Minimum = _origConv.Width;
                trkWidth.Maximum = max;
                trkWidth.Value   = _origConv.Width;

                trkHeight.Minimum = _origConv.Height;
                trkHeight.Maximum = max;
                trkHeight.Value   = _origConv.Height;

                _timer.IsEnabled = false;       // touching the sliders causes the timer to be enabled

                grdSize.Visibility = Visibility.Visible;

                ShowConvolution(_origConv);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }