【问题标题】:How to get depth data from camera images如何从相机图像中获取深度数据
【发布时间】:2019-11-30 06:42:21
【问题描述】:

我正在编写一个应用程序来从相机拍摄的照片中获取深度数据和视差数据。我可以得到视差数据,但不能得到它总是返回 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 守卫处总是得到零

【问题讨论】:

    标签: ios swift avcapturephotooutput avdepthdata


    【解决方案1】:

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

    let depthData = photo.depthData
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多