Пример #1
0
        private static void AnalyseLayout()
        {
            string imageFile = @"D:\Self-Study\OpenSources\Tesseract\original\phototest.tif";
            //imageFile = @"D:\Self-Study\OpenSources\Tesseract\original\eurotext.tif";

            TesseractProcessor processor = new TesseractProcessor();
            processor.InitForAnalysePage();
            processor.SetPageSegMode(ePageSegMode.PSM_AUTO);

            using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
            {
                DocumentLayout doc = processor.AnalyseLayout(bmp);
                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)
                        {
                            DrawBlock(grph, block);
                        }
                    }

                    tmp.Save(@"D:\temp\page_layout_test2.bmp");
                }
            }
        }
Пример #2
0
        private static void AnalyseLayout()
        {
            string imageFile = @"D:\Self-Study\OpenSources\Tesseract\original\phototest.tif";
            //imageFile = @"D:\Self-Study\OpenSources\Tesseract\original\eurotext.tif";

            TesseractProcessor processor = new TesseractProcessor();

            processor.InitForAnalysePage();
            processor.SetPageSegMode(ePageSegMode.PSM_AUTO);

            using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
            {
                DocumentLayout doc = processor.AnalyseLayout(bmp);
                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)
                        {
                            DrawBlock(grph, block);
                        }
                    }

                    tmp.Save(@"D:\temp\page_layout_test2.bmp");
                }
            }
        }
        public static void AnalyseLayout()
        {
            int n_images = Workspace.Images.Length;
            int i_image  = 0;

            for (; i_image < n_images; i_image++)
            {
                string fileName = Workspace.Images[i_image];

                Console.WriteLine("{0} Image: {1}", i_image, fileName);

                string imageFile = Path.Combine(Workspace.InputFolder, fileName);

                string name = Path.GetFileNameWithoutExtension(imageFile);

                string outFile  = Path.Combine(Workspace.OutputFolder, string.Format("Simple1_{0}_layout.bmp", name));
                string outFile2 = Path.Combine(Workspace.OutputFolder, string.Format("Simple1_{0}_grey.bmp", name));
                string outFile3 = Path.Combine(Workspace.OutputFolder, string.Format("Simple1_{0}_bin.bmp", name));

                using (TesseractProcessor processor = new TesseractProcessor())
                {
                    processor.InitForAnalysePage();
                    //processor.SetPageSegMode(ePageSegMode.PSM_AUTO);

                    using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
                    {
                        DateTime started = DateTime.Now;
                        DateTime ended   = DateTime.Now;

                        DocumentLayout doc = null;

                        unsafe
                        {
                            started = DateTime.Now;

                            doc = processor.AnalyseLayout(bmp);

                            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);
                        }
                    }
                }
            }
        }
Пример #4
0
    static oTesseractRequest __ocrExecute(oTesseractRequest req, Bitmap bitmap)
    {
        using (TesseractProcessor processor = new TesseractProcessor())
        {
            // call SetVariable() method before passing image(api->SetImage(image))
            // 0: otsu
            // 1: isodata local adaptive
            // 2: sauvola local adaptive => not implement yet
            //processor.SetVariable("tessedit_thresholding_method", "0");
            processor.SetVariable("tessedit_thresholding_method", "1");

            //processor.InitForAnalysePage();
            //processor.SetPageSegMode(ePageSegMode.PSM_AUTO_ONLY);
            //var success = processor.Init(req.data_path, req.lang, (int)eOcrEngineMode.OEM_DEFAULT);

            //var imageColor = new Emgu.CV.Mat();
            //var imageCV = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>(bitmap); //Image Class from Emgu.CV
            ////var imageCV = new Emgu.CV.Image<Emgu.CV.Structure.Gray, byte>(bitmap); //Image Class from Emgu.CV
            //var image = imageCV.Mat; //This is your Image converted to Mat

            //if (image.NumberOfChannels == 1)
            //    Emgu.CV.CvInvoke.CvtColor(image, imageColor, Emgu.CV.CvEnum.ColorConversion.Gray2Bgr);
            //else
            //    image.CopyTo(imageColor);

            using (var m0 = new MemoryStream())
            {
                bitmap.Save(m0, ImageFormat.Jpeg);
                //using (Bitmap bmp = Bitmap.FromFile(@"C:\temp\1.jpg") as Bitmap)
                using (Bitmap bmp = new Bitmap(m0))
                {
                    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.AnalyseLayout(bmp);
                        }
                        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);
    }