Ejemplo n.º 1
0
 public CounterArea(Rectangle o_area, Rectangle i_area, Bitmap image, PatternOCR pocr)
 {
     this.BoundingBox = o_area;
     this.InnerBox    = i_area;
     this.OCRImage    = image;
     this.patternOCR  = pocr;
 }
Ejemplo n.º 2
0
        public HexcellsSolver(HexPatternParameter pparam)
        {
            _patternParameter = pparam;

            Dictionary <string, Bitmap> refdic = new Dictionary <string, Bitmap>();

            refdic.Add("-", Properties.Resources.pattern_dash);
            refdic.Add("{", Properties.Resources.pattern_Open);
            refdic.Add("}", Properties.Resources.pattern_Close);
            refdic.Add("?", Properties.Resources.pattern_QMark);
            refdic.Add("0", Properties.Resources.pattern_0);
            refdic.Add("1", Properties.Resources.pattern_1);
            refdic.Add("2", Properties.Resources.pattern_2);
            refdic.Add("3", Properties.Resources.pattern_3);
            refdic.Add("4", Properties.Resources.pattern_4);
            refdic.Add("5", Properties.Resources.pattern_5);
            refdic.Add("6", Properties.Resources.pattern_6);
            refdic.Add("7", Properties.Resources.pattern_7);
            refdic.Add("8", Properties.Resources.pattern_8);
            refdic.Add("9", Properties.Resources.pattern_9);
            refdic.Add("10", Properties.Resources.pattern_10);
            refdic.Add("12", Properties.Resources.pattern_12);
            refdic.Add("13", Properties.Resources.pattern_13);
            refdic.Add("14", Properties.Resources.pattern_14);
            refdic.Add("15", Properties.Resources.pattern_15);
            refdic.Add("16", Properties.Resources.pattern_16);
            refdic.Add("17", Properties.Resources.pattern_17);
            refdic.Add("18", Properties.Resources.pattern_18);
            refdic.Add("19", Properties.Resources.pattern_19);


            POCR = new PatternOCR(refdic, OCRCoupling.NORMAL_COUPLED_SEGMENTS);
            Cam  = new HexCam();
            OCR  = new HexOCR(POCR);
        }
Ejemplo n.º 3
0
 public HexagonCellImage(Vec2d center, double radius, Bitmap image, PatternOCR pocr)
 {
     this.OCRCenter   = center;
     this.OCRRadius   = radius;
     this.OCRImage    = image;
     this.OCRHeight   = OCRRadius * (Math.Sin(MathExt.ToRadians(60)) / Math.Sin(MathExt.ToRadians(90)));
     this.BoundingBox = GetBoundingBox(center, radius);
     this.PatternOCR  = pocr;
 }
Ejemplo n.º 4
0
        private void Train_Click(object sender, RoutedEventArgs e)
        {
            PatternOCR pocr = new PatternOCR((OCRCoupling)cbxCoupling.SelectedIndex);

            List <Tuple <string, bool[, ]> > trainingdata = new List <Tuple <string, bool[, ]> >();

            foreach (var tdata in data)
            {
                var characters = pocr.RecognizeSingleCharacter(tdata.Item2);

                List <String> characters_final = (from Match match in Regex.Matches(tdata.Item1, "[0-9]+|[^0-9]+") select match.Value).ToList();

                int cpos = 0;
                foreach (var ochar in characters)
                {
                    string pchar = null;
                    if (characters.Count == characters_final.Count)
                    {
                        pchar = characters_final[cpos];
                    }
                    if (characters.Count == tdata.Item1.Length)
                    {
                        pchar = tdata.Item1[cpos] + "";
                    }

                    trainingdata.Add(Tuple.Create(pchar, ochar));

                    cpos++;
                }
            }

            foreach (var pattern in pocr.TrainPatternsToImage(trainingdata))
            {
                pattern.Value.Save(@"..\..\pattern\pattern_" + pattern.Key.Replace("?", "Q") + ".png");
            }
        }
Ejemplo n.º 5
0
 public HexOCR(PatternOCR pocr)
 {
     patternOCR = pocr;
 }
Ejemplo n.º 6
0
 public HexagonCell(Vec2i position, Vec2d center, double radius, Bitmap image, PatternOCR pocr)
 {
     this.Position = position;
     this.Image    = new HexagonCellImage(center, radius, image, pocr);
 }
