【问题标题】:How to adjust a color image like a scanned image如何像扫描图像一样调整彩色图像
【发布时间】:2018-03-05 23:03:00
【问题描述】:

我想制作一个彩色普通图像到扫描图像的效果。

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

第一张图片:

第二张图片:

我想使用 CoreImage 和 CIFilter 来执行此操作。除了对比度和亮度。我认为应该像Photoshop一样调整水平。但如何调整呢?还是其他方法?

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

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

【问题讨论】:

  • 你试过什么?当你这样做时发生了什么?
  • 我已经添加了一些有问题的解释。@matt

标签: ios objective-c swift core-image cifilter


【解决方案1】:

试试下面的代码。

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,请尝试以下链接和答案。

Get UIImage from functionConvert UIImage to grayscale keeping image quality

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

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


【讨论】:

  • 非常感谢,我去试试。
  • 让我知道代码有效,并确保您接受答案。
  • 感谢您的代码,它适用于这张图片,所以我接受这个答案。但如果使用其他图片,可能效果不佳。我正在努力开发一个通用的解决方案,以便它可以适用于大多数此类图片。自动制作扫描图片。因此,如果您有适当的解决方案,请帮助我。非常感谢。
  • 查看这篇帖子stackoverflow.com/questions/43011332/…@Tolecen。
  • @Tolecen 我相信您可以轻松实现 UISlider 手动调整饱和度、亮度和对比度值以获得最佳效果。希望这有助于...
【解决方案2】:

我相信要手动实现类似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?

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多