如何修复Core Image的CILanczosScaleTransform过滤器边框伪像? [英] How can I fix a Core Image's CILanczosScaleTransform filter border artifact?

查看:217
本文介绍了如何修复Core Image的CILanczosScaleTransform过滤器边框伪像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为iOS实现图像缩小算法。读完Core Images的 CILanczosScaleTransform 非常适合它之后,我通过以下方式实现了它:

I want to implement an image downscaling algorithm for iOS. After reading that Core Images's CILanczosScaleTransform was a great fit for it, I implemented it the following way:

public func resizeImage(_ image: UIImage, targetWidth: CGFloat) -> UIImage? {
    assert(targetWidth > 0.0)

    let scale = Double(targetWidth) / Double(image.size.width)

    guard let ciImage = CIImage(image: image) else {
        fatalError("Couldn't create CIImage from image in input")
    }

    guard let filter = CIFilter(name: "CILanczosScaleTransform") else {
        fatalError("The filter CILanczosScaleTransform is unavailable on this device.")
    }

    filter.setValue(ciImage, forKey: kCIInputImageKey)
    filter.setValue(scale, forKey: kCIInputScaleKey)

    guard let result = filter.outputImage else {
        fatalError("No output on filter.")
    }

    guard let cgImage = context.createCGImage(result, from: result.extent) else {
        fatalError("Couldn't create CG Image")
    }

    return UIImage(cgImage: cgImage)
}

它的效果很好,但是我得到的是经典的边框伪像该算法的像素邻域基础。我在Apple的文档中找不到与此相关的任何内容。 是否有比渲染更大图像然后裁剪边框来解决此问题更聪明的东西?

It works well but I get a classic border artifact probably due to the pixel-neighborhood base of the algorithm. I couldn't find anything in Apple's doc about this. Is there something smarter than rendering a bigger image and then crop the border to solve this issue?

推荐答案

您可以使用 imageByClampingToExtent


调用此方法...通过从原始图像的边缘重复
像素颜色来创建无限范围的图像。

Calling this method ... creates an image of infinite extent by repeating pixel colors from the edges of the original image.

您可以像这样使用它:

...
guard let ciImage = CIImage(image: image)?.clampedToExtent() else {
    fatalError("Couldn't create CIImage from image in input")
}

在此处查看更多信息: Apple Docted forextent

See more information here: Apple Doc for clampedtoextent

这篇关于如何修复Core Image的CILanczosScaleTransform过滤器边框伪像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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