Sign in

How to convert an image of text into a binary view with Deep Learning in C# .NET Framework

Optical character recognition (OCR) can be greatly improved if an image is preprocessed first. Setting an image to binary view (pure black and white) is one of the most effective ways of doing this, and an excellent way to boost accuracy. Today, however, we will not be delving into the rabbit hole that is Deep Learning. Instead we will be taking a massive shortcut — in short, an API. Instead of hours, how does a few minutes sound?

First we need to install our API client via NuGet, which can be accomplished by pasting this code into the console of Package Manager.

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

Now, we simply call PreprocessingBinarizeAdvanced, like so:

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 PreprocessingBinarizeAdvancedExample{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{// Convert an image of text into a binary (light and dark) view with MLbyte[] result = apiInstance.PreprocessingBinarizeAdvanced(imageFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling PreprocessingApi.PreprocessingBinarizeAdvanced: " + e.Message );}}}}

And that’s all there is to it. Our image will now be in ultra-high contrast black and white and ready for OCR.

Image for post
Image for post

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