Save ARDepthData as .tiff

I would like to save the depth map from ARDepthData as .tiff, but notice my output tiff distances are incorrect. Objects that are close are reported to be slightly farther away, and walls that are around 4 meters away from me have a recorded value of 2 meters. I am using this code to write the tiff:

import UIKit

# Save method
extension CVPixelBuffer {
    func saveDepthMapToTIFF(to path: URL) {
        let ciImage = CIImage(cvPixelBuffer: self)
        let context = CIContext()
        do {
            try context.writeTIFFRepresentation(
                of: ciImage,
                to: path,
                format: .Lf,
                colorSpace: CGColorSpaceCreateDeviceGray()
            )
        } catch {
            print("Failed to write TIFF: \(error)")
        }
    }
}

# Calling the save
arFrame.sceneDepth?.depthMap.saveDepthMapToTIFF(to: depthMapPath)

I am reading the file like this in Python

import tifffile

depth_map = tifffile.imread("test.tiff")
plt.imshow(depth_map)
plt.colorbar()

which creates this image:

The farthest parts of the room should be around 4 meters, not 2. The dark blue spot on the lower right is closer than half a meter away.

Notably the depth map contains distances from the camera plane to each region, not the distance from the camera sensor to the region. Even correcting for this though, the depth map remains about the same.

Is there an issue with how I am saving the depth image? Is there a scale factor or format error?

  • Hi, I had a similar issue. After trying different things, I found out that the color space you are using (CGColorSpaceCreateDeviceGray()) produces this kind of result. However, after trying this with CGColorSpace(name: CGColorSpace.linearGray), the result are much more accurate.

Add a Comment