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",
69  200);
70 
71  nh_.param(ros::this_node::getName() + "/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  {
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_,
111 
113  cv::Mat image(im_ptr->image);
114  image_array_.push_back(image);
116  {
118  }
119  }
120  }
121 
122 
124  {
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_,
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 }
NormalizationImageNode::raw_count_
int32_t raw_count_
Definition: normalization_image_generator_node.cpp:57
msg
msg
ros::package::getPath
ROSLIB_DECL std::string getPath(const std::string &package_name)
boost::shared_ptr
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
NormalizationImageNode::get_image_written
bool get_image_written()
Definition: normalization_image_generator_node.cpp:189
NormalizationImageNode::get_parameters
void get_parameters()
Definition: normalization_image_generator_node.cpp:65
NormalizationImageNode
Definition: normalization_image_generator_node.cpp:46
NormalizationImageNode::image_array_
std::vector< cv::Mat > image_array_
Definition: normalization_image_generator_node.cpp:62
NormalizationImageNode::nh_
ros::NodeHandle nh_
Definition: normalization_image_generator_node.cpp:49
NormalizationImageNode::image_sub_
ros::Subscriber image_sub_
Definition: normalization_image_generator_node.cpp:51
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
NormalizationImageNode::image_count_
int32_t image_count_
Definition: normalization_image_generator_node.cpp:58
main
int main(int argc, char **argv)
Definition: normalization_image_generator_node.cpp:195
NormalizationImageNode::image_written_
bool image_written_
Definition: normalization_image_generator_node.cpp:60
NormalizationImageNode::image_cb
void image_cb(const sensor_msgs::ImageConstPtr &msg)
Definition: normalization_image_generator_node.cpp:97
NormalizationImageNode::max_num_to_average_
int32_t max_num_to_average_
Definition: normalization_image_generator_node.cpp:54
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
NormalizationImageNode::filename_
std::string filename_
Definition: normalization_image_generator_node.cpp:55
package.h
image_normalization.h
NormalizationImageNode::generate_and_write_image
void generate_and_write_image()
Definition: normalization_image_generator_node.cpp:123
ros::this_node::getName
const ROSCPP_DECL std::string & getName()
NormalizationImageNode::shut_down
void shut_down()
Definition: normalization_image_generator_node.cpp:166
swri_image_util::generate_normalization_image
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.
Definition: image_normalization.cpp:54
cv_bridge.h
ROS_ERROR
#define ROS_ERROR(...)
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
NormalizationImageNode::subscribe_to_topics
void subscribe_to_topics()
Definition: normalization_image_generator_node.cpp:89
ros::spin
ROSCPP_DECL void spin()
NormalizationImageNode::NormalizationImageNode
NormalizationImageNode(const ros::NodeHandle &nh)
Definition: normalization_image_generator_node.cpp:150
ros::NodeHandle
ros::Subscriber
NormalizationImageNode::num_to_skip_
int32_t num_to_skip_
Definition: normalization_image_generator_node.cpp:53


swri_image_util
Author(s): Kris Kozak
autogenerated on Fri Aug 2 2024 08:39:19