Ejemplo n.º 7
0
        private void Recognize_Click(object sender, RoutedEventArgs e)
        {
            var references = Directory
                             .EnumerateFiles(@"..\..\pattern\")
                             .Select(p => new { Path = p, File = Path.GetFileName(p) })
                             .Where(p => Regex.IsMatch(p.File, @"^pattern_(.*)\.png$"))
                             .ToDictionary(p => Regex.Match(p.File, @"^pattern_(.*)\.png$").Groups[1].Value.Replace("Q", "?"), q => new Bitmap(System.Drawing.Image.FromFile(q.Path)));

            PatternOCR pocr = new PatternOCR(references, (OCRCoupling)cbxCoupling.SelectedIndex);

            int errors = 0;

            List <int> distanceTable = new List <int>();

            int pos = 1;

            foreach (var tdata in data)
            {
                var ocr = pocr.RecognizeOCR(tdata.Item2, (OCRCoupling)cbxCoupling.SelectedIndex);

                SetContentGridCell(ocr.Value + "        {" + string.Join(", ", ocr.Characters.Select(p => p.Distance.ToString("F0"))) + "}" + "\r\n           {" + string.Join(", ", ocr.Characters.Select(p => p.EulerNumber)) + "}", 13, pos, ocr.Value == tdata.Item1);

                errors += ocr.Value == tdata.Item1 ? 0 : 1;

                //############################################
                {
                    var           ochars           = pocr.RecognizeSingleCharacter(tdata.Item2);
                    List <String> characters_final = (from Match match in Regex.Matches(ocr.Value, "[0-9]+|[^0-9]+") select match.Value).ToList();

                    int opos = 0;
                    foreach (var ochar in ochars)
                    {
                        string pchar = null;
                        if (ochars.Count == characters_final.Count)
                        {
                            pchar = characters_final[opos];
                        }
                        else if (ochars.Count == ocr.Value.Length)
                        {
                            pchar = (ocr.Value[opos] + "");
                        }
                        else
                        {
                            continue;
                        }

                        var pattern = pocr.GetPattern(pchar);
                        var diff    = pocr.GetPatternDiff(ochar, pattern, ocr.Characters[opos].OffsetX, ocr.Characters[opos].OffsetY);

                        SetContentGridCell(diff, 15 + 2 * opos, pos);

                        opos++;
                    }
                }
                //############################################

                SetContentGridCell(string.Join("\r\n", ocr.Characters.Select(p => string.Join(" | ", p.AllDistances.OrderBy(q => Regex.IsMatch(q.Key, @"^[0-9+]+$") ? int.Parse(q.Key) : ((q.Key + "@")[0] + 255)).Select(q => String.Format("{0}: {1:X}", q.Key, (int)q.Value))))), 23, pos, true);

                if (ocr.Value != tdata.Item1)
                {
                    if (cbSaveError.IsChecked.Value)
                    {
                        int sidx = 0;
                        while (File.Exists(string.Format(@"..\..\errordata\{0}_{1}.png", tdata.Item1, sidx)))
                        {
                            sidx++;
                        }
                        tdata.Item2.Save(string.Format(@"..\..\errordata\{0}_{1}.png", tdata.Item1, sidx));
                        Console.Out.WriteLine("Saved errordata to " + Path.GetFullPath(string.Format(@"..\..\errordata\{0}_{1}.png", tdata.Item1, sidx)));
                    }

                    var           ochars           = pocr.RecognizeSingleCharacter(tdata.Item2);
                    List <String> characters_final = (from Match match in Regex.Matches(tdata.Item1, "[0-9]+|[^0-9]+") select match.Value).ToList();

                    int opos = 0;
                    foreach (var ochar in ochars)
                    {
                        string pchar = null;
                        if (ochars.Count == characters_final.Count)
                        {
                            pchar = characters_final[opos];
                        }
                        else if (opos < tdata.Item1.Length)
                        {
                            pchar = (tdata.Item1[opos] + "");
                        }
                        else
                        {
                            continue;
                        }

                        var pattern = pocr.GetPattern(pchar);
                        var diff    = pocr.GetPatternDiff(ochar, pattern, ocr.Characters[opos].OffsetX, ocr.Characters[opos].OffsetY);

                        SetContentGridCell(diff, 25 + 2 * opos, pos);

                        opos++;
                    }
                }
                else
                {
                    foreach (var chr in ocr.Characters)
                    {
                        if (chr.Character != "-")
                        {
                            distanceTable.Add((int)chr.Distance);
                        }
                    }
                }

                pos += 2;
            }

            MessageBox.Show(errors + "/" + data.Count + "  false detections");

            MessageBox.Show("Distances: " + Environment.NewLine + string.Join(Environment.NewLine, distanceTable.GroupBy(p => p).OrderBy(p => p.Key).Select(p => string.Format("{0:00} = {1}", p.Key, p.Count()))));
        }
Ejemplo n.º 8
0
        //private static int picid = 0;

        private void Boxing_Click(object sender, RoutedEventArgs e)
        {
            Color[] CLRS = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Fuchsia, Color.SaddleBrown, Color.Purple, Color.Orange, Color.LightGray };

            PatternOCR pocr = new PatternOCR((OCRCoupling)cbxCoupling.SelectedIndex);

            {
                int pos = 1;
                foreach (var tdata in data)
                {
                    var result = pocr.FindCharacterBoxes(tdata.Item2);
                    var grid   = result.Item1;

                    Bitmap img = new Bitmap(tdata.Item2.Width, tdata.Item2.Height);
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.Clear(Color.White);

                        for (int x = 1; x < img.Width - 1; x++)
                        {
                            for (int y = 1; y < img.Height - 1; y++)
                            {
                                if (grid[x, y] > 0)
                                {
                                    img.SetPixel(x, y, CLRS[grid[x, y] % CLRS.Length]);
                                }
                            }
                        }
                    }

                    SetContentGridCell(img, 3, pos);
                    pos += 2;
                }
            }

            {
                int pos = 1;
                foreach (var tdata in data)
                {
                    var characters = pocr.RecognizeSingleCharacter(tdata.Item2);

                    int cpos = 0;
                    foreach (var ochar in characters)
                    {
                        Bitmap img = new Bitmap(ochar.GetLength(0), ochar.GetLength(1));
                        using (Graphics g = Graphics.FromImage(img))
                        {
                            g.Clear(Color.White);

                            for (int x = 0; x < img.Width; x++)
                            {
                                for (int y = 0; y < img.Height; y++)
                                {
                                    img.SetPixel(x, y, Color.FromArgb(ochar[x, y] ? 0 : 255, ochar[x, y] ? 0 : 255, ochar[x, y] ? 0 : 255));
                                }
                            }
                        }

                        SetContentGridCell(img, 5 + 2 * cpos++, pos);
                        //img.Save(@"..\..\imgsave\_" + picid++ + ".png");
                    }

                    pos += 2;
                }
            }
        }