Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - Reliable access and modify captured camera frames under SceneKit

I try to add a B&W filter to the camera images of an ARSCNView, then render colored AR objects over it.

enter image description here

I'am almost there with the following code added to the beginning of - (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);
    }
}

This works really fast on mobile, but here's the thing: sometimes a colored frame is displayed. I've checked and my filtering code is executed but I assume it's too late, SceneKit's pipeline already processed camera input.

Calling the code earlier would help, but updateAtTime is the earliest point one can add custom frame by frame code.

Getting notifications on frame captures might help, but looks like the whole AVCapturesession is unaccessible.

The Metal ARKit example shows how to convert the camera image to RGB and that is the place where I would do filtering, but that shader is hidden when using SceneKit.

I've tried this possible answer but it's way too slow.

So how can I overcome the frame misses and convert the camera feed reliably to BW?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's the key for this problem:

session:didUpdateFrame:

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

So just moved CVPixelBufferRef manipulation, the image filtering code from

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

to

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

Made sure to set self.sceneView.session.delegate = self to have this delegate called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...