Skew correction

When a page is fed to a flatbed scanner (mechanically or manually) or photographed with a smartphone, it is nearly impossible to achieve perfect alignment. As a result, a slight skew (tilt) inevitably occurs in scanned images or photographs.

Skew angle detection and image straightening is critical to the OCR process as it directly affects the reliability and efficiency of segmentation and text extraction. Aspose.OCR offers automated processing algorithms to correct image tilt (deskew) before proceeding to recognition.

Detecting skew angles

To find out skew angles for all images in a batch, use Aspose.OCR.AsposeOcr.CalculateSkew method. It returns a list of Aspose.OCR.SkewOutput objects, one per image.

Property Type Description
Angle float Skew angle in degrees.
ImageIndex int Sequence number of the image on the page. When working with single-page images, this value is always 0.
Page int Page number. When working with single-page images, this value is always 0.
Source string The full path or URL of the source file. If the file is provided as a MemoryStream object, an array of pixels, or a Base64 string, this value will be empty.
Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Add PDF documents to OcrInput object
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(InputType.PDF);
input.Add("source.pdf");
// Detect skew angles
List<Aspose.OCR.SkewOutput> angles = recognitionEngine.CalculateSkew(input);
foreach(Aspose.OCR.SkewOutput angle in angles) Console.WriteLine($"File: {angle.Source} | Page: {angle.Page} | Image: {angle.ImageIndex} | Angle: {angle.Angle}°");

Skewed image

> File: "C:\source.pdf" | Page: 0 | Image: 0 | Angle: 5.9°

Automatic skew correction

To automatically straighten skewed image before recognition, run the image through AutoSkew processing filter.

Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Enable automatic skew correction
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();
filters.Add(Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter.AutoSkew());
// Add an image to OcrInput object and apply processing filters
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(Aspose.OCR.InputType.SingleImage, filters);
input.Add("source.png");
// Save processed image to the folder
Aspose.OCR.ImageProcessing.Save(input, @"C:\result");
// Recognize image
List<Aspose.OCR.RecognitionResult> results = recognitionEngine.Recognize(input);
foreach(Aspose.OCR.RecognitionResult result in results)
{
	Console.WriteLine(result.RecognitionText);
}
Skewed image Deskewed image

Manual skew correction

In some edge cases, automatic skew correction may not detect the angle of the image. This can happen with poor quality photographs with significant perspective distortion.

To deal with such situations, you can rotate the image by the specified degree using Rotate image processing filter. The rotation angle is passed in degrees:

  • -360 to 0: rotate counterclockwise;
  • 0 to 360: rotate clockwise.
Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Rotate the image 90 degrees counterclockwise
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();
filters.Add(Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter.Rotate(-90));
// Add an image to OcrInput object and apply processing filters
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(Aspose.OCR.InputType.SingleImage, filters);
input.Add("source.png");
// Save processed image to the folder
Aspose.OCR.ImageProcessing.Save(input, @"C:\result");
// Recognize image
List<Aspose.OCR.RecognitionResult> results = recognitionEngine.Recognize(input);
foreach(Aspose.OCR.RecognitionResult result in results)
{
	Console.WriteLine(result.RecognitionText);
}

Image regions processing

Automatic skew correction and manual rotation filters can be applied to specific regions of an image. For example, you can straighten an illustration on a page while leaving the rest of the content unchanged.

To apply a filter to an area, specify its top left corner along with width and height as Aspose.Drawing.Rectangle object. If the region is omitted, the filter is applied to the entire image.

Aspose.Drawing.Rectangle rectangle = new Aspose.Drawing.Rectangle(5, 161, 340, 340);
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();
filters.Add(Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter.Rotate(90, rectangle));

Usage scenarios

  • Automatic skew correction is recommended in almost all cases, except for perfectly straight scans.
  • Manual rotation is recommended for:
    • photos of low quality;
    • images with a significant angle of inclination.