normalization_image_generator_node.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 
30 #include <string>
31 #include <vector>
32 
33 // ROS Libraries
34 #include <ros/ros.h>
35 #include <ros/package.h>
36 #include <sensor_msgs/Image.h>
37 #include <cv_bridge/cv_bridge.h>
38 
39 // OpenCV Libraries
40 #include <opencv2/core/core.hpp>
41 #include <opencv2/highgui/highgui.hpp>
42 
43 // RANGER Libraries
45 
47 {
48  private:
50 
52 
53  int32_t num_to_skip_;
55  std::string filename_;
56 
57  int32_t raw_count_;
58  int32_t image_count_;
59 
61 
62  std::vector<cv::Mat> image_array_;
63 
64 
66  {
67  nh_.param(ros::this_node::getName() + "/num_to_skip",
68  num_to_skip_,
69  200);
70 
71  nh_.param(ros::this_node::getName() + "/max_num_to_average",
72  max_num_to_average_,
73  100);
74 
75  std::string temp_filename;
76  temp_filename = ros::package::getPath("ranger_common") +
77  "/normalization_image.png";
78 
79 
80  nh_.param(ros::this_node::getName() + "/filename",
81  filename_,
82  temp_filename);
83 
84  ROS_ERROR("Planning to write normalization image to: %s",
85  filename_.c_str());
86  }
87 
88 
90  {
91  image_sub_ = nh_.subscribe("image",
92  2,
94  this);
95  }
96 
97  void image_cb(const sensor_msgs::ImageConstPtr& msg)
98  {
99  if (image_count_ >= max_num_to_average_)
100  {
101  ::sleep(1);
102  return;
103  }
104 
105  if (raw_count_++ % num_to_skip_ == 0)
106  {
107  image_count_++;
108  ROS_ERROR("Got image %d of %d",
109  image_count_,
110  max_num_to_average_);
111 
113  cv::Mat image(im_ptr->image);
114  image_array_.push_back(image);
115  if (image_count_ >= max_num_to_average_)
116  {
118  }
119  }
120  }
121 
122 
124  {
125  cv::Mat norm_im = swri_image_util::generate_normalization_image(image_array_);
126  if (!norm_im.empty())
127  {
128  try
129  {
130  cv::imwrite(filename_, norm_im);
131  }
132  catch (const std::exception& e)
133  {
134  ROS_ERROR("Failed to save the normalization image: %s",
135  e.what());
136  return;
137  }
138  ROS_ERROR("Successfully wrote normalization image to: %s",
139  filename_.c_str());
140 
141  image_written_ = true;
142  }
143  else
144  {
145  ROS_ERROR("Failed to generate a normalization image");
146  }
147  }
148 
149  public:
151  nh_(nh),
152  num_to_skip_(20),
153  max_num_to_average_(100),
154  raw_count_(0),
155  image_count_(0),
156  image_written_(false)
157  {
158  filename_ = ros::package::getPath("ranger_common") +
159  "/normalization_image.png";
160 
161  get_parameters();
162 
164  }
165 
166  void shut_down()
167  {
168  if (!image_written_ && image_array_.size() > 25)
169  {
171  fprintf(stderr, "\nNode killed before enough frames received to generate "
172  "normalized image, so a normalized image was generated with "
173  "available frames (%d vs. %d)\n",
174  image_count_,
175  max_num_to_average_);
176  }
177  else if (!image_written_)
178  {
179  fprintf(stderr, "\nNode killed before enough frames received to generate "
180  "normalized image: Too few frames in the buffer to generate a "
181  "normalization image\n");
182  }
183  else
184  {
185  fprintf(stderr, "\nExiting normally\n");
186  }
187  }
188 
190  {
191  return image_written_;
192  }
193 };
194 
195 int main(int argc, char **argv)
196 {
197  ros::init(argc, argv, "image_normalization_node");
198 
199  ros::NodeHandle n;
200 
201  NormalizationImageNode node(n);
202 
203  ros::spin();
204 
205  node.shut_down();
206 
207  return 0;
208 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
ROSCPP_DECL void spin(Spinner &spinner)
cv::Mat generate_normalization_image(const std::vector< cv::Mat > &image_list)
Computes a best estimate of a normalization image from a vector of images.
NormalizationImageNode(const ros::NodeHandle &nh)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
int main(int argc, char **argv)
ROSLIB_DECL std::string getPath(const std::string &package_name)
void image_cb(const sensor_msgs::ImageConstPtr &msg)
#define ROS_ERROR(...)


swri_image_util
Author(s): Kris Kozak
autogenerated on Fri Jun 7 2019 22:05:56