如何像扫描图像一样调整彩色图像

How to adjust a color image like a scanned image

我想将彩色普通图像制作成扫描图像的效果。

喜欢下面2张图,第一张是原图,第二张是调整后的图,效果应该和第二张一样,页面背面的文字也应该消失:

第一张图片:

第二张图片:

我想用 CoreImage 和 CIFilter 来做这个。除了对比度和亮度。我认为应该像 photoshop 一样调整水平。但如何调整呢?或其他方法?

我试过在photoshop里调整过,好像用photoshop的level功能可以达到这个效果,数值如下图:

但我不知道如何使用 CIFilter 或其他方法做到这一点?我不想导入第三方框架或来源。因为iOS11 Notes APP可以达到这个效果,我觉得系统的方法和框架可以做到。

我相信,为了手动实现类似 Photoshop 的水平调整功能,您需要了解图像处理领域的一些核心知识。

您可能希望阅读此 Core Image Programming Guide 作为起点:

https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html

或者,您可能只是最终使用第三方框架,尽管您已经声明不想使用第 3 方库。如果是这种情况,我想推荐GPUImage2以防万一

https://github.com/BradLarson/GPUImage2

最后,我发现了一个类似的问题,你可以看看:

How can I map Photoshop's level adjustment to a Core Image filter?

祝你好运。

试试下面的代码。

func getScannedImage(inputImage: UIImage) -> UIImage? {

    let openGLContext = EAGLContext(api: .openGLES2)
    let context = CIContext(eaglContext: openGLContext!)

    let filter = CIFilter(name: "CIColorControls")
    let coreImage = CIImage(image: filterImage.image!)

    filter?.setValue(coreImage, forKey: kCIInputImageKey)
    //Key value are changable according to your need.
    filter?.setValue(7, forKey: kCIInputContrastKey) 
    filter?.setValue(1, forKey: kCIInputSaturationKey) 
    filter?.setValue(1.2, forKey: kCIInputBrightnessKey) 

    if let outputImage = filter?.value(forKey: kCIOutputImageKey) as? CIImage {
    let output = context.createCGImage(outputImage, from: outputImage.extent)
        return UIImage(cgImage: output!)
    }     
    return nil
}

你可以像下面这样调用上面的函数。

filterImage.image = getImage(inputImage: filterImage.image!)

输出:(真实设备的输出)


要进一步了解 Core Image Filter,请尝试以下 link 和答案。

and

来自 Apple Doc 的核心图像过滤器参考:https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

注意:对于更具体的要求,您需要创建自己的自定义过滤器。关注 link 可能会有所帮助 https://spin.atomicobject.com/2016/10/20/ios-image-filters-in-swift/