1
+ #include < iostream>
2
+ #include < fstream>
3
+
4
+ #include < ros/ros.h>
5
+ #include < cv_bridge/cv_bridge.h>
6
+ #include < tf/transform_listener.h>
7
+ #include < message_filters/subscriber.h>
8
+ #include < message_filters/time_synchronizer.h>
9
+ #include < message_filters/sync_policies/approximate_time.h>
10
+ #include < image_transport/subscriber_filter.h>
11
+ #include < sensor_msgs/image_encodings.h>
12
+ #include < auv_msgs/NavSts.h>
13
+ #include < image_transport/image_transport.h>
14
+ #include < opencv2/opencv.hpp>
15
+
16
+ // Topic sync
17
+ typedef message_filters::sync_policies::ApproximateTime<auv_msgs::NavSts,
18
+ sensor_msgs::Image,
19
+ sensor_msgs::CameraInfo> SyncPolicy;
20
+ typedef message_filters::Synchronizer<SyncPolicy> Sync;
21
+
22
+ class ImagePositions {
23
+ ros::NodeHandle nh_;
24
+ image_transport::ImageTransport it_;
25
+ image_transport::CameraSubscriber cam_sub_;
26
+ image_transport::CameraSubscriber sub_;
27
+ tf::TransformListener tf_listener_;
28
+ std::string parent_frame_id_;
29
+ std::string child_frame_id_;
30
+ std::string output_file_;
31
+ std::string output_folder_;
32
+ std::ofstream file_;
33
+ int count_;
34
+
35
+ public:
36
+ ImagePositions (const std::vector<std::string>& frame_ids)
37
+ : it_(nh_), count_(0 ){
38
+ nh_.getParam (" parent_frame_id" , parent_frame_id_);
39
+ nh_.getParam (" child_frame_id" , child_frame_id_);
40
+ nh_.getParam (" output_file" , output_file_);
41
+ nh_.getParam (" output_folder" , output_folder_);
42
+ std::string fn (output_folder_ + " /" + output_file_);
43
+ file_.open (fn.c_str ());
44
+ file_ << " name, lat, lon, depth, altitude, roll, pitch, yaw\n " ;
45
+ std::string camera_topic = nh_.resolveName (" camera" );
46
+ std::string nav_sts_topic = nh_.resolveName (" nav_sts" );
47
+
48
+ image_transport::SubscriberFilter image_sub;
49
+ message_filters::Subscriber<sensor_msgs::CameraInfo> info_sub;
50
+ message_filters::Subscriber<auv_msgs::NavSts> nav_sts_sub;
51
+
52
+ // Message sync
53
+ boost::shared_ptr<Sync> sync;
54
+ nav_sts_sub.subscribe (nh_, nav_sts_topic, 20 );
55
+ image_sub .subscribe (it_, camera_topic + " /image_rect_color" , 5 );
56
+ info_sub .subscribe (nh_, camera_topic + " /camera_info" , 5 );
57
+ sync.reset (new Sync (SyncPolicy (10 ), nav_sts_sub, image_sub, info_sub) );
58
+ sync->registerCallback (boost::bind (&ImagePositions::msgsCallback, this , _1, _2, _3));
59
+ }
60
+
61
+ ~ImagePositions () {
62
+ file_.close ();
63
+ }
64
+
65
+ void msgsCallback (const auv_msgs::NavStsConstPtr& nav_sts_msg,
66
+ const sensor_msgs::ImageConstPtr& image_msg,
67
+ const sensor_msgs::CameraInfoConstPtr& info_msg) {
68
+ cv::Mat image;
69
+ cv_bridge::CvImagePtr input_bridge;
70
+ try {
71
+ input_bridge = cv_bridge::toCvCopy (image_msg, sensor_msgs::image_encodings::BGR8);
72
+ image = input_bridge->image ;
73
+ }
74
+ catch (cv_bridge::Exception& ex){
75
+ ROS_ERROR (" [ImagePositions] Failed to convert image" );
76
+ return ;
77
+ }
78
+
79
+ double lat = nav_sts_msg->position .north ;
80
+ double lon = nav_sts_msg->position .east ;
81
+ double depth = nav_sts_msg->position .depth ;
82
+ double altitude = nav_sts_msg->altitude ;
83
+ double roll = nav_sts_msg->orientation .roll ;
84
+ double pitch = nav_sts_msg->orientation .pitch ;
85
+ double yaw = nav_sts_msg->orientation .yaw ;
86
+
87
+ std::stringstream ss;
88
+ ss << std::setw (6 ) << std::setfill (' 0' ) << count_;
89
+ std::string name = ss.str () + " .png" ;
90
+
91
+ ROS_INFO_STREAM (" [ImagePositions]: Saving image " << name);
92
+ cv::imwrite (output_folder_ + " /" + name, image);
93
+
94
+ file_ << name << " , "
95
+ << lat << " , "
96
+ << lon << " , "
97
+ << depth << " , "
98
+ << altitude << " , "
99
+ << roll << " , "
100
+ << pitch << " , "
101
+ << yaw << " \n " ;
102
+ }
103
+ };
104
+
105
+ int main (int argc, char ** argv) {
106
+ ros::init (argc, argv, " image_positions" );
107
+ ImagePositions ip ();
108
+ ros::spin ();
109
+ }
0 commit comments