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 TEST(ImageConversion, roundTrip8UC3)
24 {
25  // Create grid map.
26  GridMap mapIn({"layer"});
27  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
28  mapIn["layer"].setRandom();
29  const float minValue = -1.0;
30  const float maxValue = 1.0;
31 
32  // Convert to image.
33  cv::Mat image;
34  GridMapCvConverter::toImage<unsigned char, 3>(mapIn, "layer", CV_8UC3, minValue, maxValue, image);
35 
36  // Convert back to grid map.
37  GridMap mapOut(mapIn);
38  mapOut["layer"].setConstant(NAN);
39  GridMapCvConverter::addLayerFromImage<unsigned char, 3>(image, "layer", mapOut, minValue, maxValue);
40 
41  // Check data.
42  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
43  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
44  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
45  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
46 }
47 
48 TEST(ImageConversion, roundTrip8UC4)
49 {
50  // Create grid map.
51  GridMap mapIn({"layer"});
52  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
53  mapIn["layer"].setRandom();
54  mapIn["layer"](1, 2) = NAN; // To check for transparnecy/nan handling.
55  const float minValue = -1.0;
56  const float maxValue = 1.0;
57 
58  // Convert to image.
59  cv::Mat image;
60  GridMapCvConverter::toImage<unsigned char, 4>(mapIn, "layer", CV_8UC4, minValue, maxValue, image);
61 
62  // Convert back to grid map.
63  GridMap mapOut(mapIn);
64  mapOut["layer"].setConstant(NAN);
65  GridMapCvConverter::addLayerFromImage<unsigned char, 4>(image, "layer", mapOut, minValue, maxValue);
66 
67  // Check data.
68  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
69  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
70  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
71  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
72 }
73 
74 TEST(ImageConversion, roundTrip16UC1)
75 {
76  // Create grid map.
77  GridMap mapIn({"layer"});
78  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
79  mapIn["layer"].setRandom();
80  const float minValue = -1.0;
81  const float maxValue = 1.0;
82 
83  // Convert to image.
84  cv::Mat image;
85  GridMapCvConverter::toImage<unsigned short, 1>(mapIn, "layer", CV_16UC1, minValue, maxValue, image);
86 
87  // Convert back to grid map.
88  GridMap mapOut(mapIn);
89  mapOut["layer"].setConstant(NAN);
90  GridMapCvConverter::addLayerFromImage<unsigned short, 1>(image, "layer", mapOut, minValue, maxValue);
91 
92  // Check data.
93  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
94  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
95  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
96  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
97 }
98 
99 TEST(ImageConversion, roundTrip32FC1)
100 {
101  // Create grid map.
102  GridMap mapIn({"layer"});
103  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
104  mapIn["layer"].setRandom();
105  const float minValue = -1.0;
106  const float maxValue = 1.0;
107 
108  // Convert to image.
109  cv::Mat image;
110  GridMapCvConverter::toImage<float, 1>(mapIn, "layer", CV_32FC1, minValue, maxValue, image);
111 
112  // Convert back to grid map.
113  GridMap mapOut(mapIn);
114  mapOut["layer"].setConstant(NAN);
115  GridMapCvConverter::addLayerFromImage<float, 1>(image, "layer", mapOut, minValue, maxValue);
116 
117  // Check data.
118  const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
119  expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
120  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
121  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
122 }
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
TEST(ImageConversion, roundTrip8UC3)
void expectNear(const M1 &A, const M2 &B, T tolerance, std::string const &message="")
Eigen::Array2d Length


grid_map_cv
Author(s): Péter Fankhauser
autogenerated on Tue Jun 25 2019 20:02:13