Rosanswers logo

Hi All,

I tried to subscribe to kinect depth and color image topics from openni_launch..(/camera/rgb/image_color and /camera/depth/image )

I converted them to cv::Mat format using below code.

cv_rgb = cv_bridge::toCvCopy(color_image, enc::BGR8); cv_depth = cv_bridge::toCvCopy(depth_image, enc::BGR8); (I am not sure about this line)

Now I want to access each pixel values of both color and depth image. I am able to access the rgb image by the below code.

for(int i=0; i<cv_rgb->image.rows; i++) {
for(int j=0; j<cv_rgb->image.cols; j++) {

       cv_rgb->image.at<cv::Vec3b>(i,j)[0] = 0;
       cv_rgb->image.at<cv::Vec3b>(i,j)[1] = 0;
      cv_rgb->image.at<cv::Vec3b>(i,j)[2] = 0;


I am unable to access the kinect depth image. It says me the type is 21. and Its a single channel image. Can anybody suggest me a way that I can access each pixel values of Kinect and how can this pixel values be translated in terms of real distance in mm ?

Originally posted by Subhasis on ROS Answers with karma: 31 on 2012-10-25

Post score: 1


1 Answer 1


Rosanswers logo

The encoding you are using for the depth stream is wrong, you should use the unsigned 16 bit representation with a single channel, instead of the 3 channels of the RGB representation.

Modify the line -cv_depth = cv_bridge::toCvCopy(depth_image, enc::BGR8);

changing the encoding (check the correct definition for 16 unsigned char) -cv_depth = cv_bridge::toCvCopy(depth_image, enc::16UC1);

In this way, you can access depth data properly. According to the topic you subscribe to, you may obtain either representation in millimeters or in meters (both of them use floats)

Good luck, Tambo

Originally posted by Tambo with karma: 56 on 2012-10-25

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by dmngu9 on 2015-05-28:
i wonder how you can align rgb and depth frames together


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.