0

我正在尝试构建一个数据结构来表示 xtensor 中的 RGB 图像(一个 3D 矩阵,形状为(WIDTH,HEIGHT,3)。每个“像素”包含由像素坐标函数收集的数据。基本上,我想复制这段代码在 python 中的作用:

image = [[cell_info(x, y) for x in range(WIDTH)]
                          for y in range(HEIGHT)]

其中单元格信息返回代表颜色通道的 3 个元素列表。

我想正确的方法应该是使用 xgenerator,但老实说,我无法理解如何使用该类。

4

1 回答 1

0

我找到了一个解决方案:我将 cell_info 更改为接受一个int channel参数,以便它返回一个整数而不是一个数组。然后我写了这个:

class img_generator_fn {
        public:
          using value_type = int;
          img_generator_fn(const Map *map, const Position &center, shared_ptr<const Player> player,
                           const unsigned int field_radius)
              : m_map(map), m_center(center), m_player(player), m_translation(-(field_radius + 1)) {}
          ~img_generator_fn() { m_map = nullptr; }
          inline auto operator()(const unsigned int x, const unsigned int y, const unsigned int channel) const {
             return m_map->at(m_center + Position(x, y))->cell_info(m_player, channel);
          }
          template <class It> inline auto element(It, It end) const {
             return m_map->at(m_center + Position(*(end - 2) + m_translation, (*(end - 3)) + m_translation))
                 ->cell_info(m_player, *(end - 1));
          }

        private:
          const Map *m_map;
          const Position &m_center;
          shared_ptr<const Player> m_player;
          const unsigned int m_translation;
       };

       template <unsigned int field_side> auto field(const Position &center, shared_ptr<const Player> player) const {
          const array<unsigned int, 3> shape = {field_side, field_side, 3};
          auto gen = xt::detail::make_xgenerator(img_generator_fn(this, center, player, (field_side - 1) / 2), shape);
          return xt::xtensor_fixed<int, xt::xshape<field_side, field_side, 3>>(gen);
       }

这里Map表示一个 2D 矩阵,它是包含player我想要存储在图像中的信息的结构。该函数at在指定位置拾取地图单元格(地图单元格将转换为像素)。该函数使用 xgeneratorfield生成以给定地图为中心的图像。center

于 2019-01-21T16:56:14.283 回答