首页 > 解决方案 > iOS ARKit AVDepthData 返回巨大的数字

问题描述

不知道我在这里做错了什么,但是 AVDepthData,它应该以米为单位用于非差异数据,它返回了巨大的数字。我有代码...

-(bool)getCurrentFrameDepthBufferIntoBuffer:(ARSession*)session buffer:(BytePtr)buffer width:(int)width height:(int)height bytesPerPixel:(int)bytesPerPixel
{
    // do we have a current frame
    if (session.currentFrame != nil)
    {
        // do we have a captured image?
        if (session.currentFrame.capturedDepthData != nil)
        {
            // get depth data parameters
            int ciImageWidth = (int)CVPixelBufferGetWidth(session.currentFrame.capturedDepthData.depthDataMap);
            int ciImageHeight = (int)CVPixelBufferGetHeight(session.currentFrame.capturedDepthData.depthDataMap);
            // how many bytes per pixel
            int bytesPerPixel;
            if (session.currentFrame.capturedDepthData.depthDataType == kCVPixelFormatType_DisparityFloat16 ||
                session.currentFrame.capturedDepthData.depthDataType == kCVPixelFormatType_DepthFloat16)
                bytesPerPixel = 2;
            else
                bytesPerPixel = 4;

            // copy to passed buffer
            CVPixelBufferLockBaseAddress(session.currentFrame.capturedDepthData.depthDataMap, kCVPixelBufferLock_ReadOnly);
            memcpy(buffer, session.currentFrame.capturedDepthData.depthDataMap, ciImageWidth*ciImageHeight*bytesPerPixel);

            float *floatBuffer = (float*)buffer;
            float maxDepth = 0.0f;
            float minDepth = 0.0f;
            for (int i=0; i < ciImageWidth*ciImageHeight; i++)
            {
                if (floatBuffer[i] > maxDepth)
                    maxDepth = floatBuffer[i];
                if (floatBuffer[i] < minDepth)
                    minDepth = floatBuffer[i];
            }

            NSLog(@"In iOS, max depth is %f min depth is %f", maxDepth, minDepth);
            CVPixelBufferUnlockBaseAddress(session.currentFrame.capturedDepthData.depthDataMap, kCVPixelBufferLock_ReadOnly);
        }
    }

    return true;
}

但它返回最小值和最大值,例如......

2019-06-27 12:32:32.167868+0900 AvatarBuilder[13577:2650159] 在 iOS 中,最大深度为 3531476501829561451725831270301696000.000000 最小深度为 -10967712993094046407301024

看起来一点也不像米。

标签: arkit

解决方案


呃,这是我的memcpy。我必须做...

            float *bufferAddress = (float*)CVPixelBufferGetBaseAddress(session.currentFrame.capturedDepthData.depthDataMap);
        memcpy(buffer, bufferAddress, ciImageWidth*ciImageHeight*bytesPerPixel);

推荐阅读