【问题标题】:Depth map from AVDepthData different from HEIC file depth data in Photoshop来自 AVDepthData 的深度图与 Photoshop 中的 HEIC 文件深度数据不同
【发布时间】:2023-03-08 21:20:02
【问题描述】:

我正在使用以下代码提取深度图(按照 Apple 自己的示例):

- (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;
}

我是这样称呼的:

[[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) {

}];

结果如下:这是一张低质量的图片(并且已经旋转,但我们暂时先把方向放在一边):

然后我已经传输了原始 HEIC 文件,在 Photoshop 中打开,进入 Channels,然后选择深度图如下:

结果如下:

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

【问题讨论】:

    标签: ios image-processing ios12 avdepthdata


    【解决方案1】:

    我找到了问题所在。实际上,它隐藏在视线之外。从+[AVDepthData depthDataFromDictionaryRepresentation:error:] 方法获得的内容返回disparity 数据。我已使用以下代码将其转换为 depth

    if(depthData.depthDataType != kCVPixelFormatType_DepthFloat32){
        depthData = [depthData depthDataByConvertingToDepthDataType:kCVPixelFormatType_DepthFloat32];
    }
    

    (没试过,但16位深度,kCVPixelFormatType_DepthFloat16,应该也可以)

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-10
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多