ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
-1

CvBridge conversion problem: Asus Xtion depth image to OpenCV

asked 2015-07-02 06:24:55 -0600

WillAndrew gravatar image

Hi,

I have a Asus Xtion sensor being driven by the Openni2_launch package. I subscribe to the "/camera/depth/image" topic and attempt to convert the ImageConstPtr& data type to an OpenCV Mat in a callback function utilising the cv_bridge package. Visualising the "/camera/depth/image" ROS topic in RQT, I get a high quality depth image as expected:

image description

However I can't seem to find an image type encoding (e.g. "8UC1") which OpenCV/cv_bridge accepts which results in an image similar to the one above. As an example, with the encoding "32FC1", this is the type of quality I get. I'm not entirely sure what I'm doing wrong!

image description

And here's the source code

// Callback function for time-synchronised RGB and depth images
void sync_callback(const sensor_msgs::ImageConstPtr& rgb, const sensor_msgs::ImageConstPtr& depth)
{
    // Convert ROS images to OpenCV
    cv::Mat rgb_image;
    cv::Mat depth_image;

    try
    {
        rgb_image = cv_bridge::toCvShare(rgb, "mono8") -> image;
        depth_image = cv_bridge::toCvShare(depth, "32FC1") -> image;
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("Could not convert from ROS images to OpenCV type: %s", e.what());
    }

Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2015-07-04 01:01:21 -0600

fergs gravatar image

The output from openni2_camera is 32FC1, with measurements in meters. In the past I have converted to 32FC1, and then normalized in the range of ~0.5m to 5.5m. I haven't tried to compile my depth_viewer in a while, but it is probably a good starting spot.

edit flag offensive delete link more

Comments

Works really well, thanks!

WillAndrew gravatar image WillAndrew  ( 2015-07-06 09:23:28 -0600 )edit
1

answered 2015-07-03 02:06:00 -0600

updated 2015-07-04 04:31:18 -0600

This should works.

void sync_callback(const sensor_msgs::ImageConstPtr& rgb, const sensor_msgs::ImageConstPtr& depth)
{
    cv_bridge::CvImagePtr depthImg = cv_bridge::toCvCopy(depth , sensor_msgs::image_encodings::TYPE_32FC1);

    //Normalize the pixel value
    cv::normalize(depthImg->image, depthImg->image, 1, 0, cv::NORM_MINMAX);

    imwrite("./depth.jpg", depthImg->image);
    ros::shutdown();
}
edit flag offensive delete link more

Comments

My Xtion gives me a 16bpp image, so I would use 16UC1.

Humpelstilzchen gravatar image Humpelstilzchen  ( 2015-07-03 02:42:50 -0600 )edit

Thanks! but both of your suggestions didn't work (32FC1 resulted in the same black&white image, 16UC1 was just completely black..)

WillAndrew gravatar image WillAndrew  ( 2015-07-03 03:43:15 -0600 )edit

Thx, I used 32FC1 for Kinect. But it's weird it cannot show properly.

Po-Jen Lai gravatar image Po-Jen Lai  ( 2015-07-03 04:40:39 -0600 )edit

Sorry I have to correct myself, 16UC1 is for image_raw, what does

rostopic echo /camera/depth/image --noarr

report for you?

Humpelstilzchen gravatar image Humpelstilzchen  ( 2015-07-03 11:02:47 -0600 )edit

My Kinect is borrowed from someone now, so I don't have one to test.

Po-Jen Lai gravatar image Po-Jen Lai  ( 2015-07-03 22:00:44 -0600 )edit

"encoding: 32FC1" for the Xtion

WillAndrew gravatar image WillAndrew  ( 2015-07-06 03:30:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-02 06:24:55 -0600

Seen: 4,349 times

Last updated: Jul 04 '15