How can I fix a Core Image's CILanczosScaleTransform filter border artifact?

时光毁灭记忆、已成空白 提交于 2019-12-04 05:06:57

问题


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)
}

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?


回答1:


You can use imageByClampingToExtent.

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

You could use it like this:

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

See more information here: Apple Doc for clampedtoextent



来源:https://stackoverflow.com/questions/49147109/how-can-i-fix-a-core-images-cilanczosscaletransform-filter-border-artifact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!