39 const double lengthX = resolution * image.rows;
40 const double lengthY = resolution * image.cols;
41 Length length(lengthX, lengthY);
58 template<
typename Type_,
int NChannels_>
61 const float upperValue = 1.0,
const double alphaThreshold = 0.5)
63 if (gridMap.
getSize()(0) != image.rows || gridMap.
getSize()(1) != image.cols) {
64 std::cerr <<
"Image size does not correspond to grid map size!" << std::endl;
69 if (image.channels() >= 3) isColor =
true;
70 bool hasAlpha =
false;
71 if (image.channels() >= 4) hasAlpha =
true;
74 if (isColor && !hasAlpha) {
75 cv::cvtColor(image, imageMono, CV_BGR2GRAY);
76 }
else if (isColor && hasAlpha) {
77 cv::cvtColor(image, imageMono, CV_BGRA2GRAY);
78 }
else if (!isColor && !hasAlpha){
81 std::cerr <<
"Something went wrong when adding grid map layer form image!" << std::endl;
85 const float mapValueDifference = upperValue - lowerValue;
88 if (std::is_same<Type_, float>::value || std::is_same<Type_, double>::value) {
90 }
else if (std::is_same<Type_, unsigned short>::value || std::is_same<Type_, unsigned char>::value) {
91 maxImageValue = (float)std::numeric_limits<Type_>::max();
93 std::cerr <<
"This image type is not supported." << std::endl;
97 const Type_ alphaTreshold = (Type_)(alphaThreshold * maxImageValue);
103 const Index index(*iterator);
107 const Type_ alpha = image.at<cv::Vec<Type_, NChannels_>>(index(0), index(1))[NChannels_ - 1];
108 if (alpha < alphaTreshold)
continue;
112 const Type_ imageValue = imageMono.at<Type_>(index(0), index(1));
113 const float mapValue = lowerValue + mapValueDifference * ((float) imageValue / maxImageValue);
114 data(index(0), index(1)) = mapValue;
127 template<
typename Type_,
int NChannels_>
131 if (gridMap.
getSize()(0) != image.rows || gridMap.
getSize()(1) != image.cols) {
132 std::cerr <<
"Image size does not correspond to grid map size!" << std::endl;
136 bool hasAlpha =
false;
137 if (image.channels() >= 4) hasAlpha =
true;
141 cv::cvtColor(image, imageRGB, CV_BGRA2RGB);
149 const auto& cvColor = imageRGB.at<cv::Vec<Type_, 3>>((*iterator)(0), (*iterator)(1));
150 Eigen::Vector3i colorVector;
151 colorVector(0) = cvColor[0];
152 colorVector(1) = cvColor[1];
153 colorVector(2) = cvColor[2];
172 template<
typename Type_,
int NChannels_>
174 const int encoding, cv::Mat& image)
176 const float minValue = gridMap.
get(layer).minCoeffOfFinites();
177 const float maxValue = gridMap.
get(layer).maxCoeffOfFinites();
178 return toImage<Type_, NChannels_>(gridMap, layer, encoding, minValue, maxValue, image);
191 template<
typename Type_,
int NChannels_>
193 const int encoding,
const float lowerValue,
const float upperValue,
198 image = cv::Mat::zeros(gridMap.
getSize()(0), gridMap.
getSize()(1), encoding);
200 std::cerr <<
"Invalid grid map?" << std::endl;
206 if (std::is_same<Type_, float>::value || std::is_same<Type_, double>::value) {
208 }
else if (std::is_same<Type_, unsigned short>::value || std::is_same<Type_, unsigned char>::value) {
209 imageMax = (Type_)std::numeric_limits<Type_>::max();
211 std::cerr <<
"This image type is not supported." << std::endl;
221 bool isColor =
false;
222 if (image.channels() >= 3) isColor =
true;
223 bool hasAlpha =
false;
224 if (image.channels() >= 4) hasAlpha =
true;
227 const Index index(*iterator);
228 if (std::isfinite(data(index(0), index(1)))) {
229 const float& value = data(index(0), index(1));
230 const Type_ imageValue = (Type_) (((value - lowerValue) / (upperValue - lowerValue)) * (float) imageMax);
231 const Index imageIndex(iterator.getUnwrappedIndex());
232 unsigned int channel = 0;
233 image.at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[channel] = imageValue;
236 image.at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[++channel] = imageValue;
237 image.at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[++channel] = imageValue;
240 image.at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[++channel] = imageMax;
static bool toImage(const grid_map::GridMap &gridMap, const std::string &layer, const int encoding, const float lowerValue, const float upperValue, cv::Mat &image)
static bool toImage(const grid_map::GridMap &gridMap, const std::string &layer, const int encoding, cv::Mat &image)
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
static bool addColorLayerFromImage(const cv::Mat &image, const std::string &layer, grid_map::GridMap &gridMap)
static bool initializeFromImage(const cv::Mat &image, const double resolution, grid_map::GridMap &gridMap, const grid_map::Position &position)
static bool addLayerFromImage(const cv::Mat &image, const std::string &layer, grid_map::GridMap &gridMap, const float lowerValue=0.0, const float upperValue=1.0, const double alphaThreshold=0.5)
bool colorVectorToValue(const Eigen::Vector3i &colorVector, unsigned long &colorValue)
const Matrix & get(const std::string &layer) const
float & at(const std::string &layer, const Index &index)
void add(const std::string &layer, const double value=NAN)
const Size & getSize() const