LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: C# Convert Light Text to Black Text for OCR
#1
Posted
:
Wednesday, February 12, 2020 1:16:31 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Sometimes certain documents could contain lighter color text or documents may be scanned in color. This may effect expected OCR results. You can find the light gray text DOCX file and the processed JPEG file in the ZIP file attached at the bottom of this post. The below C# console application code will showcase how to convert lighter text (in this case light gray) to black text using the LEADTOOLS v20 SDK.
Code:
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing.Core;
namespace ConvertLightGrayTextToBlack
{
class Program
{
static void Main(string[] args)
{
SetLicense();
ConvertLightGrayText();
}
private static void SetLicense()
{
string licenseFilePath = @"C:\LEADTOOLS 20\Support\Common\License\LEADTOOLS.lic";
string developerKey = File.ReadAllText(@"C:\LEADTOOLS 20\Support\Common\License\LEADTOOLS.lic.key");
RasterSupport.SetLicense(licenseFilePath, developerKey);
}
private static void ConvertLightGrayText()
{
string filePath = @"C:\Users\Public\Documents\LEADTOOLS Images\ConvertLightGrayTexttoBlackforOCR.docx";
string destPath = @"C:\Users\Public\Documents\LEADTOOLS Images\ConvertLightGrayTexttoBlackforOCR.jpg";
using (RasterCodecs codecs = new RasterCodecs())
{
RasterImage image = codecs.Load(filePath, 0, CodecsLoadByteOrder.Bgr, 1 ,-1);
AutoBinarizeCommand command = new AutoBinarizeCommand();
command.Flags = AutoBinarizeCommandFlags.UseUserThreshold;
command.Factor = 250;
command.Run(image);
codecs.Save(image, destPath, RasterImageFormat.Jpeg, image.BitsPerPixel);
}
}
}
}
Relevant LEADTOOLS v20 links:
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: C# Convert Light Text to Black Text for OCR
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.