C# .NET OCR 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1130473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:55:48  来源:igfitidea点击:

.NET OCRing an Image

c#.netocrmodi

提问by Kirschstein

I'm trying to use MODI to OCR a window's program. It works fine for screenshots I grab programmatically using win32 interop like this:

我正在尝试使用 MODI 对窗口程序进行 OCR。它适用于我使用 win32 互操作以编程方式抓取的屏幕截图,如下所示:

public string SaveScreenShotToFile()
{
    RECT rc;
    GetWindowRect(_hWnd, out rc);

    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;

    Bitmap bmp = new Bitmap(width, height);
    Graphics gfxBmp = Graphics.FromImage(bmp);
    IntPtr hdcBitmap = gfxBmp.GetHdc();

    PrintWindow(_hWnd, hdcBitmap, 0);

    gfxBmp.ReleaseHdc(hdcBitmap);
    gfxBmp.Dispose();

    string fileName = @"c:\temp\screenshots\" + Guid.NewGuid().ToString() + ".bmp";
    bmp.Save(fileName);
    return fileName;
}

This image is then saved to a file and ran through MODI like this:

然后将此图像保存到文件中,并像这样通过 MODI 运行:

    private string GetTextFromImage(string fileName)
    {

        MODI.Document doc = new MODI.DocumentClass();
        doc.Create(fileName);
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
        MODI.Image img = (MODI.Image)doc.Images[0];
        MODI.Layout layout = img.Layout;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < layout.Words.Count; i++)
        {
            MODI.Word word = (MODI.Word)layout.Words[i];
            sb.Append(word.Text);
            sb.Append(" ");
        }

        if (sb.Length > 1)
            sb.Length--;

        return sb.ToString();
    }

This part works fine, however, I don't want to OCR the entire screenshot, just portions of it. I try cropping the image programmatically like this:

这部分工作正常,但是,我不想对整个屏幕截图进行 OCR,只对其中的一部分进行 OCR。我尝试像这样以编程方式裁剪图像:

    private string SaveToCroppedImage(Bitmap original)
    {
        Bitmap result = original.Clone(new Rectangle(0, 0, 250, 250), original.PixelFormat);
        var fileName = "c:\" + Guid.NewGuid().ToString() + ".bmp";
        result.Save(fileName, original.RawFormat);

        return fileName;
    }

and then OCRing this smaller image, however MODI throws an exception; 'OCR running error', the error code is -959967087.

然后对这个较小的图像进行 OCR,但是 MODI 抛出异常;'OCR 运行错误',错误代码为-959967087。

Why can MODI handle the original bitmap but not the smaller version taken from it?

为什么 MODI 可以处理原始位图,而不能处理从中提取的较小版本?

采纳答案by Rhys Parry

Looks as though the answer is in giving MODI a bigger canvas. I was also trying to take a screenshot of a control and OCR it and ran into the same problem. In the end I took the image of the control, copied the image into a larger bitmap and OCRed the larger bitmap.

看起来答案是给 MODI 一个更大的画布。我还试图截取控件的屏幕截图并对其进行 OCR,但遇到了同样的问题。最后我拿了控件的图像,将图像复制到一个更大的位图,并对更大的位图进行 OCR。

Another issue I found was that you must have a proper extension for your image file. In other words, .tmp doesn't cut it.

我发现的另一个问题是您的图像文件必须有适当的扩展名。换句话说,.tmp 不会削减它。

I kept the work of creating a larger source inside my OCR method, which looks something like this (I deal directly with Image objects):

我在我的 OCR 方法中保留了创建更大源的工作,它看起来像这样(我直接处理 Image 对象):

public static string ExtractText(this Image image)
{
    var tmpFile = Path.GetTempFileName();
    string text;
    try
    {
        var bmp = new Bitmap(Math.Max(image.Width, 1024), Math.Max(image.Height, 768));
        var gfxResize = Graphics.FromImage(bmp);
        gfxResize.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
        bmp.Save(tmpFile + ".bmp", ImageFormat.Bmp);
        var doc = new MODI.Document();
        doc.Create(tmpFile + ".bmp");
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
        var img = (MODI.Image)doc.Images[0];
        var layout = img.Layout;
        text = layout.Text;
    }
    finally
    {
        File.Delete(tmpFile);
        File.Delete(tmpFile + ".bmp");
    }

    return text;
}

