32 #include <opencv2/core.hpp>
33 #include <opencv2/core/eigen.hpp>
34 #include <opencv2/core/hal/interface.h>
35 #include <opencv2/core/mat.hpp>
36 #include <opencv2/imgcodecs.hpp>
37 #include <opencv2/imgproc.hpp>
39 #include <SimoxUtility/json/eigen_conversion.h>
48 #include <nlohmann/json.hpp>
54 load(
const std::filesystem::path& directory)
57 const auto filename = directory /
"costmap.json";
60 const nlohmann::json j = nlohmann::json::parse(ifs);
63 const auto& jParam = j.at(
"params");
66 .cellSize = jParam.at(
"cell_size")};
69 const auto& jSceneBounds = j.at(
"scene_bounds");
71 const std::vector<float> boundsMin = jSceneBounds.at(
"min");
72 const std::vector<float> boundsMax = jSceneBounds.at(
"max");
77 const SceneBounds sceneBounds{.
min = {boundsMin.at(0), boundsMin.at(1)},
78 .max = {boundsMax.at(0), boundsMax.at(1)}};
82 const std::string gridFilename = j.at(
"grid_filename");
83 cv::Mat gridMat = cv::imread((directory / gridFilename).
string(),
84 cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
90 std::optional<Costmap::Mask> optMask;
91 if (not j.at(
"mask_filename").empty())
93 const std::string maskFilename = j.at(
"grid_filename");
94 cv::Mat maskMat = cv::imread((directory / maskFilename).
string(),
95 cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
100 optMask = mask.cast<
bool>();
104 if (not j.at(
"origin").empty())
110 return {grid, params, sceneBounds, optMask, origin};
114 save(
const Costmap& costmap,
const std::filesystem::path& directory)
118 ARMARX_INFO <<
"Storing costmap in directory `" << directory.string() <<
"`";
120 if (not std::filesystem::exists(directory))
122 ARMARX_VERBOSE <<
"Creating directory `" << directory.string() <<
"`";
123 std::filesystem::create_directories(directory);
126 nlohmann::json j = {{
"params",
136 {
"origin", costmap.
origin().matrix()}};
138 std::optional<cv::Mat1b> mask;
139 if (costmap.
getMask().has_value())
143 costmap.
getMask()->cast<std::uint8_t>();
156 const std::string gridFilename =
"grid.exr";
157 cv::imwrite((directory / gridFilename).
string(), grid);
159 j[
"grid_filename"] = gridFilename;
162 const std::string gridDebuggingFilename =
"grid";
168 cv::minMaxIdx(grid, &
min, &
max, 0, 0, mask.value());
172 cv::minMaxIdx(grid, &
min, &
max);
175 grid.convertTo(adjMap, CV_8UC1, 255 / (
max -
min), -255 *
min / (
max -
min));
178 applyColorMap(adjMap, colorsMap, cv::COLORMAP_JET);
179 cv::Mat maskedColorsMap;
182 colorsMap.copyTo(maskedColorsMap, mask.value());
186 maskedColorsMap = colorsMap;
188 cv::imwrite((directory / (gridDebuggingFilename +
".png")).
string(), maskedColorsMap);
195 const std::string maskFilename =
"mask.ppm";
196 cv::imwrite((directory / maskFilename).
string(), mask.value());
198 j[
"mask_filename"] = maskFilename;
203 std::ofstream of(directory /
"costmap.json");