static oTesseractRequest __ocrExecute2(oTesseractRequest req, Bitmap bitmap) { using (TesseractProcessor processor = new TesseractProcessor()) { processor.InitForAnalysePage(); using (GreyImage greyImage = GreyImage.FromImage(bitmap)) { //greyImage.Save(ImageFormat.Bmp, outFile2); ImageThresholder thresholder = new AdaptiveThresholder(); using (BinaryImage binImage = thresholder.Threshold(greyImage)) { DocumentLayout doc = null; switch (req.command) { case TESSERACT_COMMAND.GET_TEXT: //string s = tes.GetText().Trim(); //req.output_text = s; //req.output_count = s.Length; req.ok = 1; break; default: unsafe { doc = processor.AnalyseLayoutBinaryImage( binImage.BinaryData, greyImage.Width, greyImage.Height); } if (doc != null) { var bs = new List <string>(); if (doc.Blocks.Count > 0) { for (int i = 0; i < doc.Blocks.Count; i++) { for (int j = 0; j < doc.Blocks[i].Paragraphs.Count; j++) { bs.AddRange(doc.Blocks[j].Paragraphs[j].Lines .Select(x => string.Format( "{0}_{1}_{2}_{3}", x.Left, x.Top, x.Right, x.Bottom))); } } } req.output_format = "left_top_right_bottom"; req.output_text = string.Join("|", bs.ToArray()); req.output_count = bs.Count; req.ok = 1; } break; } } } } return(req); }
static void Simple2_AnalyseLayout() { int n_images = Images.Length; int i_image = 0; for (; i_image < n_images; i_image++) { string fileName = Images[i_image]; Console.WriteLine("{0} Image: {1}", i_image, fileName); string imageFile = Path.Combine(InputFolder, fileName); string name = Path.GetFileNameWithoutExtension(imageFile); string outFile = Path.Combine(OutputFolder, string.Format("Simple2_{0}_layout.bmp", name)); string outFile2 = Path.Combine(OutputFolder, string.Format("Simple2_{0}_grey.bmp", name)); string outFile3 = Path.Combine(OutputFolder, string.Format("Simple2_{0}_bin.bmp", name)); using (TesseractProcessor processor = new TesseractProcessor()) { processor.InitForAnalysePage(); //processor.SetPageSegMode(ePageSegMode.PSM_AUTO); using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap) { using (GreyImage greyImage = GreyImage.FromImage(bmp)) { greyImage.Save(ImageFormat.Bmp, outFile2); ImageThresholder thresholder = new AdaptiveThresholder(); using (BinaryImage binImage = thresholder.Threshold(greyImage)) { binImage.Save(ImageFormat.Bmp, outFile3); DateTime started = DateTime.Now; DateTime ended = DateTime.Now; DocumentLayout doc = null; unsafe { started = DateTime.Now; doc = processor.AnalyseLayoutBinaryImage( binImage.BinaryData, greyImage.Width, greyImage.Height); ended = DateTime.Now; Console.WriteLine("Duration AnalyseLayout: {0} ms", (ended - started).TotalMilliseconds); } Console.WriteLine(doc.ToString()); using (Image tmp = new Bitmap(bmp.Width, bmp.Height)) // prevents one-byte index format { using (Graphics grph = Graphics.FromImage(tmp)) { Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height); grph.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel); grph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; foreach (Block block in doc.Blocks) { Render.DrawBlock(grph, block); } } tmp.Save(outFile); } } } } } } }