예제 #1
0
파일: MainForm.cs 프로젝트: IMbrera/OCR
        private void TestLoadBitmap(Bitmap b)
        {
            var images = symbol.SplitUp(b); // пробелы между символами

            //load white space straight away
            if (symbol.Letters.Any(s => s.Letter.Equals(' ')) == false)
            {
                symbol.Train(SymbolIdentity.WhiteBitmap, ' ');
            }
            //for each image that hasnt been seen, have the player enter it
            foreach (var row in images)
            {
                foreach (var col in row)
                {
                    //try and find a perfect match
                    var c = symbol.PerformOCRCharacterPerfect(col);
                    //we only want things we havent seen before
                    if (c != null)
                    {
                        continue;
                    }

                    if (BitmapExtras.IsAllPixelsAColour(col, Color.White))
                    {
                        continue;
                    }

                    //ask user for character and show
                    var gsi     = new GetImageCompare();
                    var letters = gsi.ShowDialog("Какой символ изображен на форме?", "Определение символов", col, 1);
                    if (letters == null)
                    {
                        return;
                    }

                    if (letters.Length == 0)
                    {
                        continue;
                    }
                    var letter = letters.First().First();
                    if (char.IsWhiteSpace(letter))
                    {
                        MessageBox.Show("cannot be white space");
                        return;
                    }

                    if (symbol.Letters.Any(s => s.Letter.Equals(letter)))
                    {
                        symbol.Letters.RemoveAll(s => s.Letter.Equals(letter));
                    }

                    //ОБУЧЕНИЕ
                    symbol.Train(col, letter);
                }
            }
        }
예제 #2
0
        public void GetAndSetImages(PrintModes pm, MinimalOptions mo)
        {
            //download first if necessary
            //colour
            var pathColour = GetImageFilename(C, PrintModes.Colour);

            if (File.Exists(pathColour) == false)
            {
                NetExtras.DownloadFile(C.ImgURL, pathColour);
            }

            //black and white
            var pathBw = GetImageFilename(C, PrintModes.BlackAndWhite);

            if (File.Exists(pathBw) == false)
            {
                Bitmap bw;
                using (var i = (Bitmap)Image.FromFile(pathColour))
                {
                    bw = BitmapExtras.ApplyGrayscale(new Bitmap(i));
                }

                bw.Save(pathBw);
            }

            //minimal - always generate in case options have changed - todo - do properly
            var minpath = GetImageFilename(C, PrintModes.Minimal);

            if (File.Exists(minpath))
            {
                File.Delete(minpath);
            }

            var m   = MinimalCardControl.Show(C, mo);
            var min = new Bitmap(m.Width, m.Height);

            m.DrawToBitmap(min, new Rectangle(0, 0, m.Width, m.Height));
            min.Save(minpath);

            SetImage(pm);
        }
예제 #3
0
        /// <summary>
        /// trim white and resize to created dimensions
        /// </summary>
        /// <param name="b1">The b1.</param>
        /// <returns></returns>
        public Bitmap Normalise(Bitmap b1)
        {
            //white 1x1=empty
            if (b1.Width == 1 && b1.Height == 1)
            {
                return(b1);
            }

            //trim white
            var b = BitmapExtras.RemoveExcessWhitespace(b1);

            if (b == null)
            {
                return(null);
            }

            //resize to internal size
            b = BitmapExtras.StretchBitmap(b, HistogramWidth, HistogramHeight);

            //convert to black and white
            b = BitmapExtras.ChangeAllNonWhitePixelsToColour(b, Color.Black);

            return(b);
        }
예제 #4
0
        /// <summary>
        /// Splits the specified b.
        /// </summary>
        /// <param name="b">The b.</param>
        /// <param name="byRow">if set to <c>true</c> [by row].</param>
        /// <param name="whiteToSpace">The white to space.</param>
        /// <returns></returns>
        /// <exception cref="Exception">error splitting</exception>
        private static IEnumerable <Bitmap> Split(Bitmap b, bool byRow, ref int whiteToSpace)
        {
            //split rows then cols
            var w  = b.Width;
            var h  = b.Height;
            var to = byRow ? h : w;

            var items      = new List <Bitmap>();
            var whiteStart = 0;
            var lastValue  = -1;

            for (var v = 0; v < to; v++)
            {
                //keep going until we find a row with black
                var isWhite = BitmapExtras.IsRowOrColSameColour(b, v, byRow, Color.White);

                if (isWhite)
                {
                    if (whiteStart == v)
                    {
                        whiteStart++;
                        continue;
                    }

                    //see if there is a space at the start
                    if (whiteToSpace != -1 && items.Count == 0 && v >= whiteToSpace)
                    {
                        items.Add(WhiteBitmap);
                    }

                    //if there is more than a certain amount of space check/add a white item
                    if (whiteToSpace != -1 && lastValue != -1 && ((v - lastValue) >= whiteToSpace))
                    {
                        items.Add(WhiteBitmap);
                    }

                    //if this occurs, there is a character before
                    Bitmap i = null;
                    if (byRow)
                    {
                        //if by row, and the second time, we can estimate the white space for a space
                        if (whiteToSpace == -1 && items.Count == 1)
                        {
                            whiteToSpace = whiteStart;
                        }

                        i = BitmapExtras.Crop(b, -1, v - whiteStart, 0, whiteStart);
                    }
                    else
                    {
                        i = BitmapExtras.Crop(b, v - whiteStart, -1, whiteStart, 0);
                    }
                    if (i == null)
                    {
                        throw new Exception("error splitting");
                    }

                    items.Add(i);

                    lastValue  = v;
                    whiteStart = v + 1;
                }
            }

            //see if there is a space at the end
            if (whiteToSpace != -1 && lastValue != -1 && ((to - lastValue) >= whiteToSpace))
            {
                items.Add(WhiteBitmap);
            }

            return(items);
        }