public static void ShowRecognizeForm(MainForm parent, RecognizeForm rform) { if (parent.InvokeRequired) { ShowForm sf = new ShowForm(ShowRecognizeForm); parent.Invoke(sf, new object[] { parent, rform }); } else { rform.Show(parent); } }
public static void Execute(object obj) { object[] objs = (object[])obj; MainForm mform = (MainForm)objs[0]; // eb -execute {cmd [args]} string[] cmd = (string[])objs[1]; if (rform == null) { rform = new RecognizeForm(); ShowRecognizeForm(mform, rform); } string errorMsg = "none"; try { switch (cmd[0]) { case "showimg": for (int i = 0; i < cmd.Length; i++) { if (cmd[i].Equals("-input")) { ori_img = (Bitmap)Bitmap.FromFile(cmd[i + 1]); break; } } if (ori_img == null) { errorMsg = "please enter the -input path"; } else { show_img = ori_img; rform.UpdateImage(show_img); } break; case "back": Bitmap tmp = show_img; show_img = pre_img; pre_img = tmp; rform.UpdateImage(show_img); break; case "avggray": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { Image <Gray, byte> grayimg = new Image <Gray, byte>(show_img); grayimg._ThresholdBinary(grayimg.GetAverage(), new Gray(255)); pre_img = show_img; show_img = grayimg.Bitmap; rform.UpdateImage(show_img); } break; case "sobel": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { SobelFilter(); } break; case "median": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { Image <Bgr, byte> img = new Image <Bgr, byte>(show_img).SmoothMedian(3); pre_img = show_img; show_img = img.Bitmap; rform.UpdateImage(show_img); } break; case "erode": Image <Gray, byte> eimg = new Image <Gray, byte>(show_img); Size kSize = new System.Drawing.Size(Convert.ToInt32(cmd[1]), Convert.ToInt32(cmd[2])); Point anchor = new Point(Convert.ToInt32(cmd[3]), Convert.ToInt32(cmd[4])); ElementShape shape = ElementShape.Rectangle; switch (cmd[5]) { case "ellipse": shape = ElementShape.Ellipse; break; case "rectangle": shape = ElementShape.Rectangle; break; case "cross": shape = ElementShape.Cross; break; } Mat element = CvInvoke.GetStructuringElement(shape, kSize, anchor); Mat dstImg = new Mat(); CvInvoke.Erode(eimg, dstImg, element, anchor, Convert.ToInt32(cmd[6]), BorderType.Default, new MCvScalar(0, 0, 0)); pre_img = show_img; show_img = dstImg.Bitmap; rform.UpdateImage(show_img); break; case "dilate": Image <Gray, byte> dimg = new Image <Gray, byte>(show_img); Size dSize = new System.Drawing.Size(Convert.ToInt32(cmd[1]), Convert.ToInt32(cmd[2])); Point danchor = new Point(Convert.ToInt32(cmd[3]), Convert.ToInt32(cmd[4])); ElementShape dshape = ElementShape.Rectangle; switch (cmd[5]) { case "ellipse": dshape = ElementShape.Ellipse; break; case "rectangle": dshape = ElementShape.Rectangle; break; case "cross": dshape = ElementShape.Cross; break; } Mat delement = CvInvoke.GetStructuringElement(dshape, dSize, danchor); Mat ddstImg = new Mat(); CvInvoke.Dilate(dimg, ddstImg, delement, danchor, Convert.ToInt32(cmd[6]), BorderType.Default, new MCvScalar(0, 0, 0)); pre_img = show_img; show_img = ddstImg.Bitmap; rform.UpdateImage(show_img); break; case "clearedge": EdgeFilter(); break; case "canny": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { Image <Gray, byte> cannyimg = new Image <Gray, byte>(show_img).Canny(Convert.ToDouble(cmd[1]), Convert.ToDouble(cmd[2])); pre_img = show_img; show_img = cannyimg.Bitmap; rform.UpdateImage(show_img); } break; case "corners": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { GetCornersByHarris(); //GetCorners(); } break; case "findrec": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { FindRectangle(Convert.ToInt32(cmd[1]), Convert.ToInt32(cmd[2])); } break; case "houghlines": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { Image <Gray, byte> img = new Image <Gray, byte>(show_img); LineSegment2D[][] lines = img.HoughLines( Convert.ToDouble(cmd[1]), // canny low threshold Convert.ToDouble(cmd[2]), // canny high threshold 1, // rho Math.PI / 180.0, // theta Convert.ToInt32(cmd[3]), // threshold(cross point) Convert.ToDouble(cmd[4]), // min lenght for line Convert.ToDouble(cmd[5]) // max allow gap between lines ); foreach (LineSegment2D line in lines[0]) { img.Draw(line, new Gray(0), 1); } pre_img = show_img; show_img = img.Bitmap; rform.UpdateImage(show_img); } break; case "iterrec": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { DrawAllRectangles(); } break; case "refresh": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { pre_img = show_img; show_img = ori_img; rform.UpdateImage(show_img); } break; case "saveimg": if (ori_img == null) { errorMsg = "please run showimg command first"; } else { show_img.Save("save_img_" + savet++ + ".png"); } break; case "exit": rform.ExitForm(); rform.Dispose(); rform = null; break; case "help": StreamReader input = new StreamReader(@"document.txt"); string doc; bool tag = false; while (!input.EndOfStream) { doc = input.ReadLine(); if (doc.Equals("<img>")) { tag = true; continue; } else if (doc.Equals("</img>")) { break; } if (tag) { mform.UpdateLog(doc); } } input.Close(); input.Dispose(); break; default: break; } } catch (Exception e) { mform.UpdateLog(e.Message); } if (errorMsg.Equals("none")) { mform.UpdateText("finished"); } else { mform.UpdateLog(errorMsg); } }
public static void RecognizeBill(object obj) { object[] objs = (object[])obj; MainForm mform = (MainForm)objs[0]; ori_img = (Bitmap)Bitmap.FromFile((string)objs[1]); RecognizeForm rform = new RecognizeForm(); ShowRecognizeForm(mform, rform); mform.UpdateText("Clear Edges"); EdgeFilter(); rform.UpdateImage(show_img); mform.UpdateLog("Clear Edges"); mform.UpdateText("Execute Sobel Filter"); SobelFilter(); rform.UpdateImage(show_img); mform.UpdateLog("Execute Sobel Filter"); mform.UpdateText("Find target Rectangles"); FindRectangle(10, 20); rform.UpdateImage(show_img); mform.UpdateLog("Find target Rectangles"); mform.UpdateText("Cut image"); CutImage(); mform.UpdateLog("Cut image"); mform.UpdateText("Recognize address"); TesseractEngine ocr = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "chi_tra+eng", EngineMode.Default); Pix img = PixConverter.ToPix(targets[1]); Page addpage = ocr.Process(img); string address = addpage.GetText().Trim().Replace(" ", String.Empty); ocr.Dispose(); mform.UpdateLog("Recognize address"); mform.UpdateText("Recognize eid, date, price"); Pix idpimg = PixConverter.ToPix(targets[2]); TesseractEngine ocre = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "eng", EngineMode.Default); Page idppage = ocre.Process(idpimg); string[] idpdata = idppage.GetText().Trim().Split(' '); int tar = 0; string eid = ""; for (int i = 0; i < idpdata.Length; i++) { Regex rex = new Regex("\\d{2}-\\d{2}-\\d{4}-\\d{2}-\\d{1}"); if (rex.IsMatch(idpdata[i])) { tar = i; Match match = rex.Match(idpdata[i]); eid = match.Value; break; } } ocre.Dispose(); string date = idpdata[tar + 1]; string price = idpdata[tar + 2].Replace("*", String.Empty); mform.UpdateLog("Recognize eid, date, price\n"); mform.UpdateText("Recognize kWh"); Pix kwhimg = PixConverter.ToPix(targets[0]); ocre = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "eng", EngineMode.Default); Page kwhpage = ocre.Process(kwhimg); string[] kwhdata = kwhpage.GetText().Trim().Split(' '); string kwh = ""; for (int i = 0; i < kwhdata.Length; i++) { Regex rex = new Regex("\\*\\d{1,}"); if (rex.IsMatch(kwhdata[i])) { Match match = rex.Match(kwhdata[i]); kwh = match.Value.Replace("*", String.Empty); break; } } ocre.Dispose(); mform.UpdateLog("Recognize kWh"); mform.UpdateLog("Result [ " + eid + " , " + date + " , " + price + " , " + kwh + " , " + address + " ]"); mform.UpdateText("Finished"); mform.UpdateLog("Finished"); }