Ejemplo n.º 1
0
        private void StrokeCollection_StrokesChanged(object sender, System.Windows.Ink.StrokeCollectionChangedEventArgs e)
        {
            try
            {
                EncogOCR_SketchData sketch = new EncogOCR_SketchData()
                {
                    Name    = "",
                    Strokes = canvasInk.Strokes.
                              Select(o => new EncogOCR_StrokeDefinition(o)).
                              ToArray(),
                    InkCanvasSize = new Size(canvasInk.ActualWidth, canvasInk.ActualHeight),
                };

                sketch.GenerateBitmap(IMAGESIZE);

                // The bitmap is white on black, switch to black on white
                double[] inverted = sketch.NNInput.
                                    Select(o => 1d - o).
                                    ToArray();

                double[] converted = inverted;
                if (_patternStorage != null)
                {
                    converted = _patternStorage.Convert_Local_External(converted);
                }

                DrawImage(imageCurrent, converted);

                panelGuessedImages.Children.Clear();

                if (_patternStorage != null)
                {
                    double[][] recognized = _patternStorage.Recognize(inverted);

                    #region show thumbnails

                    if (recognized != null)
                    {
                        foreach (var guessImage in recognized)
                        {
                            DrawImage(panelGuessedImages.Children, guessImage);
                        }
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 2
0
        private void ResetTraining()
        {
            _patternStorage = null;
            panelTrainingImages.Children.Clear();

            if (_images.Count == 0)
            {
                return;
            }

            #region choose images

            int numImages;
            if (!int.TryParse(txtTrainCount.Text, out numImages))
            {
                MessageBox.Show("Couldn't parse count as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            numImages = Math.Min(_images.Count, numImages);

            var trainImages = UtilityCore.RandomRange(0, _images.Count, numImages).
                              Select(o => new
            {
                File = _images[o],
                Conv = GetTrainingImage(_images[o], _trainKernel),
            }).
                              ToArray();

            #endregion

            //_patternStorage = new RandomPatternStorage();
            _patternStorage = new Hopfield(IMAGESIZE * IMAGESIZE, 0, 1, .9);

            #region show thumbnails

            // Display thumbnails
            //< !--Run them through a KMeans, then sort in 1D-- >
            //< !--Show full resolution over the canvas on mouseover-- >
            //< !--Show full resolution under the canvas on click-- >

            foreach (var trainImage in trainImages)
            {
                double widthHeight    = Math.Sqrt(trainImage.Conv.Length);  // they should be square
                int    widthHeightInt = widthHeight.ToInt_Round();
                if (!Math1D.IsNearValue(widthHeight, widthHeightInt))
                {
                    throw new ApplicationException("Expected square images");
                }

                double[] imageConv = trainImage.Conv;
                imageConv = _patternStorage.Convert_Local_External(imageConv);

                BitmapSource source;
                //if (isColor)
                //{
                //    source = UtilityWPF.GetBitmap_RGB(example, width, height);
                //}
                //else
                //{
                source = UtilityWPF.GetBitmap(imageConv, widthHeightInt, widthHeightInt);
                //}

                Image image = new Image()
                {
                    Source = source,
                    Width  = source.PixelWidth,     // if this isn't set, the image will take up all of the width, and be huge
                    Height = source.PixelHeight,
                    Margin = new Thickness(8),
                };

                panelTrainingImages.Children.Add(image);
            }

            #endregion

            _patternStorage.AddItems(trainImages.Select(o => o.Conv).ToArray());
        }
        private void ResetTraining()
        {
            _patternStorage = null;
            panelTrainingImages.Children.Clear();

            if (_images.Count == 0)
            {
                return;
            }

            #region choose images

            int numImages;
            if (!int.TryParse(txtTrainCount.Text, out numImages))
            {
                MessageBox.Show("Couldn't parse count as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            numImages = Math.Min(_images.Count, numImages);

            var trainImages = UtilityCore.RandomRange(0, _images.Count, numImages).
                Select(o => new
                {
                    File = _images[o],
                    Conv = GetTrainingImage(_images[o], _trainKernel),
                }).
                ToArray();

            #endregion

            //_patternStorage = new RandomPatternStorage();
            _patternStorage = new Hopfield(IMAGESIZE * IMAGESIZE, 0, 1, .9);

            #region show thumbnails

            // Display thumbnails
            //< !--Run them through a KMeans, then sort in 1D-- >
            //< !--Show full resolution over the canvas on mouseover-- >
            //< !--Show full resolution under the canvas on click-- >

            foreach (var trainImage in trainImages)
            {
                double widthHeight = Math.Sqrt(trainImage.Conv.Length);     // they should be square
                int widthHeightInt = widthHeight.ToInt_Round();
                if (!Math1D.IsNearValue(widthHeight, widthHeightInt))
                {
                    throw new ApplicationException("Expected square images");
                }

                double[] imageConv = trainImage.Conv;
                imageConv = _patternStorage.Convert_Local_External(imageConv);

                BitmapSource source;
                //if (isColor)
                //{
                //    source = UtilityWPF.GetBitmap_RGB(example, width, height);
                //}
                //else
                //{
                source = UtilityWPF.GetBitmap(imageConv, widthHeightInt, widthHeightInt);
                //}

                Image image = new Image()
                {
                    Source = source,
                    Width = source.PixelWidth,      // if this isn't set, the image will take up all of the width, and be huge
                    Height = source.PixelHeight,
                    Margin = new Thickness(8),
                };

                panelTrainingImages.Children.Add(image);
            }

            #endregion

            _patternStorage.AddItems(trainImages.Select(o => o.Conv).ToArray());
        }