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

Error: "Unable to convert 32FC1 image to bgr8" while extracting rbg(d?) data from a bag

asked 2013-04-07 16:31:23 -0600

FranciscoD gravatar image

updated 2014-11-22 17:05:10 -0600

ngrennan gravatar image

I'm trying to extract messages of the "/camera/depth_registered/image_rect" topic from my bag:

[ankur@eng050194  20130405]$ rosbag info 20130405_navtest_0.bag
path:        20130405_navtest_0.bag
version:     2.0
duration:    58.8s
start:       Apr 05 2013 15:32:07.42 (1365136327.42)
end:         Apr 05 2013 15:33:06.25 (1365136386.25)
size:        3.3 GB
messages:    17410
compression: none [3286/3286 chunks]
types:       sensor_msgs/Image      [060021388200f6f0f447d0fcd9c64743]
             sensor_msgs/Imu        [6a62c6daae103f4ff57a132d6f95cec2]
             sensor_msgs/JointState [3066dcd76a6cfaef579bd0f34173e9fd]
             sensor_msgs/LaserScan  [90c7ef2dc6895d81024acba2ac42f369]
topics:      /base_scan                            1178 msgs    : sensor_msgs/LaserScan
             /camera/depth_registered/image_rect   1626 msgs    : sensor_msgs/Image
             /camera/rgb/image_rect_color          1658 msgs    : sensor_msgs/Image
             /joint_states                         5883 msgs    : sensor_msgs/JointState
             /tilt_scan                            1182 msgs    : sensor_msgs/LaserScan
             /torso_lift_imu/data                  5883 msgs    : sensor_msgs/Imu
[ankur@eng050194  20130405]$

I've written up a launch file as described in the tutorial (here):

  <node pkg="rosbag" type="play" name="rosbag" args="-d 2 /home/ankur/dr_jack/20130405/20130405_navtest_0.bag"/>
  <node name="extract" pkg="image_view" type="extract_images" respawn="false" output="screen" cwd="ROS_HOME">
    <remap from="image" to="/camera/depth_registered/image_rect"/>

However, when I run the launch file, it errors out:

[ankur@eng050194  20130405]$ roslaunch exportImageData.launch
... logging to /home/ankur/.ros/log/c113647a-9feb-11e2-b9c4-24be051362b4/
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server


 * /rosdistro
 * /rosversion

    extract (image_view/extract_images)
    rosbag (rosbag/play)


core service [/rosout] found
process[rosbag-1]: started with pid [32095]
process[extract-2]: started with pid [32096]
[ INFO] [1365387639.215633458]: Initialized sec per frame to 0.100000
[ERROR] [1365387644.328011223]: Unable to convert 32FC1 image to bgr8
[ WARN] [1365387644.328120105]: Couldn't save image, no data!
[ERROR] [1365387644.329480639]: Unable to convert 32FC1 image to bgr8
[ERROR] [1365387644.393881439]: Unable to convert 32FC1 image to bgr8
[ERROR] [1365387644.423329040]: Unable to convert 32FC1 image to bgr8
[ERROR] [1365387644.454279657]: Unable to convert 32FC1 image to bgr8
[ WARN] [1365387644.454386746]: Couldn't save image, no data!
[ERROR] [1365387644.487755386]: Unable to convert 32FC1 image to bgr8

Would someone know what I'm doing wrong? I extracted the images of the "/camera/rgb/image_rect_color" topic just fine using this approach.

Could be related: I can't view these images using rqt_bag either. Am I missing some package that provides this capability?

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted

answered 2013-04-07 17:39:08 -0600

updated 2017-01-08 23:57:29 -0600

We encountered the same problem last time. Dan is correct, the format is not matched.

If you look into rgbdslam package, there is a small piece of code doing this conversion.

void depthToCV8UC1(const cv::Mat& float_img, cv::Mat& mono8_img){
  //Process images
  if(mono8_img.rows != float_img.rows || mono8_img.cols != float_img.cols){
    mono8_img = cv::Mat(float_img.size(), CV_8UC1);}
  cv::convertScaleAbs(float_img, mono8_img, 100, 0.0);
  //The following doesn't work due to NaNs
  //double minVal, maxVal; 
  //minMaxLoc(float_img, &minVal, &maxVal);
  //ROS_DEBUG("Minimum/Maximum Depth in current image: %f/%f", minVal, maxVal);
  //mono8_img = cv::Scalar(0);
  //cv::line( mono8_img, cv::Point2i(10,10),cv::Point2i(200,100), cv::Scalar(255), 3, 8);

After this, you change the image to a CV_8UC1, this is a 256 greyscale image. You can write a small node, subscribe to the depth image and publish this greyscale image, than use your extract...should work.

Actually, the depth image is using 11 bit to store the depth info (I remember, the other 5 bit is using to label different object), so it is OK to save it to a 8 bit greyscale image, you will not lose too much info.

------ edit ------ to answer the questions in comments.

in main function, create the callback

image_transport::Subscriber depth_sub = it.subscribe("camera/depth/image", 1, depthCallback);

and my callback function (just a simple void function for your reference)

void depthCallback(const sensor_msgs::ImageConstPtr& original_image)
    cv_bridge::CvImagePtr cv_ptr;
    //Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing
        //Always copy, returning a mutable CvImage
        //OpenCV expects color images to use BGR channel order.
        cv_ptr = cv_bridge::toCvCopy(original_image);
    catch (cv_bridge::Exception& e)
        //if there is an error during conversion, display it
        ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());

    //Copy the to imageBuf.
    cv::Mat depth_float_img = cv_ptr->image;
    cv::Mat depth_mono8_img;
    depthToCV8UC1(depth_float_img, depth_mono8_img);
edit flag offensive delete link more


Thanks for your answers @dan-lazewatsky, @tianb03. I understand what you mean. I can visualize the data via rviz. If I must extract the data, I'll use the method described above.

FranciscoD gravatar image FranciscoD  ( 2013-04-07 18:38:24 -0600 )edit

@tianb03 I am also trying to convert bagfiles containing raw depth and rgb data and face the same problem when trying to convert raw depth to images. But I don't understand where and how to execute the code you have given and what the node should contain. I am new to ROS. So please explain it to me

shravan gravatar image shravan  ( 2014-12-16 06:23:00 -0600 )edit

I'm also interested ! Thank

lilouch gravatar image lilouch  ( 2015-03-26 01:57:08 -0600 )edit

answered 2013-04-07 17:15:18 -0600

This is happening because extract_images tries to decode the images as bgr8 so they're in a suitable format to save to disk, but because depth images are 32FC1, they can't be decoded as bgr8. Arguably, this is the correct behavior, especially considering that images are saved using lossy compression which isn't designed for depth images.

Is there a reason you want individual images saved out of your depth data rather than using the bag file itself? There are a few options depending on what you want to do.

edit flag offensive delete link more

answered 2013-04-07 17:16:11 -0600

As I understood, the /camera/depth_registered/image_rect images should be depth only images. That means that you can just treat the images as grey images, but not color images. So there is only one component (depth) for each pixel.


edit flag offensive delete link more

answered 2016-12-20 06:54:52 -0600

Really Worked for me! Thank You!

edit flag offensive delete link more



this should leave as a comment under the answer you voted but not open a new answer...

tianb03 gravatar image tianb03  ( 2017-01-09 00:18:38 -0600 )edit

Question Tools

1 follower


Asked: 2013-04-07 16:31:23 -0600

Seen: 10,887 times

Last updated: Jan 08 '17