InpaintFilter.cpp
Go to the documentation of this file.
1 /*
2  * InpaintFilter.cpp
3  *
4  * Created on: May 6, 2017
5  * Author: Tanja Baumann, Peter Fankhauser
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
11 #include <ros/ros.h>
12 
13 // Grid Map
15 
16 using namespace filters;
17 
18 namespace grid_map {
19 
20 template<typename T>
22  : radius_(5.0) {
23 
24 }
25 
26 template<typename T>
28 
29 }
30 
31 template<typename T>
33  if (!FilterBase < T > ::getParam(std::string("radius"), radius_)) {
34  ROS_ERROR("InpaintRadius filter did not find param radius.");
35  return false;
36  }
37 
38  if (radius_ < 0.0) {
39  ROS_ERROR("Radius must be greater than zero.");
40  return false;
41  }
42 
43  ROS_DEBUG("Radius = %f.", radius_);
44 
45  if (!FilterBase < T > ::getParam(std::string("input_layer"), inputLayer_)) {
46  ROS_ERROR("Inpaint filter did not find parameter `input_layer`.");
47  return false;
48  }
49 
50  ROS_DEBUG("Inpaint input layer is = %s.", inputLayer_.c_str());
51 
52  if (!FilterBase < T > ::getParam(std::string("output_layer"), outputLayer_)) {
53  ROS_ERROR("Inpaint filter did not find parameter `output_layer`.");
54  return false;
55  }
56 
57  ROS_DEBUG("Inpaint output layer = %s.", outputLayer_.c_str());
58 
59  return true;
60 }
61 
62 template<typename T>
63 bool InpaintFilter<T>::update(const T& mapIn, T& mapOut) {
64  // Add new layer to the elevation map.
65  mapOut = mapIn;
66  mapOut.add(outputLayer_);
67 
68  //Convert elevation layer to OpenCV image to fill in holes.
69  //Get the inpaint mask (nonzero pixels indicate where values need to be filled in).
70  mapOut.add("inpaint_mask", 0.0);
71 
72  mapOut.setBasicLayers(std::vector<std::string>());
73  for (grid_map::GridMapIterator iterator(mapOut); !iterator.isPastEnd(); ++iterator) {
74  if (!mapOut.isValid(*iterator, inputLayer_)) {
75  mapOut.at("inpaint_mask", *iterator) = 1.0;
76  }
77  }
78  cv::Mat originalImage;
79  cv::Mat mask;
80  cv::Mat filledImage;
81  const float minValue = mapOut.get(inputLayer_).minCoeffOfFinites();
82  const float maxValue = mapOut.get(inputLayer_).maxCoeffOfFinites();
83 
84  grid_map::GridMapCvConverter::toImage<unsigned char, 3>(mapOut, inputLayer_, CV_8UC3, minValue, maxValue,
85  originalImage);
86  grid_map::GridMapCvConverter::toImage<unsigned char, 1>(mapOut, "inpaint_mask", CV_8UC1, mask);
87 
88  const double radiusInPixels = radius_ / mapIn.getResolution();
89  cv::inpaint(originalImage, mask, filledImage, radiusInPixels, cv::INPAINT_NS);
90 
91  grid_map::GridMapCvConverter::addLayerFromImage<unsigned char, 3>(filledImage, outputLayer_, mapOut, minValue, maxValue);
92  mapOut.erase("inpaint_mask");
93 
94  return true;
95 }
96 
97 }/* namespace */
98 
std::string outputLayer_
Output layer name.
virtual bool configure()
std::string inputLayer_
Input layer name.
virtual bool update(const T &mapIn, T &mapOut)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
double radius_
Inpainting radius.
#define ROS_DEBUG(...)


grid_map_cv
Author(s): Péter Fankhauser , Magnus Gärtner
autogenerated on Tue Jun 1 2021 02:13:32