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>
46 #include <nlohmann/json.hpp>
52 load(
const std::filesystem::path& directory)
55 const auto filename = directory /
"costmap.json";
58 const nlohmann::json j = nlohmann::json::parse(ifs);
61 const auto& jParam = j.at(
"params");
64 .cellSize = jParam.at(
"cell_size")};
67 const auto& jSceneBounds = j.at(
"scene_bounds");
69 const std::vector<float> boundsMin = jSceneBounds.at(
"min");
70 const std::vector<float> boundsMax = jSceneBounds.at(
"max");
75 const SceneBounds sceneBounds{.
min = {boundsMin.at(0), boundsMin.at(1)},
76 .max = {boundsMax.at(0), boundsMax.at(1)}};
80 const std::string gridFilename = j.at(
"grid_filename");
81 cv::Mat gridMat = cv::imread((directory / gridFilename).
string(),
82 cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
88 std::optional<Costmap::Mask> optMask;
89 if (not j.at(
"mask_filename").empty())
91 const std::string maskFilename = j.at(
"grid_filename");
92 cv::Mat maskMat = cv::imread((directory / maskFilename).
string(),
93 cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
98 optMask = mask.cast<
bool>();
101 return {grid, params, sceneBounds, optMask};
105 save(
const Costmap& costmap,
const std::filesystem::path& directory)
109 ARMARX_INFO <<
"Storing costmap in directory `" << directory.string() <<
"`";
111 if (not std::filesystem::exists(directory))
113 ARMARX_VERBOSE <<
"Creating directory `" << directory.string() <<
"`";
114 std::filesystem::create_directories(directory);
117 nlohmann::json j = {{
"params",
128 std::optional<cv::Mat1b> mask;
129 if (costmap.
getMask().has_value())
133 costmap.
getMask()->cast<std::uint8_t>();
146 const std::string gridFilename =
"grid.exr";
147 cv::imwrite((directory / gridFilename).
string(), grid);
149 j[
"grid_filename"] = gridFilename;
152 const std::string gridDebuggingFilename =
"grid";
158 cv::minMaxIdx(grid, &
min, &
max, 0, 0, mask.value());
162 cv::minMaxIdx(grid, &
min, &
max);
165 grid.convertTo(adjMap, CV_8UC1, 255 / (
max -
min), -255 *
min / (
max -
min));
168 applyColorMap(adjMap, colorsMap, cv::COLORMAP_JET);
169 cv::Mat maskedColorsMap;
172 colorsMap.copyTo(maskedColorsMap, mask.value());
176 maskedColorsMap = colorsMap;
178 cv::imwrite((directory / (gridDebuggingFilename +
".png")).
string(), maskedColorsMap);
185 const std::string maskFilename =
"mask.ppm";
186 cv::imwrite((directory / maskFilename).
string(), mask.value());
188 j[
"mask_filename"] = maskFilename;
193 std::ofstream of(directory /
"costmap.json");