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);
108 const Type_ alpha = image.
at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[NChannels_ - 1];
109 if (alpha < alphaTreshold)
continue;
113 const Type_ imageValue = imageMono.at<Type_>(imageIndex(0), imageIndex(1));
114 const float mapValue = lowerValue + mapValueDifference * ((float) imageValue / maxImageValue);
115 data(gridMapIndex(0), gridMapIndex(1)) = mapValue;
128 template<
typename Type_,
int NChannels_>
132 if (gridMap.
getSize()(0) != image.rows || gridMap.
getSize()(1) != image.cols) {
133 std::cerr <<
"Image size does not correspond to grid map size!" << std::endl;
137 bool hasAlpha =
false;
138 if (image.channels() >= 4) hasAlpha =
true;
142 cv::cvtColor(image, imageRGB, CV_BGRA2RGB);
150 const auto& cvColor = imageRGB.at<cv::Vec<Type_, 3>>((*iterator)(0), (*iterator)(1));
151 Eigen::Vector3i colorVector;
152 colorVector(0) = cvColor[0];
153 colorVector(1) = cvColor[1];
154 colorVector(2) = cvColor[2];
173 template<
typename Type_,
int NChannels_>
175 const int encoding, cv::Mat& image)
177 const float minValue = gridMap.
get(layer).minCoeffOfFinites();
178 const float maxValue = gridMap.
get(layer).maxCoeffOfFinites();
179 return toImage<Type_, NChannels_>(gridMap, layer, encoding, minValue, maxValue, image);
192 template<
typename Type_,
int NChannels_>
194 const int encoding,
const float lowerValue,
const float upperValue,
199 image = cv::Mat::zeros(gridMap.
getSize()(0), gridMap.
getSize()(1), encoding);
201 std::cerr <<
"Invalid grid map?" << std::endl;
207 if (std::is_same<Type_, float>::value || std::is_same<Type_, double>::value) {
209 }
else if (std::is_same<Type_, unsigned short>::value || std::is_same<Type_, unsigned char>::value) {
210 imageMax = (Type_)std::numeric_limits<Type_>::max();
212 std::cerr <<
"This image type is not supported." << std::endl;
222 bool isColor =
false;
223 if (image.channels() >= 3) isColor =
true;
224 bool hasAlpha =
false;
225 if (image.channels() >= 4) hasAlpha =
true;
228 const Index index(*iterator);
229 const float& value = data(index(0), index(1));
230 if (std::isfinite(value)) {
231 const Type_ imageValue = (Type_)(((value - lowerValue) / (upperValue - lowerValue)) * (float)imageMax);
232 const Index imageIndex(iterator.getUnwrappedIndex());
233 unsigned int channel = 0;
234 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;
238 image.at<cv::Vec<Type_, NChannels_>>(imageIndex(0), imageIndex(1))[++channel] = imageValue;
241 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