Median filter

Photos taken in low light conditions can have a lot of digital noise. Noise can also show up in highly compressed JPEG images in form of compression artifacts. This noise can mislead OCR algorithms and prevent other processing filters from working properly.

Aspose.OCR for Python via .NET provides an alternative method for removing noise from an image at the cost of some detail, called the median filter. This makes the image a little blurry while preserving the edges of high-contrast objects such as letters. The results can be further improved with the auto-contrast or binarization processing filters.

Applying the median filter

To smooth out noise in an image, run the image through median processing filter.

# Instantiate Aspose.OCR API
api = AsposeOcr()
# Initialize image processing
filters = PreprocessingFilter()
filters.add(PreprocessingFilter.median())
# Add image to the recognition batch and apply processing filter
input = OcrInput(InputType.SINGLE_IMAGE, filters)
input.add("source.png")
# Save processed image to the "result" folder
ImageProcessing.save(input, "result")
# Recognize the image
result = api.recognize(input)
# Print recognition result
print(result[0].recognition_text)
Noisy image Smooth noise

Usage scenarios

Median filter is recommended for the following images:

  • Photos that were taken in low light conditions.
  • Poor quality printouts.
  • Highly compressed / low quality JPEG’s.