I'm currently working with an OCR program. I'm using tesseract and i need to deskew images to improve the quality of the detected characters. The problem is that the deskew property given by tesseract doesn't produce enough attractive results. So i tried to deskew the image with AForge and Atalasoft, but every time, no matter what, the image is not in the format they require. What am i doing wrong? Or there is a better solution?
This is AForge implementation
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");
This is Atalasoft implementation
AtalaImage img = new AtalaImage(imagePath);
AutoDeskewCommand cmd = new AutoDeskewCommand();
AtalaImage resultImage = cmd.Apply(img).Image;
resultImage.Save("result.tif", new TiffEncoder(), null);
I finally managed to understand why it wasn't working: the image should be converted to Format8bppIndexed or the method skewChecker.GetSkewAngle(image) will throw an exception
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();