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 : 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:

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))
      Directory.CreateDirectory(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
      deskew.Run(image);
   }

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

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

   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
      resolution.Run(image);
   }

   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;
   selectiveColor.Run(image);

   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
   balance.Run(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:
example.jpg

Here's an example using a color image:
example_color.jpg

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 33 time(s).
Joe Kerrigan
Intern
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-2024, Yet Another Forum.NET
This page was generated in 0.082 seconds.