# Align Depth and Color Frames – Depth and RGB Registration

Sometimes it is necessary to create a point cloud from a given depth and color (RGB) frame. This is especially the case when a scene is captured using depth cameras such as Kinect. The process of aligning the depth and the RGB frame is called “registration” and it is very easy to do (and the algorithm’s pseudo-code is surprisingly hard to find with a simple Google search! 😀 )

To perform registration, you would need 4 pieces of information:

1. The depth camera intrinsics:
1. Focal lengths fxd and fyd (in pixel units)
2. Optical centers (sometimes called image centers) Cxd and Cyd
2. The RGB camera intrinsics:
1. Focal lengths fxrgb and fyrgb (in pixel units)
2. Optical centers (sometimes called image centers) Cxrgb and Cyrgb
3. The extrinsics relating the depth camera to the RGB camera. This is a 4×4 matrix containing rotation and translation values.
4. (Obviously) the depth and the RGB frames. Note that they do not have to have the same resolution. Applying the intrinsics takes care of the resolution issue. Using camera’s such as Kinect, the depth values should usually be in meters (the unit of the depth values is very important as using incorrect units will result in a registration in which the colors and the depth values are off and are clearly misaligned).
Also, note that some data sets apply a scale and a bias to the depth values in the depth frame. Make sure to account for this scaling and offsetting before proceeding. In order words, make sure there are no scales applied to the depth values of your depth frame.

Let `depthData` contain the depth frame and `rgbData` contain the RGB frame. The pseudo-code for registration in MATLAB is as follows:

A few things to note here:

1. The indices `x` and `y` in the second group of `for` loops may be invalid which indicates that the obtained RGB pixel is not visible to the RGB camera.
2. Some kind of interpolation may be necessary when using `x` and `y`. I just did rounding.
3. This code can be readily used with `savepcd` function to save the point cloud into a PCL compatible format.

The registration formulas were obtained from the paper “On-line Incremental 3D Human Body Reconstruction for HMI or AR Applications” by Almeida et al (2011). The same formulas can be found here. Hope this helps 🙂

• ramine on August 26, 2016 at 1:57 PM

Hello
Dear Mehran
Your code is useful for me.
do you know parameter camera for kinect version1?

1. Glad you found it useful! Unfortunately, I don’t have any calibration values for a Kinect 1. You may want to calibrate yourself…

• Vishnu Teja Yalakuntla on May 21, 2017 at 4:39 AM

The device comes with calibration parameters. For example, in pylibfreenect2, we can access them through

fn.openDevice(serial, pipeline=pipeline).getColorCameraParams()

or

fn.openDevice(serial, pipeline=pipeline).getIrCameraParams()

But I don’t know how accurate they are.

Cheers

1. Not very accurate. See my other post here: https://www.codefull.net/2017/04/practical-kinect-calibration/

• Seereen Noorwali on September 27, 2017 at 2:18 PM

Thanks for sharing the pseudo-code … do you have MATLAB code for register depth and color image for Kinect V2?

1. It would be literally the exact same code. The only difference in terms of application is, depth and color cameras on Kinect v2 have different resolutions compared to Kinect v1. So those value need to be set in the code I posted accordingly.

• Seereen Noorwali on October 3, 2017 at 4:06 PM

Thank you for your replay … I do not know how to get these values from Kinect v2 ? any idea? … all I need is getting the registration image from RGB and Depth images ?

1. Yes, you’d need to calibrate for both the extrinsics and the intrinsics.
Take a look at this post: https://www.codefull.net/2017/04/practical-kinect-calibration/

• Sayed on November 28, 2017 at 8:23 PM

What is depthScale? can you explain?

1. This is explained in the last paragraph of #4 above!

• N on January 16, 2018 at 1:08 PM

Hello, nice post, but i have a silly question, im using Kinect V2 and for the calibration im using GML C++ Camera Calibration Toolbox, i get the intrinsic parameters of the color and ir images individually, but how do you relate the extrinsics of the depth and rgb camera?

1. For that, you need to do an extrinsic calibration (in other words, find the rotation and translation between the IR and the color camera). I believe GML can also do extrinsic calibration. MATLAB can also do this for you. Take a look at https://www.mathworks.com/help/vision/ref/extrinsics.html?requestedDomain=true

• N on January 19, 2018 at 3:42 PM

Thank you so much! you’re the best (:

• amin__ on March 6, 2018 at 3:14 PM

Are those intrinsic and extrinsic parameters specific to a Kinect v2 sensor? Or they should be same for all Kinect v2 sensor? The situation I am having is, I have rgb and depth data of some vidoes. I want to extract the foreground from those data (by using the depth map). I don’t have the parameters for the Kinect v2 sensor used to collect those data. Is that possible?

1. Each Kinect has a different calibration. While the values are generally close between various devices, it’s usually best to calibrate the device before using it.
You could certainly use the values you find online, but be advised that you may not get accurate registration results.
Take a look at this post (https://www.codefull.net/2017/04/practical-kinect-calibration/) to get some insight into obtaining good Kinect calibration values.

• amin__ on March 7, 2018 at 5:47 PM

I have dataset from another group. They did not publish calibration params. They only published depth, rgb and skeleton data. That’s why I have no way to get params from the Kinect they used to collect data. I will try to use calibration params from online. Thanks

1. Usually, whoever creates a dataset will also include calibration values for the dataset (otherwise the dataset will be useless :D)
Try to see if that dataset you’re using has anything shipped with it. What dataset is it anyway?

• Dave on July 17, 2018 at 5:25 PM

Is there a working example of your code?

and how did u got depthData and rgbData into the code?

• D on July 17, 2018 at 8:00 PM

hey, can you show a working example of your code?

how did you feed depthData and rgbData into the code?

• Ankit Jaiswal on November 7, 2018 at 8:20 AM

Hi,
I am very new to this and just started with MATLAB.

Before using the above function, I will have to define all input parameters right.
I cant use the code as it is and just run it and get the output right?

• Ting on December 2, 2018 at 4:41 PM

Shouldn’t the extrinsics matrix be a 3×4 matrix instead of 4×4?

Also, it looks like matlab function extrinsics can obtain the rotation and translation matrices for RGB and depth camera separately… How do I get the rotation and translation matrices between RGB and depth?

Sorry that I do not know much about image processing, please forgive me if I am asking stupid questions….

Thank you so much for your help!

1. Although the extrinsics can be represented as 3×4, it’s usually convenient to represent as a 4×4 because you can easily multiply them together in cases where you have more than 2 cameras.

Also. obtaining the extrinsics itself requires camera calibration. Read more about it here: https://www.codefull.net/2017/04/practical-kinect-calibration