Ask Your Question
2

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());
        return;
    }

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

    //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
1

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

gvdhoorn gravatar image

Yes.

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

Comments

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

user23fj239 gravatar imageuser23fj239 ( 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 imagegvdhoorn ( 2016-02-18 05:01:45 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 8,536 times

Last updated: Feb 18 '16