首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从VNImageOptionCameraIntrinsics设置ARCamera

从VNImageOptionCameraIntrinsics设置ARCamera
EN

Stack Overflow用户
提问于 2018-09-02 20:20:03
回答 1查看 822关注 0票数 2

我正在构建一个将ARKit和CoreML结合在一起的应用程序。我使用以下行将帧传递给VNImageRequestHandler

代码语言:javascript
复制
// the frame of the current Scene
CVPixelBufferRef pixelBuffer = _cameraPreview.session.currentFrame.capturedImage;

NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCVPixelBuffer:pixelBuffer options:requestOptions];

注意requestOptions。它应该包含VNImageOptionCameraIntrinsics字段,该字段将相机的本质传递给CoreML。

在使用ARKit之前,我使用了一个CMSampleBufferRef从相机中获取图像。可以使用以下方法检索和设置指示信息:

代码语言:javascript
复制
CFTypeRef cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil);
requestOptions[VNImageOptionCameraIntrinsics] = (__bridge id)(cameraIntrinsicData);

但是,我现在使用的是ARFrame,但是由于pixelBuffer是旋转的,所以我仍然希望设置正确的本质。

看医生:

https://developer.apple.com/documentation/vision/vnimageoption?language=objc

https://developer.apple.com/documentation/arkit/arcamera/2875730-intrinsics?language=objc

我们可以看到,ARCamera也提供了本质,但是,如何正确地在requestOptions中设置这个值呢?

到目前为止,应该是这样的:

代码语言:javascript
复制
ARCamera *camera = _cameraPreview.session.currentFrame.camera;
NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];
// How to put camera.intrinsics here?
requestOptions[VNImageOptionCameraIntrinsics] = camera.intrinsics;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-02 21:29:20

正如Giovanni在注释中提到的,将UIDeviceOrientation转换为CGImagePropertyOrientation避免了使用VNImageOptionCameraIntrinsics的需要

Utils.m

代码语言:javascript
复制
+(CGImagePropertyOrientation) getOrientation {
    CGImagePropertyOrientation orientation;
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    switch (deviceOrientation) {
        case UIDeviceOrientationPortrait:
            orientation = kCGImagePropertyOrientationRight;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = kCGImagePropertyOrientationLeft;
            break;
        case UIDeviceOrientationLandscapeLeft:
            orientation = kCGImagePropertyOrientationUp;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = kCGImagePropertyOrientationDown;
            break;
        default:
            orientation = kCGImagePropertyOrientationRight;
            break;
    }
    return orientation;
}

ViewController.mm

代码语言:javascript
复制
- (void)captureOutput {
    ARFrame *frame = self.cameraPreview.session.currentFrame;
    CVPixelBufferRef pixelBuffer = frame.capturedImage;

    CGImagePropertyOrientation deviceOrientation = [Utils getOrientation];
    NSMutableDictionary<VNImageOption, id> *requestOptions = [NSMutableDictionary dictionary];

    VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCVPixelBuffer:pixelBuffer orientation:deviceOrientation options:requestOptions];

    [handler performRequests:@[[self request]] error:nil];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52140731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档