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



Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, July 11, 2019 12:58:00 PM(UTC)
Joe Kerrigan

Groups: Registered, Tech Support
Posts: 6

The following code snippet takes images such as those captured with mobile device cameras, automatically crops and deskews to adjust for camera position, and performs automatic preprocessing on the result. Users will commonly use their mobile device cameras as document scanners. Unlike the largely controlled, evenly lit environments of traditional document scanners, mobile cameras introduce variations in camera positioning, lighting/shadows, and other environmental factors that will negatively impact the quality of the resulting image. Luckily, LEADTOOLS image processing commands can help account for these variations, greatly improving the scan quality achieved without compromising the convenience of a mobile scan.

The snippet uses PerspectiveDeskewCommand to detect the document and perform a transformation to restore its original rectangular shape, even if the camera position isn’t perfectly orthogonal to the document. Next, AutoColorLevelCommand, StretchIntensityCommand, SelectiveColorCommand, and ColorIntensityBalanceCommand are used to adjust the image’s colors, enhancing readability, improving colors, and maintaining the visual fidelity.

Here's the code:


static void PreprocessImage(string imagePath, bool shouldCrop)
   RasterImage image = codecs.Load(imagePath);
   var outDir = Path.Combine(Path.GetDirectoryName(imagePath), "preprocessed");
   if (!Directory.Exists(outDir))
   string outFile = Path.Combine(outDir, Path.GetFileNameWithoutExtension(imagePath) + "_preprocessed.png");

   if (shouldCrop)
      PerspectiveDeskewCommand deskew = new PerspectiveDeskewCommand(); // detect the document, then adjust it to a rectangular space

   AutoColorLevelCommand level = new AutoColorLevelCommand(); // adjust color levels to try and improve visual fidelity
   level.Type = AutoColorLevelCommandType.Intensity;

   StretchIntensityCommand stretchIntensity = new StretchIntensityCommand(); // stretch the image's intensity levels

   if (image.BitsPerPixel != 48 && image.BitsPerPixel != 64)
      // some of the following commands only support 48- or 64-bit images
      ColorResolutionCommand resolution = new ColorResolutionCommand();
      resolution.BitsPerPixel = 48;
      resolution.Mode = ColorResolutionCommandMode.InPlace; // running in-place allows us to avoid creating another image for this conversion

   SelectiveColorCommand selectiveColor = new SelectiveColorCommand();
   selectiveColor.ColorsData[(int)SelectiveCommandColorTypes.White].Black = -100; // make white parts of the image much lighter
   selectiveColor.ColorsData[(int)SelectiveCommandColorTypes.Black].Black = 85; // make dark parts darker
   selectiveColor.ColorsData[(int)SelectiveCommandColorTypes.Cyan].Cyan = 15; // make cyan, magenta, and yellow moderately lighter
   selectiveColor.ColorsData[(int)SelectiveCommandColorTypes.Magenta].Magenta = 15;
   selectiveColor.ColorsData[(int)SelectiveCommandColorTypes.Yellow].Yellow = 15;

   ColorIntensityBalanceCommand balance = new ColorIntensityBalanceCommand();
   balance.Shadows = new ColorIntensityBalanceCommandData()
      Red = 95, // we're going to try and lighten some of the shadows to make for a smoother background
      Green = 95,
      Blue = 95
   balance.Luminance = false; // we don't need to preserve the luminance of the original image

   codecs.Save(image, outFile, RasterImageFormat.Png, 0); // finally, save the modified image
   Console.WriteLine($"Saved to {outFile}.");

Here are some sample results from the snippet above:

Here's an example using a color image:

Finally, here's an example project that incorporates the snippet above. The program reads the files in a given directory, pre-processes them as images, and saves the final images in a subdirectory (cropping optional).
File Attachment(s):
CameraDocCleanup.zip (3kb) downloaded 31 time(s).
Joe Kerrigan
LEAD Technologies, Inc.
leadtools header

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-2021, Yet Another Forum.NET
This page was generated in 0.081 seconds.