# How to convert RawDepthImage

**URL:** <https://forum.magicleap.cloud/t/how-to-convert-rawdepthimage/3432>\
**Category:** Unity Development\
**Tags:** unity, depth-camera\
**Created:** [April 22, 2024, 8:06am UTC](https://forum.magicleap.cloud/t/how-to-convert-rawdepthimage/3432 "2024-04-22T08:06:10Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![kbabilinski](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/kbabilinski/32/19_2.png) [@kbabilinski](https://forum.magicleap.cloud/u/kbabilinski)\
**Post date:** [April 23, 2024, 2:47pm UTC](https://forum.magicleap.cloud/t/how-to-convert-rawdepthimage/3432/4 "2024-04-23T14:47:26Z")

</div>

You will need to do two things.

1. Undistort the image so that the depth points are projected correctly.
2. The image is a range image, so you will need to multiply the pixel position by the depth from the image.

The example below uses a cached projection table (that handles the distortion) and the depth data obtained from the depth sensor:

```csharp
        // Iterate through each pixel in the depth data.
        for (int y = 0; y < resolution.y; ++y)
        {
            for (int x = 0; x < resolution.x; ++x)
            {
                // Calculate the linear index based on x, y coordinates.
                int index = x + (resolution.y - y - 1) * resolution.x;
                float depth = depthData[index];

                // Skip processing if depth is out of range or confidence is too low (if filter is enabled).
                // Confidence comes directly from the sensor pipeline and is represented as a float ranging from
                // [-1.0, 0.0] for long range and [-0.1, 0.0] for short range, where 0 is highest confidence. 
                if (depth < minDepth || depth > maxDepth || (useConfidenceFilter && confidenceData[index] < -0.1f))
                {
                    //Set the invalid points to be positioned at 0,0,0
                    depthPoints[index] = Vector3.zero;
                    continue;
                }

                // Use the cached projection table to find the UV coordinates for the current point.
                Vector2 uv = cachedProjectionTable[y, x];
                // Transform the UV coordinates into a camera space point.
                Vector3 cameraPoint = new Vector3(uv.x, uv.y, 1).normalized * depth;
                // Convert the camera space point into a world space point.
                Vector3 worldPoint = cameraToWorldMatrix.MultiplyPoint3x4(cameraPoint);

                // Store the world space point in the depthPoints array.
                depthPoints[index] = worldPoint;
            }
        }

```

Here is a more detailed example:

> [@Processing the depth frames](https://forum.magicleap.cloud/t/processing-the-depth-frames/3284/13):
>
> Here is a script that sends an array of world points to a "point cloud renderer" . I have not included the point cloud renderer code but the script should be enough to demonstrate how to obtain the undistorted depth data. A few things to note. You may want to check if the depth point falls into the user's field of view and exclude any depth points that are not visible. This will ensure that the depth image aligns to the world more closely. Although, the point cloud is undistorted, some skewing …

---

_[View the full topic](https://forum.magicleap.cloud/t/how-to-convert-rawdepthimage/3432)._
