ARTICLE AD BOX
I have a matrix and I can extract color regions as std::vector<std::pair<int, int> > and I want to convert them into Polygon Boost objects. I have yet no idea how to proceed.
Here is a matrix example with a region of 3 and 0 and the matching searched polygon.

Here is a sample code:
#include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geometries/polygon.hpp> #include <Eigen/Core> template <typename Matrix> bool validIndex(Matrix const& a, std::pair<int, int> const& at) { return (0 <= at.first && at.first < a.rows() && 0 <= at.second && at.second < a.cols()); } inline std::vector<std::pair<int, int> > neighbors(std::pair<int, int> const& loc, std::pair<int, int> const& size, bool diagonals) { std::vector<std::pair<int, int> > n; for (int di = -1; di <= 1; ++di) { for (int dj = -1; dj <= 1; ++dj) { if (!di && !dj) continue; auto const i{loc.first + di}; auto const j{loc.second + dj}; if (!(0 <= i && i < size.first && 0 <= j && j < size.second)) continue; if (!diagonals && std::abs(di) == std::abs(dj)) continue; n.emplace_back(std::make_pair(i, j)); } } return n; } template <typename Matrix> std::vector<std::pair<int, int> > region(Matrix const& a, std::pair<int, int> const& at, bool diagonals) { if (!validIndex(a, at)) return std::vector<std::pair<int, int> >{}; std::set<std::pair<int, int> > s; std::set<std::pair<int, int> > stack; stack.emplace(at); auto const v{a(at.first, at.second)}; std::vector<std::pair<int, int> > indices; while (stack.size()) { auto const loc{*stack.begin()}; stack.erase(stack.begin()); if (s.find(loc) == s.end()) { s.emplace(loc); if (std::abs(a(loc.first, loc.second) - v) < 1.0e-6) { indices.emplace_back(loc); for (auto const& n : neighbors(loc, std::make_pair((int)a.rows(), (int)a.cols()), diagonals)) stack.emplace(n); } } } return indices; } template <typename Matrix> std::set<std::vector<std::pair<int, int> > > regionSet(Matrix const& m, bool diagonals) { std::set<std::vector<std::pair<int, int> > > s; for (int i{0}; i < m.rows(); ++i) { for (int j{0}; j < m.cols(); ++j) { auto r{region(m, std::make_pair(i, j), diagonals)}; std::sort(r.begin(), r.end(), [] (auto const& x, auto const& y) -> bool { if (x.first == y.first) return x.second < y.second; return x.first < y.first; }); if (s.find(r) == s.end()) s.emplace(r); } } return s; } using Point = boost::geometry::model::d2::point_xy<int>; using Polygon = boost::geometry::model::polygon<Point>; Polygon regionToPolygon(std::vector<std::pair<int, int> > const& region) { //... } int main() { Eigen::MatrixXi input(10, 10); input << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 3, 3, 0, 0, 3, 0, 0, 3, 0, 0, 3, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 3, 3, 3, 0, 0, 3, 0, 3, 0, 0, 3, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0; auto const s{regionSet(input, false)}; std::map<int, std::vector<std::vector<std::pair<int, int> > > > regions; for (int i{0}; i < 10; ++i) regions[i].clear(); for (auto const& r : s) regions[input(r.front().first, r.front().second)].emplace_back(r); auto const polygon1{regionToPolygon(regions.at(3)[0])}; auto const polygon2{regionToPolygon(regions.at(3)[1])}; return 0; }