GridMapCvTest.cpp
Go to the documentation of this file.
00001 /*
00002  * GridMapCvTest.cpp
00003  *
00004  *  Created on: Apr 14, 2016
00005  *      Author: Peter Fankhauser, Martin Wermelinger
00006  *       Institute: ETH Zurich, Autonomous Systems Lab
00007  */
00008 
00009 #include "grid_map_cv/grid_map_cv.hpp"
00010 
00011 #include <grid_map_core/grid_map_core.hpp>
00012 #include <grid_map_core/gtest_eigen.hpp>
00013 
00014 // gtest
00015 #include <gtest/gtest.h>
00016 
00017 // OpenCV
00018 #include <cv_bridge/cv_bridge.h>
00019 
00020 using namespace std;
00021 using namespace grid_map;
00022 
00023 TEST(ImageConversion, roundTrip8UC3)
00024 {
00025   // Create grid map.
00026   GridMap mapIn({"layer"});
00027   mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
00028   mapIn["layer"].setRandom();
00029   const float minValue = -1.0;
00030   const float maxValue = 1.0;
00031 
00032   // Convert to image.
00033   cv::Mat image;
00034   GridMapCvConverter::toImage<unsigned char, 3>(mapIn, "layer", CV_8UC3, minValue, maxValue, image);
00035 
00036   // Convert back to grid map.
00037   GridMap mapOut(mapIn);
00038   mapOut["layer"].setConstant(NAN);
00039   GridMapCvConverter::addLayerFromImage<unsigned char, 3>(image, "layer", mapOut, minValue, maxValue);
00040 
00041   // Check data.
00042   const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
00043   expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
00044   EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
00045   EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
00046 }
00047 
00048 TEST(ImageConversion, roundTrip8UC4)
00049 {
00050   // Create grid map.
00051   GridMap mapIn({"layer"});
00052   mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.1);
00053   mapIn["layer"].setRandom();
00054   mapIn["layer"](1, 2) = NAN; // To check for transparnecy/nan handling.
00055   const float minValue = -1.0;
00056   const float maxValue = 1.0;
00057 
00058   // Convert to image.
00059   cv::Mat image;
00060   GridMapCvConverter::toImage<unsigned char, 4>(mapIn, "layer", CV_8UC4, minValue, maxValue, image);
00061 
00062   // Convert back to grid map.
00063   GridMap mapOut(mapIn);
00064   mapOut["layer"].setConstant(NAN);
00065   GridMapCvConverter::addLayerFromImage<unsigned char, 4>(image, "layer", mapOut, minValue, maxValue);
00066 
00067   // Check data.
00068   const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
00069   expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
00070   EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
00071   EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
00072 }
00073 
00074 TEST(ImageConversion, roundTrip16UC1)
00075 {
00076   // Create grid map.
00077   GridMap mapIn({"layer"});
00078   mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
00079   mapIn["layer"].setRandom();
00080   const float minValue = -1.0;
00081   const float maxValue = 1.0;
00082 
00083   // Convert to image.
00084   cv::Mat image;
00085   GridMapCvConverter::toImage<unsigned short, 1>(mapIn, "layer", CV_16UC1, minValue, maxValue, image);
00086 
00087   // Convert back to grid map.
00088   GridMap mapOut(mapIn);
00089   mapOut["layer"].setConstant(NAN);
00090   GridMapCvConverter::addLayerFromImage<unsigned short, 1>(image, "layer", mapOut, minValue, maxValue);
00091 
00092   // Check data.
00093   const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
00094   expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
00095   EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
00096   EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
00097 }
00098 
00099 TEST(ImageConversion, roundTrip32FC1)
00100 {
00101   // Create grid map.
00102   GridMap mapIn({"layer"});
00103   mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
00104   mapIn["layer"].setRandom();
00105   const float minValue = -1.0;
00106   const float maxValue = 1.0;
00107 
00108   // Convert to image.
00109   cv::Mat image;
00110   GridMapCvConverter::toImage<float, 1>(mapIn, "layer", CV_32FC1, minValue, maxValue, image);
00111 
00112   // Convert back to grid map.
00113   GridMap mapOut(mapIn);
00114   mapOut["layer"].setConstant(NAN);
00115   GridMapCvConverter::addLayerFromImage<float, 1>(image, "layer", mapOut, minValue, maxValue);
00116 
00117   // Check data.
00118   const float resolution = (maxValue - minValue) / (float) std::numeric_limits<unsigned char>::max();
00119   expectNear(mapIn["layer"], mapOut["layer"], resolution, "");
00120   EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
00121   EXPECT_TRUE((mapIn.getSize() == mapOut.getSize()).all());
00122 }


grid_map_cv
Author(s): Péter Fankhauser
autogenerated on Mon Oct 9 2017 03:09:25