如何从相机图像中获取深度数据



我正在编写一个应用程序,用于从相机拍摄的照片中获取深度数据和视差数据。我可以获取视差数据,但不能获得它总是返回 nil 的深度数据。我需要获取深度信息并将其另存为 JPG

我已经尝试了下面的代码,用户可以在前置和后置摄像头之间切换并拍照,然后我们拍摄的照片将是过程

import UIKit
import AVFoundation
class ViewController: UIViewController {
    @IBOutlet weak var ImageView: UIView!
    var img:UIImage?
    var rgbImage:UIImage?
    var captureSession: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var backCamera = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
    var frontCamera = AVCaptureDevice.default(.builtInTrueDepthCamera, for: .video, position: .front)
    var capturePhotoOut : AVCapturePhotoOutput?
    override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 10.2, *){
            let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)
            do{
                let input = try  AVCaptureDeviceInput(device: captureDevice!)
                captureSession = AVCaptureSession()
                captureSession?.addInput(input)
                videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
                videoPreviewLayer?.frame = view.layer.bounds
                ImageView.layer.addSublayer(videoPreviewLayer!)
                captureSession?.startRunning()
            }catch{
                print("error")
            }
        }
        capturePhotoOut = AVCapturePhotoOutput()
        capturePhotoOut?.isHighResolutionCaptureEnabled = true
        captureSession?.sessionPreset = .photo
        captureSession?.addOutput(capturePhotoOut!)
        capturePhotoOut!.isDepthDataDeliveryEnabled = capturePhotoOut!.isDepthDataDeliverySupported
        capturePhotoOut!.isPortraitEffectsMatteDeliveryEnabled = capturePhotoOut!.isPortraitEffectsMatteDeliverySupported
    }

    @IBAction func imageCapture(_ sender: Any) {
        guard let capturePhotoOutput = self.capturePhotoOut else {return}
        let photoSettings = AVCapturePhotoSettings()
        photoSettings.isAutoStillImageStabilizationEnabled = true
        photoSettings.isHighResolutionPhotoEnabled = true
        photoSettings.isDepthDataDeliveryEnabled = true
        photoSettings.isPortraitEffectsMatteDeliveryEnabled = true
        capturePhotoOut?.capturePhoto(with: photoSettings, delegate: self)
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vc = segue.destination   as! DepthImageView
        vc.img = img
        vc.rgbImg = rgbImage
    }

extension ViewController : AVCapturePhotoCaptureDelegate{
    public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        guard error == nil else{return}
        guard let imageData = photo.fileDataRepresentation() else {return}

        let detailImage = UIImage.init(data: imageData,scale: 1.0)
        rgbImage = detailImage
        let nsData = imageData as NSData
        let ptr = nsData.bytes.assumingMemoryBound(to: UInt8.self)
        let cfDataset = CFDataCreate(nil,ptr,imageData.count)

        guard let source = CGImageSourceCreateWithData(cfDataset!,nil) else {return}
        guard let auxDataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeDepth) as? [String : AnyObject] else {
            return
        }
        var depthData: AVDepthData
        do {
            depthData = try AVDepthData(fromDictionaryRepresentation: auxDataInfo)
            if depthData.depthDataType != kCVPixelFormatType_DepthFloat32 {
                depthData = depthData.converting(toDepthDataType: kCVPixelFormatType_DepthFloat32)
            }
            let depthDataMap = depthData.depthDataMap
            let ciImage = CIImage(cvPixelBuffer: depthDataMap)
            let depthDataMapImage = UIImage(ciImage: ciImage,scale: 1.0,orientation: .down)
            img = depthDataMapImage
            self.performSegue(withIdentifier: "ImageViewScreen", sender: self)
        } catch {
            print("Error")
        }
    }
}

我总是在auxDataInfo守卫中得到零

AVCapturePhoto 包含有关 AVDepthData 的信息。尝试从照片中获取深度数据

let depthData = photo.depthData

相关内容

  • 没有找到相关文章

最新更新