【问题标题】:Build neighboring vertices in a boost adjacency graph knowing the connectivity在知道连通性的增强邻接图中构建相邻顶​​点
【发布时间】:2014-04-22 16:02:22
【问题描述】:

我的 boost 图形库有问题。

在初始化步骤,我想在这个邻接图中存储一个图像:

typedef boost::adjacency_list<boost::vecS,
                              boost::vecS,
                              boost::undirectedS,
                              boost::property<boost::vertex_bundle_t, unsigned int>,
                              boost::property<boost::edge_bundle_t, float>,
                              boost::no_property> GraphType

我做了第一个循环,为图像的每个像素创建一个顶点(使用 OTB 库处理图像,in_iter 只是图像像素上的迭代器):

unsigned long int curr_id = 0;
for(in_iter.GoToBegin(); !in_iter.IsAtEnd(); ++in_iter)
{
    boost::add_vertex(curr_id, *_graph);
    curr_id++;
}

现在我想为一个顶点创建它与其邻居的连接(4 个连接): 我试过了,但它不起作用:

curr_id = 0;
long int neighbors[4];
auto iterBounds = boost::vertices(*_graph);
for(auto v_iter = iterBounds.first; v_iter != iterBounds.second; v_iter++)
{
  FindNeighboring(curr_id, neighbors, rows, cols);
  for(short j = 0; j<4; j++)
  { 
    if(neighbors[j] > -1)
    {
      boost::add_vertex(*_graph->m_vertices[curr_id], 
                        *_graph->m_vertices[neighbors[j]],
                        std::numeric_limits<float>::max();
                        *_graph);
    }
  }
}

我想访问知道其位置的顶点描述符。 是否可以在 BGL 中做到这一点?

感谢您的帮助!

【问题讨论】:

    标签: c++ image-processing boost-graph


    【解决方案1】:

    答案是“是和不是”。

    如果您为图形使用通用adjacency_list,那么当然,您必须自己维护对应关系&lt;coords&gt; &lt;--&gt; &lt;vertex&gt;。您可以将其保留为 boost::unordered_map 提供的映射,也可以保留为 boost::bimap 甚至 std::map。您有责任填充地图并使其与图表保持一致。

    或者,您可以考虑 BGL 内置类grid_graph。它为您提供了从坐标到顶点描述符的映射:

    // Get the vertex associated with vertex_index
    Traits::vertex_descriptor
    vertex(Traits::vertices_size_type vertex_index,
           const Graph& graph);
    
    // Get the index associated with vertex
    Traits::vertices_size_type
    get(boost::vertex_index_t,
        const Graph& graph,
        Traits::vertex_descriptor vertex);
    

    (以及边缘的类似功能)。

    这里您不需要创建单独的顶点,它们是由 BGL 在您指定图形尺寸的那一刻在图形构造函数中创建的。

    您可能希望使用某种自定义属性映射将像素与每个顶点相关联,但这是一个单独的故事。

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多