swift - 如何在 CIImage 上应用 CIEdgePreserveUpsampleFilter?

标签 swift ciimage

我正在关注 this WWDC 讲座。

在讲座中,他提到了一个名为“CIEdgePreserveUpsampleFilter”的过滤器,它使边缘更加保留和上采样。 我正在尝试将此应用到我的 CIImage 上,但我得到了图像的未初始化结果并崩溃。

这是我正在使用的代码以及我如何尝试应用过滤器的示例(这显然是错误的)。我只是找不到任何应用此滤镜的相关说明,我只知道我希望它的结果出现在我的图像上。 我会在我尝试应用过滤器的位置旁边评论,以及我这样做时会发生什么。

func createMask(for depthImage: CIImage, withFocus focus: CGFloat, andScale scale: CGFloat, andSlope slope: CGFloat = 4.0, andWidth width: CGFloat = 0.1) -> CIImage {

    let s1 = slope
    let s2 = -slope
    let filterWidth =  2 / slope + width
    let b1 = -s1 * (focus - filterWidth / 2)
    let b2 = -s2 * (focus + filterWidth / 2)

    let mask0 = depthImage
        .applyingFilter("CIColorMatrix", withInputParameters: [
            "inputRVector": CIVector(x: s1, y: 0, z: 0, w: 0),
            "inputGVector": CIVector(x: 0, y: s1, z: 0, w: 0),
            "inputBVector": CIVector(x: 0, y: 0, z: s1, w: 0),
            "inputBiasVector": CIVector(x: b1, y: b1, z: b1, w: 0)])
        .applyingFilter("CIColorClamp").applyingFilter("CIEdgePreserveUpsampleFilter") //returns uninitialized image

    let mask1 = depthImage
        .applyingFilter("CIColorMatrix", withInputParameters: [
            "inputRVector": CIVector(x: s2, y: 0, z: 0, w: 0),
            "inputGVector": CIVector(x: 0, y: s2, z: 0, w: 0),
            "inputBVector": CIVector(x: 0, y: 0, z: s2, w: 0),
            "inputBiasVector": CIVector(x: b2, y: b2, z: b2, w: 0)])
        .applyingFilter("CIColorClamp")

    var combinedMask = mask0.applyingFilter("CIEdgePreserveUpsampleFilter", withInputParameters: ["inputBackgroundImage" : mask1]) //complete crash
    if PortraitModel.sharedInstance.filterArea == .front {
        combinedMask = combinedMask.applyingFilter("CIColorInvert")
    }
    let mask = combinedMask.applyingFilter("CIBicubicScaleTransform", withInputParameters: [kCIInputScaleKey: scale])

    return mask
}

最佳答案

我发现的运行时 header 和一些使用代码似乎表明 CIEdgePreserveUpsampleFilter 不采用 inputBackgroundImage 参数,而是采用 inputSmallImage

参见 https://gist.github.com/HarshilShah/ca0e18db01ce250fd308ab5acc99a9d0

关于swift - 如何在 CIImage 上应用 CIEdgePreserveUpsampleFilter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49428776/

相关文章:

swift - 如何使用分段控件动态更改 collectionView 项目大小(如 instagram)

swift - didOutputSampleBuffer 丢帧

ios - SceneKit,SCNMaterial的翻转方向

ios - 将 CIImage 坐标从 CIFaceFeature 转换为 CGRect

ios - 状态恢复和托管对象崩溃

ios - 协议(protocol)和委托(delegate)不起作用..我做错了什么?

ios - 如何在 swift 中为标签栏角标(Badge)计数设置动画

swift - 检测绝对垂直轴上的加速度

ios - "Interior"(如边缘)使用 CIImage 检测?

swift - CIFilter 中的图像不会显示