据我了解,AVCameraCalibrationData 仅可通过 AVCaptureDepthDataOutput 获得。那是对的吗?
另一方面,AVCaptureDepthDataOutput 只能通过 iPhone X 前置摄像头或 iPhone Plus 后置摄像头访问,还是我弄错了?
我要做的是获取 AVCaptureVideoDataOutput SampleBuffer 的 FOV。特别是,它应该匹配所选的预设(全高清、照片等)。
据我了解,AVCameraCalibrationData 仅可通过 AVCaptureDepthDataOutput 获得。那是对的吗?
另一方面,AVCaptureDepthDataOutput 只能通过 iPhone X 前置摄像头或 iPhone Plus 后置摄像头访问,还是我弄错了?
我要做的是获取 AVCaptureVideoDataOutput SampleBuffer 的 FOV。特别是,它应该匹配所选的预设(全高清、照片等)。
背景:当被问及相机校准时,很多这些堆栈溢出响应都引用了内在数据,包括这篇文章的公认答案,但校准数据通常包括内在数据、外在数据、镜头失真等。所有这些都在此处列出iOS 文档。作者提到他们只是在寻找 FOV,它在样本缓冲区中,而不是在相机校准数据中。所以最终,我认为他的问题得到了回答。但是,如果您发现这个问题正在寻找实际的相机校准数据,这会让您失望。就像答案说的那样,您只能在特定条件下获得校准数据,我将在下面详细介绍。
在我回答其余部分之前,我只想说,如果您正在寻找的只是内在矩阵,那么这里接受的答案很好,它可以比其他这些值通过上述方法。如果您将它用于计算机视觉,这就是我正在使用它的目的,有时这就是所需要的。但是对于非常酷的东西,你会想要它!因此,我将继续解释如何实现这一目标:
我将假设您拥有通用的相机应用程序代码。在该代码中,当拍摄照片时,您可能会调用看起来像这样的 photoOutput 函数:
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {...
输出参数将有一个值,您可以参考以查看是否支持相机校准,称为isCameraCalibrationDataDeliverySupported,例如,要打印出来,请使用以下内容:
print("isCameraCalibrationDataDeliverySupported: \(output.isCameraCalibrationDataDeliverySupported)")
请注意,在我链接到的文档中,它仅在特定情况下受支持:
“只有当 isDualCameraDualPhotoDeliveryEnabled 属性为 true 时,此属性的值才能为 true。要启用相机校准传递,请在照片设置对象中设置 isCameraCalibrationDataDeliveryEnabled 属性。”
所以这很重要,注意这一点以避免不必要的压力。使用实际值进行调试并确保启用了正确的环境。
一切就绪后,您应该从以下位置获取实际的相机校准数据:
photo.cameraCalibrationData
只需拉出该对象即可获取您正在寻找的特定值,例如:
photo.cameraCalibrationData?.extrinsicMatrix
photo.cameraCalibrationData?.intrinsicMatrix
photo.cameraCalibrationData?.lensDistortionCenter
etc.
基本上我在上面链接到的文档中列出的所有内容。
您AVCameraCalibrationData
只能从深度数据输出或照片输出中获取。
但是,如果您只需要 FOV,则只需要该类提供的部分信息——相机内在矩阵——您可以从AVCaptureVideoDataOutput
.
设置cameraIntrinsicMatrixDeliveryEnabled
将AVCaptureConnection
您的相机设备连接到捕获会话。(请注意,您应该先检查cameraIntrinsicMatrixDeliverySupported
;并非所有捕获格式都支持内在函数。)
当视频输出提供样本缓冲区时,请检查每个样本缓冲区的附件以获取kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix
密钥。如中所述CMSampleBuffer.h
(有人应该提交有关将此信息获取到在线文档的雷达CFData
),该附件的值是编码 a matrix_float3x3
,并且该矩阵的 (0,0) 和 (1,1) 元素是水平和以像素为单位的垂直焦距。
这是 swift 5 中更完整/更新的代码示例,由以前的答案汇总而成。这将为您提供 iphone 的相机内在矩阵。
基于:
// 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()
现在在里面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<Float>(1599.8231, 0.0, 0.0), SIMD3<Float>(0.0, 1599.8231, 0.0), SIMD3<Float>(539.5, 959.5, 1.0)))
}
} else {
// ...
}
不是答案,而是...
自从我开始使用代码制作具有深度功能的颤振插件以来已经过去了三周,这是对带我进入工作 PoC 的痛苦试验和错误的快速回顾:
(我为代码质量道歉,这也是我第一次使用objective-c)
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:deviceTypes
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
您可以询问他们的深度能力:
for (AVCaptureDevice *device in devices) {
BOOL depthDataCapable;
if (@available(iOS 11.0, *)) {
AVCaptureDeviceFormat *activeDepthDataFormat = [device activeDepthDataFormat];
depthDataCapable = (activeDepthDataFormat != nil);
NSLog(@" -- %@ supports DepthData: %s", [device localizedName],
} else {
depthDataCapable = false;
}
}
在 iPhone12 上:
-- Front TrueDepth Camera supports DepthData: true
-- Back Dual Wide Camera supports DepthData: true
-- Back Ultra Wide Camera supports DepthData: false
-- Back Camera supports DepthData: false
-- Front Camera supports DepthData: false
ps 从历史上看,前置摄像头的质量往往低于后置摄像头,但对于深度捕捉,您无法击败使用红外投影仪/扫描仪的 TrueDepth 摄像头。
现在您知道哪些相机可以完成这项工作,您需要选择功能强大的相机并启用深度:
(空行是代码省略,这不是一个完整的例子)
// this is in your 'post-select-camera' initialization
_captureSession = [[AVCaptureSession alloc] init];
// cameraName is not the localizedName
_captureDevice = [AVCaptureDevice deviceWithUniqueID:cameraName];
// this is in your camera controller initialization
// enable depth delivery in AVCapturePhotoOutput
_capturePhotoOutput = [AVCapturePhotoOutput new];
[_captureSession addOutput:_capturePhotoOutput];
// BOOL depthDataSupported is a property of the controller
_depthDataSupported = [_capturePhotoOutput isDepthDataDeliverySupported];
if (_depthDataSupported) {
[_capturePhotoOutput setDepthDataDeliveryEnabled:YES];
}
[_captureSession addOutput:_capturePhotoOutput];
// this is in your capture method
// enable depth delivery in AVCapturePhotoSettings
AVCapturePhotoSettings *settings = [AVCapturePhotoSettings photoSettings];
if (@available(iOS 11.0, *) && _depthDataSupported) {
[settings setDepthDataDeliveryEnabled:YES];
}
// Here I use a try/catch because even depth capable and enabled cameras can crash if settings are not correct.
// For example a very high picture resolution seems to throw an exception, and this might be a different limit for different phone models.
// I am sure this information is somewhere I haven't looked yet.
@try {
[_capturePhotoOutput capturePhotoWithSettings:settings delegate:photoDelegate];
} @catch (NSException *e) {
[settings setDepthDataDeliveryEnabled:NO];
[_capturePhotoOutput capturePhotoWithSettings:settings delegate:photoDelegate];
}
// after you took a photo and
// didFinishProcessingPhoto:(AVCapturePhoto *)photo was invoked
AVDepthData *depthData = [photo depthData];
if (depthData != nil) {
AVCameraCalibrationData *calibrationData = [depthData cameraCalibrationData];
CGFloat pixelSize = [calibrationData pixelSize];
matrix_float3x3 intrinsicMatrix = [calibrationData intrinsicMatrix];
CGSize referenceDimensions = [calibrationData intrinsicMatrixReferenceDimensions];
// now do what you need to do - I need to transform that to 16bit, Grayscale, Tiff, and it starts like this...
if (depthData.depthDataType != kCVPixelFormatType_DepthFloat16) {
depthData = [depthData depthDataByConvertingToDepthDataType:kCVPixelFormatType_DepthFloat16];
}
// DON'T FORGET HIT LIKE AND SUBSCRIBE FOR MORE BAD CODE!!! :P
}
苹果实际上在这里有一个不错的设置说明: https ://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/capturing_photos_with_depth
除了 Apple 文档之外,我在其他任何地方都没有看到一个重要说明:
要捕获深度图,您需要首先选择 builtInDualCamera 或 builtInTrueDepthCamera 捕获设备作为会话的视频输入。即使 iOS 设备具有双摄像头或 TrueDepth 摄像头,选择默认的后置或前置摄像头也不会启用深度捕捉。