c# - 如何创建 tesseract OCR 可读的 TIFF 文件?

标签 c# ocr tesseract libtiff libtiff.net

我想让 tesseract ORC 运行图像文件,扫描内容。
问题似乎是 tesseract 不仅需要 TIFF,而且还要求 tiff 文件是某种格式。

只有一个普通的 tiff 文件,我得到:

root@toshiba:~/Desktop# tesseract crap.tif crap.txt
Tesseract Open Source OCR Engine
check_legal_image_size:Error:Only 1,2,4,5,6,8 bpp are supported:32
Segmentation fault

到目前为止,我已经设法找到了解毒剂。
它包括使用 GIMP,转到“图像”>“模式”>“索引”,然后将“生成最佳调色板”、“最大颜色数”设置为 256。
enter image description here
那么我必须在“另存为”之前再做一招。
转到图层 > 透明度 > 删除 Alpha channel , 这将消除透明度,因为 TIF 图像不能具有透明度。

enter image description here

现在的问题是我的输入图像来自 C#,并使用 AFORGE.NET 图像分析过滤器进行了预处理。

我还找到了 LibTiff 的 .NET 端口,以及如何在此处使用调色板编写图像的示例:
http://bitmiracle.com/libtiff/help/create-tiff-with-palette-(color-map).aspx

但我不知道如何将数据从源 tiff(调色板错误的那个)获取到目标 tiff(调色板格式正确的那个)...

最佳答案

我听说 tesseract 适用于灰度 TIFF。

因此,请尝试使用以下代码将您的 TIFF 图像转换为灰度图像:

using (Tiff tif = Tiff.Open(@"input.tif", "r"))
{
    FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
    int width = value[0].ToInt();

    value = tif.GetField(TiffTag.IMAGELENGTH);
    int height = value[0].ToInt();

    int xresolution = -1;
    value = tif.GetField(TiffTag.XRESOLUTION);
    if (value != null)
        xresolution = value[0].ToInt();

    int yresolution = -1;
    value = tif.GetField(TiffTag.YRESOLUTION);
    if (value != null)
        yresolution = value[0].ToInt();

    int[] raster = new int[height * width];
    if (!tif.ReadRGBAImageOriented(width, height, raster, Orientation.TOPLEFT))
    {
        System.Windows.Forms.MessageBox.Show("Could not read image");
        return;
    }

    string fileName = "grayscale.tif";
    using (Tiff output = Tiff.Open(fileName, "w"))
    {
        output.SetField(TiffTag.IMAGEWIDTH, width);
        output.SetField(TiffTag.IMAGELENGTH, height);
        output.SetField(TiffTag.ROWSPERSTRIP, 1);
        output.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        output.SetField(TiffTag.BITSPERSAMPLE, 8);
        output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
        output.SetField(TiffTag.COMPRESSION, Compression.LZW);
        output.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
        output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);

        if (xresolution != -1 && yresolution != -1)
        {
            output.SetField(TiffTag.XRESOLUTION, xresolution);
            output.SetField(TiffTag.YRESOLUTION, yresolution);
        }

        byte[] samples = new byte[width];
        for (int y = 0, index = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                int rgb = raster[index++];

                // compute pixel brightness taking human eye's sensitivity
                // to each of red, green and blue colors into account
                byte gray = (byte)(Tiff.GetR(rgb) * 0.299 + Tiff.GetG(rgb) * 0.587 + Tiff.GetB(rgb) * 0.114);

                // Alternative formulas for RGB -> Gray conversion

                //byte gray = (byte)(Tiff.GetR(rgb) * 0.2125 + Tiff.GetG(rgb) * 0.7154 + Tiff.GetB(rgb) * 0.0721);
                //byte gray = (byte)((Tiff.GetR(rgb) + Tiff.GetG(rgb) + Tiff.GetB(rgb)) / 3);

                samples[x] = gray;
            }

            output.WriteEncodedStrip(y, samples, samples.Length);
        }
    }
}

希望它能解决问题。

关于c# - 如何创建 tesseract OCR 可读的 TIFF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8995941/

相关文章:

c# - Windows 2003 上的 .NET 应用程序性能问题

c# - 如何将 RTF 文件转换为 pdf 文件?

c++ - OpenALPR 的段错误

android - 通过 OCR Tesseract 从图像中提取文本 - 错误 : can't find source file on baseApi. ini

c# - 在写入 Logger 或 Console 时将 foreach 转换为 LINQ

c++ - tesseract 错误置信决策

ocr - Tesseract 培训 - 只有数字的新字体

ubuntu - 使用 ruby​​ ffi-inliner 编译 tesseract-ocr gem 时出错

python-2.7 - 无法在 Mac 上安装 Tesseract-OCR

c# - 了解 MVC 5 用户声明表