如何像扫描图像一样调整彩色图像 [英] How to adjust a color image like a scanned image

查看:93
本文介绍了如何像扫描图像一样调整彩色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使彩色普通图像变成扫描图像。

I want to make a effect that a colored normal image to a scanned image.

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

like 2 pictures below, first image is the original image, second image is the adjusted image, effects should like the second image, the text on back of the page should also disappear:

第一张图片:

第二张图片:

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

I want to use CoreImage and CIFilter to do this. besides contrast and brightness. I think should adjust level like photoshop. but how can adjust that? or other method?

我尝试在photoshop中对其进行调整,看来使用photoshop的级别功能可以达到此效果,其值如下图所示:

I have tried to adjust it in photoshop, it seems that use photoshop's level function can reach this effect, the value shows on picture below:

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

but I don't know how to do this use CIFilter or other methods? I don't want to import third part framework or sources. because iOS 11 Notes APP can reach this effect, I think system's methods and frameworks can do this.

推荐答案

请尝试以下代码。

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
}

您可以像下面这样调用上述函数。

You can call the above func like below.

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

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

Output: (Output from real Device)

要进一步了解核心图像过滤器,请尝试以下链接和答案。

For more understanding about Core Image Filter try below links and answers.

从函数中获取UIImage 转换UIIma ge以保持图像质量为灰度

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

Core Image Filter Reference from 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 /

Note: For more specific requirement you need to create your own custom filter. Following link may helps https://spin.atomicobject.com/2016/10/20/ios-image-filters-in-swift/

这篇关于如何像扫描图像一样调整彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