hector_barrel_geotiff_plugin.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE 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 #include <pcl/ModelCoefficients.h>
29 #include <pcl/io/pcd_io.h>
30 #include <pcl/point_types.h>
31 #include <pcl/filters/extract_indices.h>
32 #include <pcl/filters/passthrough.h>
33 #include <pcl/features/normal_3d.h>
34 #include <pcl/sample_consensus/method_types.h>
35 #include <pcl/sample_consensus/model_types.h>
36 #include <pcl/segmentation/sac_segmentation.h>
37 
38 
41 
42 #include <ros/ros.h>
43 #include <nav_msgs/GetMap.h>
44 
45 #include <pluginlib/class_loader.h>
46 #include <fstream>
47 
48 #include <boost/date_time/posix_time/posix_time.hpp>
49 #include <boost/date_time/gregorian/formatters.hpp>
50 
51 #include <boost/tokenizer.hpp>
52 
53 #include <opencv/highgui.h>
54 #include<opencv/cv.h>
55 
56 #include <hector_worldmodel_msgs/GetObjectModel.h>
57 
59 
60 using namespace hector_geotiff;
61 
63 {
64 public:
66  virtual ~SemanticMapWriterPlugin();
67 
68  virtual void initialize(const std::string& name);
69  virtual void draw(MapWriterInterface *interface) = 0;
70 
71 protected:
72 
76 
77 
79  std::string name_;
81  std::string class_id_;
82  std::string pkg_path;
84 };
85 
87  : initialized_(false)
88 {}
89 
91 {}
92 
93 void SemanticMapWriterPlugin::initialize(const std::string& name)
94 {
95  ros::NodeHandle plugin_nh("~/" + name);
96  std::string service_name_;
97 
98  plugin_nh.param("service_name", service_name_, std::string("worldmodel/get_object_model"));
99  plugin_nh.param("draw_all_objects", draw_all_objects_, false);
100  plugin_nh.param("class_id", class_id_, std::string());
101 
102  service_client_ = nh_.serviceClient<hector_worldmodel_msgs::GetObjectModel>(service_name_);
103 
104  initialized_ = true;
105  this->name_ = name;
106  ROS_INFO_NAMED(name_, "Successfully initialized hector_geotiff MapWriter plugin %s.", name_.c_str());
107 }
108 
110 {
111 public:
112  virtual ~BarrelMapWriter() {}
113 
114  void draw(MapWriterInterface *interface)
115  {
116  if (!initialized_) return;
117 
118  hector_worldmodel_msgs::GetObjectModel data;
119  if (!service_client_.call(data)) {
120  ROS_ERROR_NAMED(name_, "Cannot draw victims, service %s failed", service_client_.getService().c_str());
121  return;
122  }
123 
124  int counter =0;
125  for(hector_worldmodel_msgs::ObjectModel::_objects_type::const_iterator it = data.response.model.objects.begin(); it != data.response.model.objects.end(); ++it) {
126  const hector_worldmodel_msgs::Object& object = *it;
127  if (!class_id_.empty() && object.info.class_id != class_id_) continue;
128  Eigen::Vector2f coords;
129  coords.x() = object.pose.pose.position.x;
130  coords.y() = object.pose.pose.position.y;
131  interface->drawObjectOfInterest(coords, boost::lexical_cast<std::string>(++counter), MapWriterInterface::Color(180,0,200));
132 
133 
134  }
135  }
136 };
137 
138 //class BarrelMapWriter : public SemanticMapWriterPlugin
139 //{
140 //public:
141 // virtual ~BarrelMapWriter() {}
142 
143 // void draw(MapWriterInterface *interface)
144 // {
145 // if (!initialized_) return;
146 
147 // nav_msgs::GetMap map_barrels;
148 // nav_msgs::GetMap map_no_barrels;
149 // if (!service_client_.call(map_barrels)) {
150 // ROS_ERROR_NAMED(name_, "Cannot draw barrels, service %s failed", service_client_.getService().c_str());
151 // return;
152 // }
153 
154 
155 // cv::Mat cv_map_small, cv_map;
156 
157 // cv_map_small= occupancyGridToCvMat(&map_barrels.response.map);
158 
159 // cv::Mat cv_map_small_bw;
160 // cv::threshold( cv_map_small, cv_map_small_bw, 0.1, 255, 0);
161 
162 
163 // cv::copyMakeBorder( cv_map_small_bw, cv_map, 50, 50, 50, 50, cv::BORDER_CONSTANT, cv::Scalar(255) );
164 
165 
166 // cv::imshow("map_big", cv_map);
167 
168 // cv::imwrite(pkg_path+"/templates/map.png",cv_map);
169 // cv::waitKey(2000);
170 // cv::Mat barrel_template = cv::imread(pkg_path+"/templates/barrel_3.png");
171 
172 // cv::Mat gref, gtpl, tmpl_gray;
173 // cv::cvtColor(barrel_template, tmpl_gray, CV_BGR2GRAY);
174 
175 
176 // gref = getSobel(cv_map);
177 
178 // ROS_WARN("blablabla ");
179 // cv::Mat ref = gref;
180 // gtpl = getSobel(tmpl_gray);
181 // ROS_WARN("blublublu ");
182 // cv::Mat tpl = gtpl;
185 
186 // if (ref.empty() || tpl.empty()) {
187 // ROS_WARN("Empty map layer for barrels");
188 // return;
189 // }
190 // else{
191 
192 // cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
193 // cv::matchTemplate(gref, gtpl, res, CV_TM_SQDIFF_NORMED);
194 
195 // cv::threshold(res, res, barrel_threshold, 1., CV_THRESH_TOZERO);
196 
197 // std::vector<cv::Point> barrel_locations;
198 // while (true)
199 // {
200 // double minval, maxval, threshold = barrel_threshold;
201 // cv::Point minloc, maxloc;
202 // cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
203 
204 // ROS_INFO("max %d, %d",maxloc.x,maxloc.y);
205 // if (maxval >= threshold)
206 // {
207 // cv::rectangle(
208 // ref,
209 // maxloc,
210 // cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows),
211 // cv::Scalar(255,0,0), 2
212 // );
213 // barrel_locations.push_back(maxloc);
214 // cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
215 // }
216 // else
217 // break;
218 // }
219 // std::cout << barrel_locations <<std::endl;
220 // //cv::namedWindow( "reference", CV_WINDOW_AUTOSIZE );
221 
222 // cv::imshow("result", ref);
223 // cv::waitKey(2000);
224 // int counter = 0;
225 // double pixelsPerMapMeter = 1.0f / map_barrels.response.map.info.resolution;
226 
227 // Eigen::Vector2f origin = Eigen::Vector2f(map_barrels.response.map.info.origin.position.x, map_barrels.response.map.info.origin.position.y);
228 // std::cout<< "origin :" <<origin <<std::endl;
229 // for (int i=0;i<barrel_locations.size();i++){
230 // cv::Point barrel_point = barrel_locations.at(i);
231 // Eigen::Vector2f coords;
232 // coords.x() = (barrel_point.x+50)/pixelsPerMapMeter;
233 // coords.y() = (barrel_point.y+50)/pixelsPerMapMeter;
234 // interface->drawObjectOfInterest(coords, boost::lexical_cast<std::string>(++counter), MapWriterInterface::Color(0,255,0));
235 
236 // std::cout<< "pixels per map !!!!!!!!!!!!!!!!!!!!!!!:"<<pixelsPerMapMeter <<std::endl;
237 // std::cout<< "coords !!!!!!!!!!!!!!!!!!!!!!!:"<<coords <<std::endl;
238 // interface->drawObjectOfInterest(coords, boost::lexical_cast<std::string>(++counter), MapWriterInterface::Color(0,255,0));
239 
240 // }
241 // }
242 // }
243 
244 // cv::Mat getSobel(cv::Mat src_gray){
245 
246 // cv::Mat grad;
247 
248 // int scale = 1;
249 // int delta = 0;
250 // int ddepth = CV_16S;
251 
252 // int c;
253 
254 // cv::GaussianBlur( src_gray, src_gray, cv::Size(3,3), 0, 0, cv::BORDER_DEFAULT );
255 
256 // /// Convert it to gray
257 
258 // /// Create window
259 
260 
261 // /// Generate grad_x and grad_y
262 // cv::Mat grad_x, grad_y;
263 // cv::Mat abs_grad_x, abs_grad_y;
264 
265 // /// Gradient X
266 // //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
267 // cv::Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, cv::BORDER_DEFAULT );
268 // convertScaleAbs( grad_x, abs_grad_x );
269 
270 // /// Gradient Y
271 // //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
272 // Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, cv::BORDER_DEFAULT );
273 // convertScaleAbs( grad_y, abs_grad_y );
274 
275 // /// Total Gradient (approximate)
276 // cv::addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
277 // ROS_WARN("gradient ");
278 // // cv::imshow( "sobel", grad );
279 // return grad;
280 // }
281 
282 // cv::Mat occupancyGridToCvMat(const nav_msgs::OccupancyGrid *map)
283 // {
284 // uint8_t *data = (uint8_t*) map->data.data(),
285 // testpoint = data[0];
286 // bool mapHasPoints = false;
287 
288 // cv::Mat im(map->info.height, map->info.width, CV_8UC1);
289 
290 // // transform the map in the same way the map_saver component does
291 // for (size_t i=0; i<map->data.size(); i++)
292 // {
293 // if (data[i] == 0) im.data[i] = 254;
294 // else if (data[i] == 100) im.data[i] = 0;
295 // else im.data[i] = 205;
296 
297 // // just check if there is actually something in the map
298 // if (i!=0) mapHasPoints = mapHasPoints || (data[i] != testpoint);
299 // testpoint = data[i];
300 // }
301 
302 // // sanity check
303 // if (!mapHasPoints) { ROS_WARN("map is empty, ignoring update."); }
304 
305 // return im;
306 // }
307 //};
308 
309 
310 
311 } // namespace
312 
313 //register this planner as a MapWriterPluginInterface plugin
316 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
#define ROS_INFO_NAMED(name,...)
ROSCONSOLE_DECL void initialize()
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
bool call(MReq &req, MRes &res)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
virtual void drawObjectOfInterest(const Eigen::Vector2f &coords, const std::string &txt, const Color &color)=0
#define ROS_ERROR_NAMED(name,...)
std::string getService()


hector_barrel_geotiff_plugin
Author(s):
autogenerated on Mon Jun 10 2019 13:36:30