Conversion to grayscale
Contents
[
Hide
]
In most cases, color is not needed for recognition and can even mislead OCR algorithms. Grayscale allows images to be processed more efficiently, resulting in less specks, cleaner backgrounds, and crisper text than color images. Converting to grayscale can also improve the results of other processing filters, such as automatic deskewing.
Aspose.OCR provides a method for converting an image to grayscale before proceeding with image processing or OCR.
Grayscale conversion is automatically performed when applying the median filter.
Grayscale conversion
To convert the image to grayscale, run the image through ToGrayscale
processing filter.
Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Convert image to grayscale
Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter filters = new Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter();
filters.Add(Aspose.OCR.Models.PreprocessingFilters.PreprocessingFilter.ToGrayscale());
// Add an image to OcrInput object and apply processing filters
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(Aspose.OCR.InputType.SingleImage, filters);
input.Add("source.png");
// Save processed image to the folder
Aspose.OCR.ImageProcessing.Save(input, @"C:\result");
// Recognize image
List<Aspose.OCR.RecognitionResult> results = recognitionEngine.Recognize(input);
foreach(Aspose.OCR.RecognitionResult result in results)
{
Console.WriteLine(result.RecognitionText);
}
Usage scenarios
Grayscale conversion is recommended for the following images:
- Photos.
- Scanned ID cards and other personal documents.
- Full-color scans.
In addition, grayscale conversion may decrease the original image size.
Improvements in recognition accuracy and quality enhancements will be highly dependent on the original image and should be empirically tested.