我想对扫描图像制作彩色普通图像的效果。
如下两张图,第一张是原图,第二张是调整后的图片,效果应该和第二张一样,页面后面的文字也应该消失:
第一张图片:
第二张图片:
我想用 CoreImage 和 CIFilter 来做这个。除了对比度和亮度。我认为应该像 photoshop 一样调整水平。但如何调整呢?或其他方法?
我试过在photoshop中调整过,好像用photoshop的level功能可以达到这个效果,数值如下图:
但我不知道如何使用 CIFilter 或其他方法做到这一点?我不想导入第三方框架或来源。因为iOS 11 Notes APP可以达到这个效果,我觉得系统的方法和框架可以做到。
最佳答案
试试下面的代码。
func getScannedImage(inputImage: UIImage) -> UIImage? {
let openGLContext = EAGLContext(api: .openGLES2)
let context = CIContext(eaglContext: openGLContext!)
let filter = CIFilter(name: "CIColorControls")
let coreImage = CIImage(image: inputImage)
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
,请尝试以下链接和答案。
Get UIImage from function和 Convert UIImage to grayscale keeping image quality
Apple 文档中的核心图像过滤器引用:https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html
注意:对于更具体的要求,您需要创建自己的自定义过滤器。以下链接可能会有所帮助 https://spin.atomicobject.com/2016/10/20/ios-image-filters-in-swift/
关于ios - 如何像扫描图像一样调整彩色图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46397367/