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.

Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Invert colors
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();
// Save preprocessed image to file for debugging purposes
using(MemoryStream ms = recognitionEngine.PreprocessImage("source.png", filters))
	using(FileStream fs = new FileStream("result.png", FileMode.Create, FileAccess.Write))
// Append preprocessing filters to recognition settings
Aspose.OCR.RecognitionSettings recognitionSettings = new Aspose.OCR.RecognitionSettings();
recognitionSettings.PreprocessingFilters = filters;
// Recognize image
Aspose.OCR.RecognitionResult result = recognitionEngine.RecognizeImage("source.png", recognitionSettings);
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 Aspose.Drawing.Rectangle object. If the region is omitted, the filter is applied to the entire image.

Aspose.Drawing.Rectangle rectangle = new Aspose.Drawing.Rectangle(5, 161, 340, 113);
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();

Usage scenarios

Automatic color inversion is recommended for the following images:

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