GridMapCvTest.cpp
Go to the documentation of this file.
1 /*
2  * GridMapCvTest.cpp
3  *
4  * Created on: Apr 14, 2016
5  * Author: Peter Fankhauser, Martin Wermelinger
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
10 
13 
14 // gtest
15 #include <gtest/gtest.h>
16 
17 // OpenCV
18 #include <cv_bridge/cv_bridge.h>
19 
20 using namespace std;
21 using namespace grid_map;
22 
23 void replaceNan(Matrix& m, const double newValue)
24 {
25  for(int r = 0; r < m.rows(); r++)
26  {
27  for(int c = 0; c < m.cols(); c++)
28  {
29  if (std::isnan(m(r,c)))
30  {
31  m(r,c) = newValue;
32  }
33  }
34  }
35 }
36 
37 TEST(ImageConversion, roundTrip8UC3)
38 {
39  // Create grid map.
40  GridMap mapIn({"layer"});
41  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
42  mapIn["layer"].setRandom(); // Sets the layer to random values in [-1.0, 1.0].
43  mapIn.move(Position(0.5, -0.2));
44  const float minValue = -1.0;
45  const float maxValue = 1.0;
46  replaceNan(mapIn.get("layer"), minValue); // When we move `mapIn`, new areas are filled with NaN. As `toImage` does not support NaN, we replace NaN with `minValue` instead.
47 
48  // Convert to image.
49  cv::Mat image;
50  GridMapCvConverter::toImage<unsigned char, 3>(mapIn, "layer", CV_8UC3, minValue, maxValue, image);
51 
52  // Convert back to grid map.
53  GridMap mapOut(mapIn);
54  mapOut["layer"].setConstant(NAN);
55  GridMapCvConverter::addLayerFromImage<unsigned char, 3>(image, "layer", mapOut, minValue, maxValue);
56 
57  // Check data.
58  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
59  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
60  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
61  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
62 }
63 
64 TEST(ImageConversion, roundTrip8UC4)
65 {
66  // Create grid map.
67  GridMap mapIn({"layer"});
68  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
69  mapIn["layer"].setRandom(); // Sets the layer to random values in [-1.0, 1.0].
70  mapIn.move(Position(0.5, -0.2));
71  const float minValue = -1.0;
72  const float maxValue = 1.0;
73  replaceNan(mapIn.get("layer"), minValue); // When we move `mapIn`, new areas are filled with NaN. As `toImage` does not support NaN, we replace NaN with `minValue` instead.
74 
75  // Convert to image.
76  cv::Mat image;
77  GridMapCvConverter::toImage<unsigned char, 4>(mapIn, "layer", CV_8UC4, minValue, maxValue, image);
78 
79  // Convert back to grid map.
80  GridMap mapOut(mapIn);
81  mapOut["layer"].setConstant(NAN);
82  GridMapCvConverter::addLayerFromImage<unsigned char, 4>(image, "layer", mapOut, minValue, maxValue);
83 
84  // Check data.
85  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
86  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
87  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
88  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
89 }
90 
91 TEST(ImageConversion, roundTrip16UC1)
92 {
93  // Create grid map.
94  GridMap mapIn({"layer"});
95  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
96  mapIn["layer"].setRandom(); // Sets the layer to random values in [-1.0, 1.0].
97  mapIn.move(Position(0.5, -0.2));
98  const float minValue = -1.0;
99  const float maxValue = 1.0;
100  replaceNan(mapIn.get("layer"), minValue); // When we move `mapIn`, new areas are filled with NaN. As `toImage` does not support NaN, we replace NaN with `minValue` instead.
101 
102  // Convert to image.
103  cv::Mat image;
104  GridMapCvConverter::toImage<unsigned short, 1>(mapIn, "layer", CV_16UC1, minValue, maxValue, image);
105 
106  // Convert back to grid map.
107  GridMap mapOut(mapIn);
108  mapOut["layer"].setConstant(NAN);
109  GridMapCvConverter::addLayerFromImage<unsigned short, 1>(image, "layer", mapOut, minValue, maxValue);
110 
111  // Check data.
112  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
113  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
114  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
115  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
116 }
117 
118 TEST(ImageConversion, roundTrip32FC1)
119 {
120  // Create grid map.
121  GridMap mapIn({"layer"});
122  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
123  mapIn["layer"].setRandom(); // Sets the layer to random values in [-1.0, 1.0].
124  mapIn.move(Position(0.5, -0.2));
125  const float minValue = -1.0;
126  const float maxValue = 1.0;
127  replaceNan(mapIn.get("layer"), minValue); // When we move `mapIn`, new areas are filled with NaN. As `toImage` does not support NaN, we replace NaN with `minValue` instead.
128 
129  // Convert to image.
130  cv::Mat image;
131  GridMapCvConverter::toImage<float, 1>(mapIn, "layer", CV_32FC1, minValue, maxValue, image);
132 
133  // Convert back to grid map.
134  GridMap mapOut(mapIn);
135  mapOut["layer"].setConstant(NAN);
136  GridMapCvConverter::addLayerFromImage<float, 1>(image, "layer", mapOut, minValue, maxValue);
137 
138  // Check data.
139  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
140  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
141  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
142  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
143 }
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
Eigen::MatrixXf Matrix
void replaceNan(Matrix &m, const double newValue)
TEST(ImageConversion, roundTrip8UC3)
void expectNear(const M1 &A, const M2 &B, T tolerance, std::string const &message="")
Eigen::Vector2d Position
Eigen::Array2d Length


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