简体   繁体   中英

How can I convert an UIImage to grayscale in Swift using CIFilter?

I am building a scanner component for an iOS app so far I have the result image cropped and in the correct perspective. Now I need to turn the color image into Black and white "Scanned" document.

I tried to use - "CIPhotoEffectNoir" but it more grayscale then totally black and white. I wish to get a full contrast image with 100% black and 100% white.

How can I achieve that? Thanks

You can use CIColorControls and set Contrast Key kCIInputContrastKey to increase the black/white contrast as follow:

Xcode 9 • Swift 4

extension String {
    static let colorControls = "CIColorControls"
}

extension UIImage {
    var coreImage: CIImage? { return CIImage(image: self) }
}

extension CIImage {
    var uiImage: UIImage? { return UIImage(ciImage: self) }
    func applying(contrast value: NSNumber) -> CIImage? {
        return applyingFilter(.colorControls, parameters: [kCIInputContrastKey: value])
    }
    func renderedImage() -> UIImage? {
        guard let image = uiImage else { return nil }
        return UIGraphicsImageRenderer(size: image.size,
                                       format: image.imageRendererFormat).image { _ in
            image.draw(in: CGRect(origin: .zero, size: image.size))
        }
    }
}

let url = URL(string: "https://i.stack.imgur.com/Xs4RX.jpg")!
do {
    if let coreImage = UIImage(data: try Data(contentsOf: url))?.coreImage,
        let increasedContrast = coreImage.applying(contrast: 1.5) {
        imageView.image = increasedContrast.uiImage
        // if you need to convert your image to data (JPEG/PNG) you would need to render the ciimage using renderedImage method on CIImage   
    }
} catch {
    print(error)
}

To convert from colors to grayscale you can set the Saturation Key kCIInputSaturationKey to zero:

extension CIImage {
    func applying(saturation value: NSNumber) -> CIImage? {
        return applyingFilter(.colorControls, parameters: [kCIInputSaturationKey: value])
    }
    var grayscale: CIImage? { return applying(saturation: 0) }
}

let url = URL(string: "https://i.stack.imgur.com/Xs4RX.jpg")!
do {
    if let coreImage = UIImage(data: try Data(contentsOf: url))?.coreImage, 
        let grayscale = coreImage.grayscale {
        // use  grayscale image here
        imageView.image = grayscale.uiImage
    }
} catch { 
    print(error) 
}
  1. Desaturate will convert your image to grayscale
  2. Increasing the contrast will push those grays out to the extremes, ie black and white.

You can CIColorControls :

let ciImage = CIImage(image: image)!
let blackAndWhiteImage = ciImage.applyingFilter("CIColorControls", withInputParameters: ["inputSaturation": 0, "inputContrast": 5])

Original:

原版的

With inputContrast = 1 (default):

inputContrast = 1

With inputContrast = 5 :

inputContrast = 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM