public string Recognize(Bitmap bmp, OCRCoupling coupling, out double errorDistance) { var ocr = RecognizeOCR(bmp, coupling); errorDistance = ocr.Distance; return(ocr.Value); }
public PatternOCR(Dictionary <string, Bitmap> refdic, OCRCoupling coupling) { Coupling = coupling; LoadReferencePatterns(refdic); }
public OCRResult RecognizeOCR(Bitmap img, OCRCoupling coupling) { Coupling = coupling; var ochars = RecognizeSingleCharacter(img); var distances = new List <Tuple <int, int, double> >(); StringBuilder result = new StringBuilder(); OCRResult ocrResult = new OCRResult() { Characters = new List <OCRCharacterResult>(), }; foreach (var ochar in ochars) { var euler = GetEulerNumber(ochar); var activePixel = GetActivePixel(ochar); var matches = references .Select(p => new { Reference = p, Distance = GetImageDistance(ochar, p.Value, euler, p.Key) }) .OrderBy(p => p.Distance.Item3) .ToList(); if (matches.Count == 0) { throw new Exception("No References"); } else { if (activePixel == ochar.GetLength(0) * ochar.GetLength(1)) { matches = matches .Select(p => new { Reference = p.Reference, Distance = Tuple.Create(p.Distance.Item1, p.Distance.Item2, p.Distance.Item3 + ((p.Reference.Key == "-") ? 0 : 64), p.Distance.Item4) }) .OrderBy(p => p.Distance.Item3) .ToList(); } if (matches.First().Distance.Item3 > 70) { matches = matches .Select(p => new { Reference = p.Reference, Distance = Tuple.Create(p.Distance.Item1, p.Distance.Item2, p.Distance.Item3 + ((p.Reference.Key == "-") ? 0 : 48), p.Distance.Item4) }) .OrderBy(p => p.Distance.Item3) .ToList(); } result.Append(matches.First().Reference.Key); ocrResult.Characters.Add(new OCRCharacterResult() { Character = matches.First().Reference.Key, OffsetX = matches.First().Distance.Item1, OffsetY = matches.First().Distance.Item2, Distance = matches.First().Distance.Item3, EulerNumber = matches.First().Distance.Item4, AllDistances = matches.ToDictionary(p => p.Reference.Key, p => p.Distance.Item3), }); } } ocrResult.Value = result.ToString(); ocrResult.Distance = (ocrResult.Characters.Count == 0) ? 0 : ocrResult.Characters.Max(p => p.Distance); return(ocrResult); }
public PatternOCR(OCRCoupling coupling) { Coupling = coupling; references = new Dictionary <string, byte[, ]>(); }