Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Friday, November 17, 2017 5:32:06 PM(UTC)

Nick  
Nick

Groups: Registered, Tech Support, Administrators
Posts: 163

Was thanked: 9 time(s) in 9 post(s)

Sometimes specific backgrounds can interfere with an OCR operation. This code tip shows how to use a double-pass of image preprocessing functionality to preserve text while removing a bothersome background. Consider the following image:
grad1.png

Note the "LEADTOOLS" text at the top of the picture. This image isn't suitable for OCR not due to how close the text is to the color behind it, but due to the magnitude of the fade from light to dark. This fade toward the darkness at the bottom of the image skews the threshold value for the binarization to such an extent that the text would be considered just as light as its surrounding background such that it would never survive the default processing required by the OCR engine.

However, the text can be preserved by selectively applying the ContrastBrightnessIntensityCommand in two steps. Here's our documentation on the command:
ContrastBrightnessIntensityCommand

The first pass will be to diminish the background while preserving the text. This is done by completely reducing the contrast and brightness, but maximizing the intensity. This reduces the contrast, and dims the image, while making the bright pixels brighter.
Code:

RasterCodecs codecs = new RasterCodecs();
RasterImage image = codecs.Load(@"grad1.png");
ContrastBrightnessIntensityCommand cbic1 = new ContrastBrightnessIntensityCommand(-1000, -1000, 1000);
cbic1.Run(image);


Here's the output produced.
grad2.png

This is still insufficient for OCR. However, running the command again on the modified image, with adjusted parameters, reduces the background farther.
Code:

ContrastBrightnessIntensityCommand cbic2 = new ContrastBrightnessIntensityCommand(1000, -400, 1000);
cbic2.Run(image);


This increases the contrast introduced in the last step between the text and the background behind it, dims the image farther, yet increases the intensity to preserve the bright areas. Here's the results of this.
grad3.png

At this point the text is, relatively, the brightest part of the image. Here's the ultimate processing image which is passed to the OCR engine.
grad-pr.png

This procedure can be modified based on the content of the image the preprocessing necessary.

Edited by user Wednesday, February 16, 2022 4:39:26 PM(UTC)  | Reason: Updated link to v22 and reformatted code

Nick Crook
Developer Support Engineer
LEAD Technologies, Inc.
LEAD Logo
thanks 1 user thanked Nick for this useful post.
Josh Clark on 5/17/2021(UTC)
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

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.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.058 seconds.