Color inversion

When working with white (or light) text on a black (or other dark) background, recognition accuracy may suffer. It can be greatly improved by reversing the colors of the image so that light areas appear dark and dark areas appear light. In addition, other colors are also swapped: red becomes cyan, green becomes magenta, blue becomes yellow, and so on.

Aspose.OCR provides the automated preprocessing filter that inverts colors in the image before proceeding to recognition.

Inverting image colors

To automatically invert colors in an image before recognition, run the image through Invert preprocessing filter.

AsposeOCR api = new AsposeOCR();
// Invert colors
PreprocessingFilter filters = new PreprocessingFilter();
filters.add(PreprocessingFilter.Invert());
// Prepare batch
OcrInput images = new OcrInput(InputType.SingleImage, filters);
images.add("image.png");
// Save processed images to the folder
ImageProcessing.Save(images, "C:\\images");
White text on dark background Inverted image

Image regions preprocessing

You can invert certain areas of the image, such as black on a white headers, while leaving the rest of the content unchanged.

To apply a filter to an area, specify its top left corner along with width and height as Rectangle object. If the region is omitted, the filter is applied to the entire image.

Rectangle rectangle = new Rectangle(5, 161, 340, 340);
PreprocessingFilter filters = new PreprocessingFilter();
filters.add(PreprocessingFilter.Invert(rectangle));

Usage scenarios

Automatic color inversion is recommended for the following images:

  • White text on black background.
  • Advertisements.
  • Business cards.
  • Screenshots.