Skip to main content

Image Processing And Filters | Android Document Scanner

The ImageProcessor class applies a chain of operations to the input image. The operations are applied according to the given order and applied step-by-step.

To make use of the ImageProcessor features for Page objects, use a PageProcessor instance. An example of this approach may be found here

Supported types of operations:#

  • CropOperation - crop the image according to a given polygon
  • RotateOperation - rotate image by a given angle. 0, 90, 180, 270 are supported
  • ResizeOperation - resize the image with a given max size
  • FilterOperation - apply the given 'ImageFilterType'
  • TuneOperation - apply the given adjustable 'ImageFilterTuneType'

Cropping#

To crop the document image from the original image based on detected polygon, use one of the detect(..) methods of the ContourDetector and then apply the Crop operation of ImageProcessor:

contourDetector.detect(originalBitmap)val documentBitmap = imageProcessor.process(originalBitmap, listOf(CropOperation(contourDetector.polygonF)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to process the document image from. This input image will not be modified.

The result documentBitmap is the result document image.

Rotation#

To rotate the document image apply the Rotation operation of ImageProcessor:

val documentBitmap = imageProcessor.process(originalBitmap, listOf(RotateOperation(degrees)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to process the document image from. This input image will not be modified.
  • degrees - The angle to rotate the image. Only 0, 90, 180, 270 are supported.

The result documentBitmap is the result document image.

Resize#

To resize the document image use the Resize operation of ImageProcessor:

val documentBitmap = imageProcessor.process(originalBitmap, listOf(ResizeOperation(maxSize)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to process the document image from. This input image will not be modified.
  • maxSize - Maximum size of the image for width and height.

The result documentBitmap is the result document image.

Filter#

To apply a Base filter to the document image use the FilterOperation of ImageProcessor:

val documentBitmap = imageProcessor.process(originalBitmap, listOf(FilterOperation(filter)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to process the document image from. This input image will not be modified.
  • filter - the selected base filter.

The result documentBitmap is the result document image.

Constants for supported image filters#

  • ImageFilterType.NONE - Do not apply an image filter, keep original colors.
  • ImageFilterType.COLOR_ENHANCED - Optimizes colors, contrast and brightness.
  • ImageFilterType.GRAYSCALE - Grayscale filter.
  • ImageFilterType.BINARIZED - Standard binarization filter with contrast optimization. Creates an 8-bit grayscale image with mostly black or white pixels.
  • ImageFilterType.COLOR_DOCUMENT - MagicColor filter. Fixes white-balance and cleans up the background.
  • ImageFilterType.PURE_BINARIZED - A filter for binarizing an image. Creates an image with pixel values set to either pure black or pure white.
  • ImageFilterType.BLACK_AND_WHITE - Black and white filter with background cleaning. Creates an 8-bit grayscale image with mostly black or white pixels.
  • ImageFilterType.BACKGROUND_CLEAN - Cleans up the background and tries to preserve photos within the image.
  • ImageFilterType.OTSU_BINARIZATION - A filter for black and white conversion using OTSU binarization.
  • ImageFilterType.DEEP_BINARIZATION - A filter for black and white conversion primary used for low-contrast documents.
  • ImageFilterType.EDGE_HIGHLIGHT - A filter that enhances edges in low-contrast documents.
  • ImageFilterType.LOW_LIGHT_BINARIZATION - Binarization filter primarily intended to use on low-contrast documents with hard shadows.
  • ImageFilterType.LOW_LIGHT_BINARIZATION_2 - Binarization filter primarily intended to use on low-contrast documents with hard shadows.
  • ImageFilterType.SENSITIVE_BINARIZATION - Binarization filter for poor quality printed papers. See important info about this type below!

Using SENSITIVE_BINARIZATION filter#

If you plan to use this filter or add it to the supported list in your app you need to make two additional steps:

  • add implementation "io.scanbot:sdk-ml-imageprocessor-assets:$scanbotSdkVersion" dependency to your build.gradle file
  • make additional call during SDK initialization: .imageProcessorType(ImageProcessor.Type.ML_BASED) (see SDK Initialization chapter)

Adjustable tunes#

To apply an adjustable tune for the document image use the Tune operation of ImageProcessor:

val documentBitmap = imageProcessor.process(originalBitmap, listOf(TuneOperation(tuneType, value)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to apply operations of the image processor to. This input image will not be modified.
  • tuneType - Type of the tune.
  • value - A value of adjustable tune to apply.

The result documentBitmap is the result document image.

Constants for supported adjustable tunes:#

  • ImageFilterTuneType.BRIGHTNESS
  • ImageFilterTuneType.CONTRAST
  • ImageFilterTuneType.SATURATION_LOWER_BALANCE
  • ImageFilterTuneType.SATURATION_UPPER_BALANCE
  • ImageFilterTuneType.VIBRANCE
  • ImageFilterTuneType.BLACK_POINT
  • ImageFilterTuneType.WHITE_POINT
  • ImageFilterTuneType.SATURATION
  • ImageFilterTuneType.TINT
  • ImageFilterTuneType.TEMPERATURE
  • ImageFilterTuneType.SHADOW
  • ImageFilterTuneType.HIGHLIGHTS
  • ImageFilterTuneType.SPECIAL_CONTRAST
  • ImageFilterTuneType.WHITE_POINT_ZERO_BASED
  • ImageFilterTuneType.BLACK_POINT_ZERO_BASED
  • ImageFilterTuneType.CONTRAST_ZERO_BASED
  • ImageFilterTuneType.COMBINED_WHITE_BLACK_POINT
  • ImageFilterTuneType.COMBINED_SATURATION_ZERO_BASED

Chains of operations#

It is possible to apply different operations using ImageProcessor

val documentBitmap = imageProcessor.process(originalBitmap, listOf(FilterOperation(filter), TuneOperation(tuneType, value)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to apply operations of the image processor to. This input image will not be modified.
  • filter - Filter in the chain.
  • tuneType - Type of the tune.
  • value - A value of adjustable tune to apply.

The result documentBitmap is the result document image.

Applying the most relevant filter#

To predict the most relevant filter for a particular image, it is possible to use the FilterPredictor object from our SDK.

There is an example of how to use the filter predictor:

val filterPredictor = ScanbotSDK(context).createFilterPredictor()val suggestedFilter = filterPredictor.predict(originalBitmap)val documentBitmap = imageProcessor.process(originalBitmap, listOf(FilterOperation(suggestedFilter)))

The parameters are:

  • originalBitmap - The original Bitmap image as input to apply operations of the image processor to. This input image will not be modified.

The suggestedFilter is a filter which is recommended to apply to originalBitmap to make it more legible by OCR.

The result documentBitmap is the result document image.