AVDepthData 的深度图与 Photoshop 中的 HEIC 文件深度数据不同

Can*_*ğlu 4 image-processing ios ios12 avdepthdata

我使用以下代码来提取深度图(按照苹果自己的示例):

- (nullable AVDepthData *)depthDataFromImageData:(nonnull NSData *)imageData orientation:(CGImagePropertyOrientation)orientation {
    AVDepthData *depthData = nil;

    CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
    if (imageSource) {
        NSDictionary *auxDataDictionary = (__bridge NSDictionary *)CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 0, kCGImageAuxiliaryDataTypeDisparity);
        if (auxDataDictionary) {
            depthData = [[AVDepthData depthDataFromDictionaryRepresentation:auxDataDictionary error:NULL] depthDataByApplyingExifOrientation:orientation];
        }

        CFRelease(imageSource);
    }

    return depthData;
}
Run Code Online (Sandbox Code Playgroud)

我将此称为:

[[PHAssetResourceManager defaultManager] requestDataForAssetResource:[PHAssetResource assetResourcesForAsset:asset].firstObject options:nil dataReceivedHandler:^(NSData * _Nonnull data) {
    AVDepthData *depthData = [self depthDataFromImageData:data orientation:[self CGImagePropertyOrientationForUIImageOrientation:pickedUiImageOrientation]];
    CIImage *image = [CIImage imageWithDepthData:depthData];
    UIImage *uiImage = [UIImage imageWithCIImage:image];
    UIGraphicsBeginImageContext(uiImage.size);
    [uiImage drawInRect:CGRectMake(0, 0, uiImage.size.width, uiImage.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *pngData = UIImagePNGRepresentation(newImage);
    UIImage* pngImage = [UIImage imageWithData:pngData];    // rewrap
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);
} completionHandler:^(NSError * _Nullable error) {

}];
Run Code Online (Sandbox Code Playgroud)

结果如下:这是一个低质量的图像(并且已旋转,但我们暂时将方向放在一边):

在此输入图像描述

然后我传输了原始的 HEIC 文件,在 Photoshop 中打开,进入通道,然后选择深度图,如下所示:

在此输入图像描述

结果如下:

在此输入图像描述

它是分辨率/质量更高、方向正确的深度图。为什么代码(实际上是 Apple 自己的代码,位于https://developer.apple.com/documentation/avfoundation/avdepthdata/2881221-depthdatafromdictionaryrepresent?language=objc)会导致质量较低的结果?

Can*_*ğlu 5

我已经找到问题了 事实上,它就隐藏在众目睽睽之下。从该方法获得的+[AVDepthData depthDataFromDictionaryRepresentation:error:]结果返回 视差数据。我使用以下代码将其转换为深度:

if(depthData.depthDataType != kCVPixelFormatType_DepthFloat32){
    depthData = [depthData depthDataByConvertingToDepthDataType:kCVPixelFormatType_DepthFloat32];
}
Run Code Online (Sandbox Code Playgroud)

(还没有尝试过,但是 16 位深度,kCVPixelFormatType_DepthFloat16应该也可以很好地工作)

将视差转换为深度后,图像与 Photoshop 中的图像完全相同。我应该在使用时醒来CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 0, kCGImageAuxiliaryDataTypeDisparity);(注意最后的“视差”),Photoshop 明确表示“深度图”,将视差转换为深度(或者只是以某种方式读取为深度,老实说,我不知道物理编码,也许当我首先复制辅助数据时,iOS 正在将深度转换为视差)。

旁注:我还通过直接从方法创建图像源[PHAsset requestContentEditingInputWithOptions:completionHandler:]并将其传递contentEditingInput.fullSizeImageURL到CGImageSourceCreateWithURL方法来解决方向问题。它负责方向。