How to straighten a document image using Deep Learning in C# .NET Framework

Before performing optical character recognition, it is important for accuracy’s sake to perform a bit of preprocessing on the image involved. Besides binarizing our view (covered in a previous tutorial), proper rotation of our image is another key step. Normally this would involve training an AI with Deep Learning to find the angle of our document page and then rotating the image using that value. That’s all well and good, but what if I told you that there is a much easier way? What if there was a way to accomplish this task in just a couple of minutes? That method is our topic today.

First off, we must install our API client via the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.OCR -Version 2.1.4

With that done, let us call PreprocessingUnrotateAdvanced, as demonstrated with this snippet here:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.OCR.Api;using Cloudmersive.APIClient.NET.OCR.Client;using Cloudmersive.APIClient.NET.OCR.Model;namespace Example{public class PreprocessingUnrotateAdvancedExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new PreprocessingApi();var imageFile = new System.IO.Stream(); // System.IO.Stream | Image file to perform OCR on.  Common file formats such as PNG, JPEG are supported.try{// Detect and unrotate a document image (advanced)byte[] result = apiInstance.PreprocessingUnrotateAdvanced(imageFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling PreprocessingApi.PreprocessingUnrotateAdvanced: " + e.Message );}}}}

That’s it. Done. The API will find our angle and rotate our image back to perfect text alignment. We are all set for OCR!

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store