Gia*_*ini 2 c# tesseract image-processing aforge
我目前正在使用 OCR 程序。我正在使用超正方体,我需要校正图像以提高检测到的字符的质量。问题是 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");
Run Code Online (Sandbox Code Playgroud)
这是Atalasoft 的实现
AtalaImage img = new AtalaImage(imagePath);
AutoDeskewCommand cmd = new AutoDeskewCommand();
AtalaImage resultImage = cmd.Apply(img).Image;
resultImage.Save("result.tif", new TiffEncoder(), null);
Run Code Online (Sandbox Code Playgroud)
我终于明白为什么它不起作用:图像应该转换为 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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4197 次 |
最近记录: |