ed_sensor_integration
image_saver.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 
3 // Services
4 #include <ed_sensor_integration_msgs/GetImage.h>
5 
6 // Write to file
7 #include <fstream>
8 
9 // Read RGBD
12 #include <rgbd/serialization.h>
13 
14 // Time
15 //#include <stdio.h>
16 #include <time.h>
17 
18 // Visualization
19 #include <opencv2/highgui/highgui.hpp>
20 
21 // ----------------------------------------------------------------------------------------------------
22 
23 // Get current date/time, format is YYYY-MM-DD-HH-mm-ss
25 {
26  time_t now = time(0);
27  struct tm tstruct;
28  char buf[80];
29  tstruct = *localtime(&now);
30 
31  // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
32  // for more information about date/time format
33  strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M-%S", &tstruct);
34 
35  return buf;
36 }
37 
38 // ----------------------------------------------------------------------------------------------------
39 
40 int main(int argc, char **argv)
41 {
42  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43  // Show usage
44 
45  std::cout << "Shows RGBD image stream and allows to save images." << std::endl
46  << std::endl
47  << "Keys:" << std::endl
48  << std::endl
49  << " <SPACE> Save image to disk" << std::endl
50  << " + Increase frames per second" << std::endl
51  << " - decrease frames per second" << std::endl
52  << " <ESC> or q quit" << std::endl
53  << std::endl;
54 
55  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
56  // Initialize
57 
58  ros::init(argc, argv, "ed_image_saver");
59  ros::NodeHandle nh;
60  ros::ServiceClient cl_get_image = nh.serviceClient<ed_sensor_integration_msgs::GetImage>("ed/kinect/get_image");
61 
62  double fps = 1;
63 
64  ros::Time t_last_saved(0);
65 
67  ed_sensor_integration_msgs::GetImage srv;
68 
69  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70  // Start loop
71 
72  while (ros::ok())
73  {
74  ros::Time t_now = ros::Time::now();
75 
76  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77  // Check if it is time to request image
78 
79  if (t_now - t_last_saved > ros::Duration(1.0 / fps))
80  {
81  // - - - - - - - - - - - - - - - - - - - - - - - - - -
82  // Retrieve image
83 
84  // Try to retrieve the current kinect image
85  srv.request.filename = currentDateTime();
86  if (!cl_get_image.call(srv))
87  {
88  std::cout << "Could not call service '" << cl_get_image.getService() << "'." << std::endl;
89  ros::Duration(1.0).sleep();
90  continue;
91  }
92 
93  // - - - - - - - - - - - - - - - - - - - - - - - - - -
94  // Deserialize rgbd image
95 
96  std::stringstream stream;
97  tue::serialization::convert(srv.response.rgbd_data, stream);
99  rgbd::deserialize(a_in, image);
100 
101  t_last_saved = t_now;
102  }
103 
104  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105  // Show rgbd image
106 
107  cv::Mat img = image.getRGBImage().clone();
108  cv::putText(img, "Press <SPACE> to save", cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0));
109 
110  std::stringstream s_fps;
111  s_fps << fps << " fps";
112 
113  cv::putText(img, s_fps.str(), cv::Point(img.cols - 60, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0));
114 
115  if (img.cols == 0 || img.rows == 0)
116  {
117 // std::cout << "Image size 0, 0, skipping" << std::endl;
118  continue;
119  }
120  cv::imshow("Image Saver", img);
121  unsigned char key = cv::waitKey(30);
122 
123  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124  // Deal with key press
125 
126  if (key == 27 || key == 'q') // ESC or 'q'
127  {
128  break;
129  }
130  else if (key == '+')
131  {
132  fps = std::min<double>(10, fps + 1);
133  }
134  else if (key == '-')
135  {
136  fps = std::max<double>(1, fps - 1);
137  }
138  else if (key == 32) // SPACE
139  {
140  const std::string& filename = srv.request.filename;
141 
142  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143  // Write RGBD file
144 
145  std::ofstream f_out;
146  std::string rgbd_filename = filename + ".rgbd";
147  f_out.open(rgbd_filename.c_str());
148  f_out.write(reinterpret_cast<char*>(&srv.response.rgbd_data[0]), srv.response.rgbd_data.size());
149 
150  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151  // Write JSON file
152 
153  std::ofstream f_meta;
154  f_meta.open((filename + ".json").c_str());
155  f_meta << srv.response.json_meta_data;
156 
157  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158  // Show image with filename
159 
160  cv::Mat img = image.getRGBImage().clone();
161  cv::putText(img, "Saved to " + filename + ".json", cv::Point(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0));
162 
163  cv::imshow("Image Saver", img);
164  cv::waitKey(1000);
165  }
166  }
167 
168  return 0;
169 }
input_archive.h
fstream
std::string
main
int main(int argc, char **argv)
Definition: image_saver.cpp:40
time.h
rgbd::deserialize
bool deserialize(tue::serialization::InputArchive &a, Image &image)
std::stringstream
std::ofstream::write
T write(T... args)
image
cv::Mat image
std::cout
tue::serialization::convert
void convert(Archive &a, std::vector< unsigned char > &data)
rgbd::Image
std::ofstream
std::string::c_str
T c_str(T... args)
serialization.h
std::ofstream::open
T open(T... args)
tue::serialization::InputArchive
std::endl
T endl(T... args)
currentDateTime
std::string currentDateTime()
Definition: image_saver.cpp:24
std::stringstream::str
T str(T... args)
conversions.h