Ios 从PHImageManager获取视差或深度数据

Ios 从PHImageManager获取视差或深度数据,ios,objective-c,image,Ios,Objective C,Image,我试图从相位集中得到视差或深度贴图。我找到了一个示例,其中地图是从加载了Phimager的图像中获取的,并实现了它: - (AVDepthData*)getDepthDataFromSource:(CGImageSourceRef)source { NSDictionary* depthData = CFBridgingRelease(CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTy

我试图从相位集中得到视差或深度贴图。我找到了一个示例,其中地图是从加载了Phimager的图像中获取的,并实现了它:

- (AVDepthData*)getDepthDataFromSource:(CGImageSourceRef)source 
{
    NSDictionary* depthData = CFBridgingRelease(CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDepth));
    if (!depthData){
        depthData = CFBridgingRelease(CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDisparity));
    }
    if (!depthDat)
        return nil; //code returns here 

    NSError* creationError = nil;
    AVDepthData* data = [AVDepthData depthDataFromDictionaryRepresentation:depthData
                                                                    error:&creationError];
    return data;
}

//from a image
[[PHImageManager defaultManager]  requestImageForAsset:asset
                            targetSize:size
                            contentMode:contentMode
                            options:requestOptions
                            resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                            CGImageRef im = image.CGImage;
                            CGImageSourceRef source = CGImageSourceCreateWithDataProvider(CGImageGetDataProvider(im), (CFDictionaryRef)@{});
                            AVDepthData* data = [self getDepthDataFromSource:source];//data is nil
                            if (source != nil)
                            {
                                CFRelease(source);
                            }
                    }];

//from a data
    PHImageRequestOptions* imageDataRequestOptions = [[PHImageRequestOptions alloc] init];
    imageDataRequestOptions.networkAccessAllowed = YES;
    imageDataRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    PHImageRequestID requestId = [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                                                options:imageDataRequestOptions
                                                                            resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                                                                                CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, (CFDictionaryRef)@{});
                                                                                AVDepthData* data = [self getDepthDataFromSource:source];//data is nil
                                                                                if (source != nil)
                                                                                {
                                                                                    CFRelease(source);
                                                                                }
                                                                            }
                                    ];

//from full resolution image
__block FADisparityDataReader* selfStong = self;
PHContentEditingInputRequestOptions* options = [[PHContentEditingInputRequestOptions alloc] init];
options.networkAccessAllowed = YES;
[asset requestContentEditingInputWithOptions:options
                                                        completionHandler:^(PHContentEditingInput * _Nullable contentEditingInput, NSDictionary * _Nonnull info) {
    NSURL* fullSizePath = [contentEditingInput fullSizeImageURL];
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)fullSizePath, (CFDictionaryRef)@{});
    @onExit{
        CFRelease(source);
    };
    AVDepthData* data = [self readDepthDataFromSource:source]; //data is not nil
    complition(data);
}]; 
我只在
requestContentEditingInputWithOptions
块中获取深度数据,但它太长,我相信我可以从PHImageManager图像中获取深度贴图。 如何从PHImageManager获取数据

2017年WWDC会议有一些指导,但没有完整的代码示例。 您可以使用Apple示例应用程序“”测试访问深度数据的方法

请参阅我修改的PhotoBrowse,它加载深度和视差图像。它使用加速框架vDSP向量函数将视差标准化为0..1范围

关键点是PHAsset requestContentEditingInput完成处理程序用于获取辅助深度(或视差)ciImage

会话演示讨论了扩展和规范化,但没有发布任何示例代码或应用程序。请参阅我修改的PhotoBrowse应用程序,以规范化CVPixelBuffer深度数据

另外,请确保使用iPhone肖像模式拍摄具有深度的照片。我个人照片库中的几乎所有图像都没有深度数据


你的问题已经有一段时间了,所以你可能已经解决了这个问题。

偶然发现了这个。。。这不是因为你在if(!depthDat){line中检查depthDat而不是depthData吗?从iOS 14.4.2开始,上面的PhotoBrowse示例深度代码不再读取auxImage for depth..某些地方发生了变化!我计划很快回到这个问题上
    @IBAction func showDepthBtn(_ sender: UIBarButtonItem) {
        // show the depth data from portrait mode from
        // iPhone 7 Plus and later camera
       requestDepthMap(selectedAsset: asset)
    }

    func requestDepthMap(selectedAsset: PHAsset)  {
       // may not have depthData in many cases
       // PH completionHandler may be invoked multiple times
       var auxImage: CIImage?
       let options = PHContentEditingInputRequestOptions()

       selectedAsset.requestContentEditingInput(with: options, completionHandler: { input, info in
           guard let input = input
               else { NSLog ("contentEditingInput not loaded")
                    return
               }
            auxImage = CIImage(contentsOf: input.fullSizeImageURL!, options: [CIImageOption.auxiliaryDepth: true])

            if auxImage != nil {
                let uiImage = UIImage(ciImage: auxImage! )
                self.imageView.image = uiImage
            }

       } )
   }