How can I get Camera Calibration Data on iOS? aka AVCameraCalibrationData

前端 未结 3 2046
走了就别回头了
走了就别回头了 2020-12-11 10:14

As I understand it, AVCameraCalibrationData is only available over AVCaptureDepthDataOutput. Is that correct?

AVCaptureDepthDataOutput on the other hand is only acce

3条回答
  •  旧时难觅i
    2020-12-11 10:58

    Here is a more complete/updated code example in swift 5 that is put together from previous answers. This gets you the camera intrinsics matrix for an iphone.

    based on:

    • https://stackoverflow.com/a/48159895/67166
    • https://stackoverflow.com/a/48565639/6716
    // session setup
    captureSession = AVCaptureSession()
    
    let captureVideoDataOutput = AVCaptureVideoDataOutput()
    
    captureSession?.addOutput(captureVideoDataOutput)
    
    // enable the flag
    if #available(iOS 11.0, *) {
        captureVideoDataOutput.connection(with: .video)?.isCameraIntrinsicMatrixDeliveryEnabled = true
    } else {
        // ...
    }
    
    // `isCameraIntrinsicMatrixDeliveryEnabled` should be set before this
    captureSession?.startRunning()
    

    and now inside AVCaptureVideoDataOutputSampleBufferDelegate.captureOutput(...)

    if #available(iOS 11.0, *) {
        if let camData = CMGetAttachment(sampleBuffer, key:kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, attachmentModeOut:nil) as? Data {
            let matrix: matrix_float3x3 = camData.withUnsafeBytes { $0.pointee }
            print(matrix)
            // > simd_float3x3(columns: (SIMD3(1599.8231, 0.0, 0.0), SIMD3(0.0, 1599.8231, 0.0), SIMD3(539.5, 959.5, 1.0)))
        }
    } else {
        // ...
    }
    

提交回复
热议问题