Instruction 02

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

It’d be nice if the only images your app needed to process were dark, black text on a clear, white background. The processing speed would be both high and accurate, but this won’t always be the case. Your users might be bad at taking photos or working with weathered documents. Some fonts are notoriously hard for OCR systems. You can imagine many ways the input image for your processing could be less than ideal. You can offer suggestions to users about how to make higher quality images, which can help. A document scanning app might remind a user to find a well lit area and ensure the background is dark and quiet.

Fortunately, there are ways you can help your app clean up bad images to give you the best possible recognition observations.

Filtering the Input

Apple provides a large library of image-manipulation filters in the CoreImage framework. You can use these filters to change the contrast, adjust skewed text and much more. A deep dive into CoreImage filters is way beyond this lesson’s scope. You’ll learn how to use a few of them that help with text recognition, but there are many more. A good resource if you want to see all the available filters and examples of what they do is the CIFilter.io site. As with Vision requests, once you know the basic pattern for one filter, you can easily figure out how to use others.

import CIImage

let ciImage = CIImage(cgImage: inputImage.cgImage!)
let filter = CIFilter(name: "CIColorControls")!
filter.setValue(ciImage, forKey: kCIInputImageKey)
filter.setValue(1.0, forKey: kCIInputContrastKey)
let outputImage = filter.outputImage
let recognitionRequestHandler = VNImageRequestHandler(ciImage: outputImage, 
  options: [:])
import CoreImage.CIFilterBuiltins

let ciImage = CIImage(cgImage: inputImage.cgImage!)
let filter = CIFilter.colorControls()
filter.contrast = 1.2
filter.saturation = 1.0
let outputImage = filter.outputImage
See forum comments
Download course materials from Github
Previous: Instruction 01 Next: Demo