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

问题描述:

我想对彩色正常图像生成扫描图像的效果。如何像扫描图像一样调整彩色图像

像下面两张照片,第一图像是原始图像,第二图像是调整后的图像,效果应该喜欢第二图像,在页面背面的文字也应该消失:

第一张图片:

original image

第二图像:

effected image

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

我试图在Photoshop中调整它,它似乎是使用Photoshop的水平仪功能可以达到这样的效果,该值显示在下面的图片:

enter image description here

,但我不知道该怎么办这使用CIFilter或其他方法?我不想导入第三方框架或来源。因为iOS 11 Notes APP可以达到这个效果,我认为系统的方法和框架可以做到这一点。

+0

你有什么试过的?你做了什么事? – matt

+0

我已经添加了一些问题解释。@ matt – Tolecen

请尝试以下代码。

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 
} 

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

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

输出:(输出真实设备)

enter image description here


有关Core Image Filter尝试下面的链接和答案更多的了解。

Get UIImage from functionConvert UIImage to grayscale keeping image quality

核心图像过滤器,从苹果文档参考:https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

注:有关更具体的要求,你需要创建自己的自定义过滤器。以下链接可能有帮助https://spin.atomicobject.com/2016/10/20/ios-image-filters-in-swift/


+0

非常感谢,我会尝试一下。 – Tolecen

+0

让我知道代码的作品,并确保你接受答案。 – Joe

+0

感谢您的代码,它适用于此图片,所以我接受此答案。但是如果使用其他图片,它可能无法正常工作。我正在努力开发一种通用的解决方案,以便它可以用于大多数此类图片。自动制作扫描的照片。所以如果你有适当的解决方案,请帮助我。非常感谢。 – Tolecen

我相信,在以手工实现的Photoshop类似水平调整功能,你需要了解一些关于图像处理领域的核心知识。

您可能需要阅读本Core Image Programming Guide为出发点:

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

或者,你可能只是最终使用第三方框架,尽管你已经说过,你不不想使用第三方库。如果是这样的话,我想推荐GPUImage2以防万一

https://github.com/BradLarson/GPUImage2

最后,我发现这是一个类似的问题,所以你可能想看看:

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

祝你好运。