I'm not sure exactly what the minimum size is, but it appears as though 1024 x 768 does the trick.

我不确定最小尺寸到底是多少,但似乎 1024 x 768 可以解决问题。

回答by Kirschstein

the modi ocr is working only tif with me. try to save image in "tif".

modi ocr 只对我有用。尝试将图像保存在“tif”中。

sorry my bad english

对不起,我的英语不好

回答by Rhys Parry

I had the same problem "OCR running problem" with some images. I re-scaled the image (in my case by 50%), i.e. reduced its size and voila! it works!

我在某些图像上遇到了同样的问题“OCR 运行问题”。我重新缩放了图像(在我的例子中是 50%),即缩小了它的大小,瞧!有用!

回答by PhoenixCoder

I had the same issue while using the

我在使用时遇到了同样的问题

doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

on a tiff file that was 2400x2496. Resizing it to 50%(reducing the size) fixed the problem and the method was not throwing the exception anymore, however, it was incorrectly recognizing the text like detecting "relerence" instead of "reference" or "712017" instead of "712517". I kept trying different image sizes but they all had the same issue, until i changed the command to

在 2400x2496 的 tiff 文件上。将其调整为 50%(减小大小)解决了问题,该方法不再抛出异常,但是,它错误地识别了文本,例如检测“relerence”而不是“reference”或“712017”而不是“712517” . 我一直在尝试不同的图像大小,但它们都有相同的问题,直到我将命令更改为

doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

which meant that i don't want it to detect the orientation and not to fix any skewing. Now the command works fine on all images including the 2400x2496 tiff.

这意味着我不希望它检测方向而不是修复任何倾斜。现在该命令适用于所有图像,包括 2400x2496 tiff。

Hope this helps out people facing the same problem

希望这可以帮助面临同样问题的人

回答by chris

yes the posts in this thread helped me gettin it to work, here what i have to add:

是的,这个线程中的帖子帮助我开始工作,在这里我必须添加:

was trying to download images ( small ones ) then ocr...

正在尝试下载图像(小图像)然后 ocr ...

-when processing images, it seems that theyr size must be power of 2 ! ( was able to ocr images: 512x512 , 128x128, 256x64 .. other sizes mostly failed ( like 1103x334 ))

- 在处理图像时,它们的大小似乎必须是 2 的幂!(能够 ocr 图像:512x512、128x128、256x64 .. 其他尺寸大多失败(如 1103x334))

  • transparent background also made troubles. I got the best results when creating a new tif with powerof2 boundary, white background, paste the downloaded image into it, save.

  • scaling the image did not succeed for me, since OCR is getting wrong results , specially for "german" characters like "ü"

  • in the end i also used: doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

  • using modi from office 2003

  • 透明背景也制造麻烦。创建带有powerof2边界,白色背景的新tif时,我得到了最好的结果,将下载的图像粘贴到其中,保存。

  • 缩放图像对我来说没有成功,因为 OCR 得到了错误的结果,特别是对于像“ü”这样的“德语”字符

  • 最后我还使用了: doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

  • 使用 office 2003 中的 modi

greetings

你好

womd

世界

回答by Sulaiman

doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

Which means that I don't want it to detect the orientation and not fix any skewing. Now the command works fine on all images including the 2400x2496 tiff.

这意味着我不希望它检测方向而不修复任何倾斜。现在该命令适用于所有图像,包括 2400x2496 tiff。

But image should be in .tif.

但图像应该在 .tif 中。

Hope this helps out people facing the same problem.

希望这可以帮助面临同样问题的人。

回答by andycted

what solved my situation was using a photo editor (Paint.NET) and use the sharpen effect at maximum.

解决我的情况是使用照片编辑器(Paint.NET)并最大程度地使用锐化效果。

I also used: doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

我也用过: doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);