在 SceneKit 下可靠地访问和修改捕获的相机帧

Reliable access and modify captured camera frames under SceneKit

我尝试向 ARSCNView 的相机图像添加黑白滤镜,然后在其上渲染彩色 AR 对象。

我已经快完成了,在 - (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time

的开头添加了以下代码
CVPixelBufferRef bg=self.sceneView.session.currentFrame.capturedImage;

if(bg){
    char* k1 = CVPixelBufferGetBaseAddressOfPlane(bg, 1);
    if(k1){
        size_t x1 = CVPixelBufferGetWidthOfPlane(bg, 1);
        size_t y1 = CVPixelBufferGetHeightOfPlane(bg, 1);
        memset(k1, 128, x1*y1*2);
    }
}

这在移动设备上运行得非常快,但问题是:有时会显示彩色框。 我已经检查并执行了我的过滤代码,但我认为为时已晚,SceneKit 的管道已经处理了相机输入。

早点调用代码会有所帮助,但 updateAtTime 是可以逐帧代码添加自定义帧的最早点。

获取有关帧捕获的通知可能会有所帮助,但看起来整个 AVCapturesession is unaccessible.

Metal ARKit 示例展示了如何将相机图像转换为 RGB,这是我要进行过滤的地方,但在使用 SceneKit 时该着色器是隐藏的。

我试过了this possible answer但是太慢了。

那么我怎样才能克服丢帧问题并将摄像机信号可靠地转换为黑白?

这里是这个问题的关键:

session:didUpdateFrame:

Provides a newly captured camera image and accompanying AR information to the delegate.

所以刚刚移动CVPixelBufferRef操作,图像过滤代码来自

- (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time

- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame

确保设置 self.sceneView.session.delegate = self 以调用此委托。