c#如何校正图像

时间:2017-05-11 10:51:11

标签: c# image-processing tesseract aforge

我目前正在使用OCR计划。我使用tesseract,我需要去抖动图像,以提高检测到的字符的质量。问题是tesseract给出的偏斜校正特性并没有产生足够的吸引力。所以我试图用AForge和Atalasoft来修改图像,但每次,无论如何,图像都不是他们需要的格式。我究竟做错了什么?或者有更好的解决方案?

这是AForge的实施

        System.Drawing.Bitmap imageToBitmap = AForge.Imaging.Image.FromFile(imagePath);
        Console.WriteLine("before  " + imageToBitmap.PixelFormat);
        System.Drawing.Bitmap NewPicture = imageToBitmap.Clone(new System.Drawing.Rectangle(0, 0, imageToBitmap.Width, imageToBitmap.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        var dop = AForge.Imaging.Image.Clone(imageToBitmap, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        Console.WriteLine("after  " + dop.PixelFormat);

        AForge.Imaging.DocumentSkewChecker skewChecker = new AForge.Imaging.DocumentSkewChecker();
        // get documents skew angle
        double angle = skewChecker.GetSkewAngle(dop);
        // create rotation filter
        AForge.Imaging.Filters.RotateBilinear rotationFilter = new AForge.Imaging.Filters.RotateBilinear(-angle);
        rotationFilter.FillColor = System.Drawing.Color.White;
        // rotate image applying the filter
        System.Drawing.Bitmap rotatedImage = rotationFilter.Apply(imageToBitmap);

        rotatedImage.Save("deskewedImage");

这是Atalasoft的实现

        AtalaImage img = new AtalaImage(imagePath);
        AutoDeskewCommand cmd = new AutoDeskewCommand();
        AtalaImage resultImage = cmd.Apply(img).Image;
        resultImage.Save("result.tif", new TiffEncoder(), null);

1 个答案:

答案 0 :(得分:1)

我终于理解了为什么它不起作用:图像应该转换为Format8bppIndexed或方法skewChecker.GetSkewAngle(image)将抛出异常

        Bitmap tempImage = AForge.Imaging.Image.FromFile(imagePath);
        Bitmap image;
        if (tempImage.PixelFormat.ToString().Equals("Format8bppIndexed"))
        {
            image = tempImage;
        }
        else
        {
            image = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(tempImage);
        }

        tempImage.Dispose();

        AForge.Imaging.DocumentSkewChecker skewChecker = new AForge.Imaging.DocumentSkewChecker();
        // get documents skew angle
        double angle = skewChecker.GetSkewAngle(image);
        // create rotation filter
        AForge.Imaging.Filters.RotateBilinear rotationFilter = new AForge.Imaging.Filters.RotateBilinear(-angle);
        rotationFilter.FillColor = Color.Black;
        // rotate image applying the filter
        Bitmap rotatedImage = rotationFilter.Apply(image);

        var deskewedImagePath = folderSavePath + filename + "_deskewed.tiff";
        rotatedImage.Save(deskewedImagePath, System.Drawing.Imaging.ImageFormat.Tiff);

        image.Dispose();
        rotatedImage.Dispose();