Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Tesseract空页_C#_Image Processing_Ocr_Tesseract - Fatal编程技术网

C# Tesseract空页

C# Tesseract空页,c#,image-processing,ocr,tesseract,C#,Image Processing,Ocr,Tesseract,我使用tesseract检测图像上的字符 try { using (var engine = new TesseractEngine(@"C:\Users\ea\Documents\Visual Studio 2015\Projects\ocrtTest", "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(

我使用tesseract检测图像上的字符

        try
        {
            using (var engine = new TesseractEngine(@"C:\Users\ea\Documents\Visual Studio 2015\Projects\ocrtTest", "eng", EngineMode.Default))
            {
                using (var img = Pix.LoadFromFile(testImagePath))
                {
                    Bitmap src = (Bitmap)Image.FromFile(testImagePath);
                    using (var page = engine.Process(img))
                    {
                        var text = page.GetHOCRText(1);
                        File.WriteAllText("test.html", text);
                        //Console.WriteLine("Text: {0}", text);
                        //Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence());

                        int p = 0;
                        int l = 0;
                        int w = 0;
                        int s = 0;
                        int counter = 0;
                        using (var iter = page.GetIterator())
                        {
                            iter.Begin();
                            do
                            {
                                do
                                {
                                    do
                                    {
                                        do
                                        {
                                            do
                                            {
                                                //if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
                                                //{
                                                //    logger.Log("New block");
                                                //}
                                                if (iter.IsAtBeginningOf(PageIteratorLevel.Para))
                                                {
                                                    p++;//counts paragraph
                                                    //logger.Log("New paragraph");
                                                }
                                                if (iter.IsAtBeginningOf(PageIteratorLevel.TextLine))
                                                {
                                                    l++;//count lines
                                                    //logger.Log("New line");
                                                }
                                                if (iter.IsAtBeginningOf(PageIteratorLevel.Word))
                                                {
                                                    w++;//count words
                                                    //logger.Log("New word");
                                                }
                                                s++;//count symbols
                                                //logger.Log(iter.GetText(PageIteratorLevel.Symbol));
                                                // get bounding box for symbol
                                                Rect symbolBounds;
                                                if (iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out symbolBounds))
                                                {
                                                    Rectangle dueDateRectangle = new Rectangle(symbolBounds.X1, symbolBounds.Y1, symbolBounds.X2 - symbolBounds.X1, symbolBounds.Y2 - symbolBounds.Y1);
                                                    rect = dueDateRectangle;
                                                    PixelFormat format = src.PixelFormat;
                                                    Bitmap cloneBitmap = src.Clone(dueDateRectangle, format);
                                                    MemoryStream ms = new MemoryStream();
                                                    cloneBitmap.Save(ms, ImageFormat.Png);
                                                    ms.Position = 0;
                                                    Image i = Image.FromStream(ms);
                                                    //i.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
                                                    i.Save("character" + counter + ".bmp", ImageFormat.Png);
                                                    counter++;
                                                }
                                            } while (iter.Next(PageIteratorLevel.Word, PageIteratorLevel.Symbol));
                                            // DO any word post processing here (e.g. group symbols by word)
                                        } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
                                    } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                                } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
                            } while (iter.Next(PageIteratorLevel.Block));
                        }
                        Console.WriteLine("Pragraphs = " + p);
                        Console.WriteLine("Lines = " + l);
                        Console.WriteLine("Words = " + w);
                        Console.WriteLine("Symbols = " + s);
                    }
当我有一个包含大量文本的图像时,它就起作用了,但当我有一个只有一个字母的图像时,它就不起作用了。

它找到了一个符号,我在输入中看到了它。符号=1。但是它不能得到边界框。为什么? 和我使用字母图像时一样
您可能需要使用不同的
页面分割模式和
OCR引擎模式测试
OCR
,以获得最佳结果。以下是
Tesseract 4.0
中提供的使用信息

Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.<br>

OCR Engine modes:
  0    Original Tesseract only.
  1    Neural nets LSTM only.
  2    Tesseract + LSTM.
  3    Default, based on what is available.
页面分割模式:
仅限0方向和脚本检测(OSD)。
1使用OSD进行自动页面分割。
2自动页面分割,但没有OSD或OCR。
3全自动页面分割,但没有OSD。(默认)
4假设一列文本大小可变。
5假设有一个垂直对齐的文本块。
6假设一个统一的文本块。
7将图像视为单个文本行。
8将图像视为单个单词。
9将图像视为圆圈中的单个单词。
10将图像视为单个字符。
11稀疏文本。在没有特定顺序的情况下,尽可能多地查找文本。
12带有OSD的稀疏文本。
13生料线。将图像视为单个文本行,
绕过特定于Tesseract的黑客攻击。
OCR引擎模式: 仅0个原始TeserAct。 1仅限LSTM神经网络。 2台Tesseract+LSTM。 3默认值,基于可用的内容。
比如说,

  • psm 8
    将为
    OCR
    单个单词提供最佳结果
  • psm 6
    可能会给出文本块的最佳结果

在代码中,它表明您使用了默认的
引擎模式
,而未指定
分段模式
。您可以进行更多的测试,以确定哪些模式给出正确的结果。

对于下面的图像,请在tesseract命令中使用--psm 9


只需添加一个psm用法示例:
tesseract inputFile output-l lang--psm 8