我可以将 CIFilter 应用到 ARkit 相机源吗? [英] Can I apply a CIFilter to ARkit camera feed?

查看:26
本文介绍了我可以将 CIFilter 应用到 ARkit 相机源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 ARSCNView 中的相机实时蒸汽图像应用模糊效果.我已经检查了 WWDC 视频.他们只提到了Metal的自定义渲染,但我在网上没有找到任何完整的例子.知道怎么做吗?

I'm trying to apply a blur effect to camera live steam image in ARSCNView. I have checked the WWDC videos. They only mentioned the custom rendering with Metal, but I didn't found any complete example on web. Any idea how to do that?

更新 1我试图将过滤器应用于背景.它显示不正确的方向.我该如何解决这个问题?

Updated 1 I have tried to apply a filter to the background. It show incorrect orientation. How can I fix this?

let bg=self.session.currentFrame?.capturedImage

        if(bg != nil){
            let context = CIContext()
            let filter:CIFilter=CIFilter(name:"CIColorInvert")!
            let image:CIImage=CIImage(cvPixelBuffer: bg!)
            filter.setValue(image, forKey: kCIInputImageKey)
            let result=filter.outputImage!
            self.sceneView.scene.background.contents = context.createCGImage(result, from: result.extent)

        }

推荐答案

我找到了一个很好的解决方案,就是在设备方向改变时简单地对 background 属性应用相应的几何变换变化:

I've found a pretty good solution, which is to simply apply a corresponding geometric transform to the background property whenever the device orientation changes:

func session(_ session: ARSession, didUpdate frame: ARFrame) {
    let image = CIImage(cvPixelBuffer: frame.capturedImage)
    filter.setValue(image, forKey: kCIInputImageKey)

    let context = CIContext()
    if let result = filter.outputImage,
        let cgImage = context.createCGImage(result, from: result.extent) {

        sceneView.scene.background.contents = cgImage
        if let transform = currentScreenTransform() {
            sceneView.scene.background.contentsTransform = transform
        }
    }
}

private func currentScreenTransform() -> SCNMatrix4? {
    switch UIDevice.current.orientation {
    case .landscapeLeft:
        return SCNMatrix4Identity
    case .landscapeRight:
        return SCNMatrix4MakeRotation(.pi, 0, 0, 1)
    case .portrait:
        return SCNMatrix4MakeRotation(.pi / 2, 0, 0, 1)
    case .portraitUpsideDown:
        return SCNMatrix4MakeRotation(-.pi / 2, 0, 0, 1)
    default:
        return nil
    }
}

确保首先在 viewDidLoad 方法中调用 UIDevice.current.beginGeneratingDeviceOrientationNotifications().

Make sure you call UIDevice.current.beginGeneratingDeviceOrientationNotifications() in your viewDidLoad method first.

这篇关于我可以将 CIFilter 应用到 ARkit 相机源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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