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

Extract header timestamp of Image message

asked 2016-02-18 04:49:55 -0500

user23fj239 gravatar image

updated 2016-02-18 04:56:52 -0500

Is the extraction of a timestamp from the header of a sensor_msgs::Image that easy:

void imageCb(const sensor_msgs::ImageConstPtr& imsg) {
    cv_bridge::CvImagePtr cv_ptr;
    try {
        cv_ptr = cv_bridge::toCvCopy(imsg, sensor_msgs::image_encodings::TYPE_16UC1);
    catch (cv_bridge::Exception& e) {
        ROS_ERROR("cv_bridge exception: %s", e.what());

    //get header info
    std_msgs::Header h = imsg->header;
    cout<<h<<endl; //all at once
    cout<<h.stamp<<endl; //specific parts of it

    //do some with it

This is the output

seq: 11536  //all info
stamp: 1455792836.004090186
frame_id: camera_depth_optical_frame

1455792836.004090186 //stamp
1455792836 //sec
4090186 //nsec
11536 //seq
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-02-18 04:52:21 -0500

gvdhoorn gravatar image


I wouldn't call it extraction though. You're basically just accessing the .header.stamp member variable.

This of course depends on your message having a field of type Header.

edit flag offensive delete link more


How to check if its there to prevent errors, segfaults etc, when trying to access it and its not there?

user23fj239 gravatar image user23fj239  ( 2016-02-18 04:58:27 -0500 )edit

ROS messages in C++ are auto-generated from the msg files. The compiler will error-out if you try to use fields that are not part of the struct definition. For dynamically typed languages you'll have to use the facilities of the language to check for presence of fields.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-18 05:01:45 -0500 )edit

Question Tools

1 follower


Asked: 2016-02-18 04:49:55 -0500

Seen: 15,648 times

Last updated: Feb 18 '16