public void GenerateTamplates(string chars, Font fnt) { Bitmap bmp = new Bitmap(40 + (int)fnt.SizeInPoints, 40 + (int)fnt.SizeInPoints); fnt = new Font(fnt.FontFamily, fnt.SizeInPoints, fnt.Style); Graphics gr = Graphics.FromImage(bmp); for (int pos = 0; pos < chars.Length; pos++) { gr.Clear(Color.White); gr.DrawString(chars[pos].ToString(), fnt, Brushes.Black, 0, 0); Rectangle rct = ImageProcess.GetBorder(bmp); Bitmap tmp = ImageProcess.GetRegion(bmp, new Rectangle(rct.X, rct.Y, rct.Width, rct.Height)); tmp = ImageProcess.SetSize(tmp, 0, 0, 16, 16); tmp = ImageProcess.Binarization(tmp, 100); string name; if (chars[pos] == chars.ToUpper()[pos]) { name = "ImgB " + chars[pos]; } else { name = "ImgS " + chars[pos]; } tptList.Add(new Template(tmp, chars[pos])); } }
public Char Recognize(Bitmap img) { Rectangle rct = ImageProcess.GetBorder(img); img = ImageProcess.GetRegion(img, new Rectangle(rct.X, rct.Y, rct.Width, rct.Height)); img = ImageProcess.SetSize(img, 0, 0, 16, 16); img = ImageProcess.Binarization(img, 10); img.Save(@"Images" + "\\img.bmp", System.Drawing.Imaging.ImageFormat.Bmp); int maxSim = 0, sim, index = 0;; for (int i = 0; i < tptList.Count; i++) { sim = Comparator(tptList[i].image, img); if (sim > maxSim) { maxSim = sim; index = i; } } return(tptList[index].value); }