.NET OCR 图像

发布于 2024-07-26 20:08:51 字数 1826 浏览 4 评论 0原文

我正在尝试使用 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;
}

然后将此图像保存到文件中并通过 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();
    }

这部分工作正常,但是,我不想 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;
    }

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

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

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:

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:

    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:

    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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

德意的啸 2024-08-02 20:08:51

看起来答案似乎在于为莫迪提供更大的画布。 我还尝试截取控件的屏幕截图并对其进行 OCR,但遇到了同样的问题。 最后,我拍摄了控件的图像,将图像复制到更大的位图中,并对更大的位图进行 ORed。

我发现的另一个问题是您的图像文件必须有正确的扩展名。 换句话说,.tmp 并不能解决问题。

我在 OCR 方法中继续创建更大的源,看起来像这样(我直接处理图像对象):

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;
}

我不确定最小尺寸到底是多少,但看起来 1024 x 768 确实可以诡计。

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.

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

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

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.

蓝眸 2024-08-02 20:08:51

是的,这个帖子中的帖子帮助我让它工作,这里我必须添加:

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

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

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

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

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

  • 使用 Office 2003 中的 modi

问候语

womd

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...

-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 ))

  • 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

greetings

womd

音盲 2024-08-02 20:08:51

modi ocr 仅适用于我。
尝试将图像保存为“tif”。

抱歉我的英语不好

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

sorry my bad english

故事还在继续 2024-08-02 20:08:51
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);

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

但图像应该是.tif。

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

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.

But image should be in .tif.

Hope this helps out people facing the same problem.

风吹短裙飘 2024-08-02 20:08:51

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

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!

水溶 2024-08-02 20:08:51

使用时遇到了同样的问题

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

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

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

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

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

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

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.

Hope this helps out people facing the same problem

红颜悴 2024-08-02 20:08:51

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

我还用过:
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, 假, 假);

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文