反转用于增强的边贴图::boykov_kolmogorov_max_flow
Reverse edge map for boost::boykov_kolmogorov_max_flow
我正在尝试使用boost::boykov_kolmogorov_max_flow来分割图像,使用标准技术从图像上的网格图开始,然后添加每个网格顶点都连接到的"特殊"源和汇节点。
我为2x2图像(总共2*2+2=6个节点)构建了这个图,以表示最基本的情况,只是为了让Boost类型达成一致。我想出了这个:
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/boykov_kolmogorov_max_flow.hpp>
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
boost::no_property,
boost::property<boost::edge_index_t, std::size_t> > GraphType;
typedef boost::graph_traits<GraphType>::vertex_descriptor VertexDescriptor;
typedef boost::graph_traits<GraphType>::edge_descriptor EdgeDescriptor;
typedef boost::graph_traits<GraphType>::vertices_size_type VertexIndex;
typedef boost::graph_traits<GraphType>::edges_size_type EdgeIndex;
void AddBidirectionalEdge(GraphType& graph, unsigned int source, unsigned int target, float weight,
std::vector<EdgeDescriptor>& reverseEdges, std::vector<float>& capacity)
{
// Add edges between grid vertices. We have to create the edge and the reverse edge,
// then add the reverseEdge as the corresponding reverse edge to 'edge', and then add 'edge'
// as the corresponding reverse edge to 'reverseEdge'
EdgeDescriptor edge = add_edge(source, target, 1, graph).first;
EdgeDescriptor reverseEdge = add_edge(target, source, 1, graph).first;
reverseEdges.push_back(reverseEdge);
reverseEdges.push_back(edge);
capacity.push_back(weight);
capacity.push_back(weight);
}
int main()
{
GraphType graph;
unsigned int numberOfVertices = 2*2 + 2; // a 2x2 grid
std::vector<int> groups(numberOfVertices);
std::vector<EdgeDescriptor> reverseEdges;
std::vector<float> capacity;
float weight = 1;
AddBidirectionalEdge(graph, 0, 1, weight, reverseEdges, capacity);
AddBidirectionalEdge(graph, 1, 2, weight, reverseEdges, capacity);
AddBidirectionalEdge(graph, 2, 3, weight, reverseEdges, capacity);
AddBidirectionalEdge(graph, 3, 0, weight, reverseEdges, capacity);
int sourceId = 4;
int sinkId = 5;
// Add edges between all vertices and the source, as well as between all vertices and the sink
float highWeight = 1000;
for(size_t i = 0; i < 4; ++i)
{
AddBidirectionalEdge(graph, i, sourceId, highWeight, reverseEdges, capacity);
AddBidirectionalEdge(graph, i, sinkId, highWeight, reverseEdges, capacity);
}
std::vector<float> residual_capacity(num_edges(graph), 0);
VertexDescriptor sourceVertex = vertex(4,graph);
VertexDescriptor sinkVertex = vertex(5,graph);
// There should be 2*2 + 2 = 6 nodes
std::cout << "Number of vertices " << num_vertices(graph) << std::endl;
// There should be 4 + 4 + 4 = 12 edges
std::cout << "Number of edges " << num_edges(graph) << std::endl;
boost::boykov_kolmogorov_max_flow(graph,
boost::make_iterator_property_map(&capacity[0], get(boost::edge_index, graph)),
boost::make_iterator_property_map(&residual_capacity[0], get(boost::edge_index, graph)),
boost::make_iterator_property_map(&reverseEdges[0], get(boost::edge_index, graph)),
boost::make_iterator_property_map(&groups[0], get(boost::vertex_index, graph)),
get(boost::vertex_index, graph),
sourceVertex,
sinkVertex);
// Display the segmentation
for(size_t index=0; index < groups.size(); ++index)
{
std::cout << "Vertex " << index << " is in group " << groups[index] << std::endl;
}
return EXIT_SUCCESS;
}
它可以编译,但在运行时我得到:
Assertion `get(m_rev_edge_map, get(m_rev_edge_map, *ei)) == *ei' failed.
有人能看到出了什么问题吗?从文档中还不清楚反向边的矢量应该是什么样子的——它应该与图中的边的数量相同吗?还是那个长度的一半?
似乎必须手动指定边缘索引。这里是AddBidirectionalEdge的修改版本,它正确地构造了反向边映射,并正确地设置了边索引。
void AddBidirectionalEdge(GraphType& graph, unsigned int source, unsigned int target, float weight,
std::vector<EdgeDescriptor>& reverseEdges, std::vector<float>& capacity)
{
// Add edges between grid vertices. We have to create the edge and the reverse edge,
// then add the reverseEdge as the corresponding reverse edge to 'edge', and then add 'edge'
// as the corresponding reverse edge to 'reverseEdge'
int nextEdgeId = num_edges(graph);
EdgeDescriptor edge;
bool inserted;
boost::tie(edge,inserted) = add_edge(source, target, nextEdgeId, graph);
if(!inserted)
{
std::cerr << "Not inserted!" << std::endl;
}
EdgeDescriptor reverseEdge = add_edge(target, source, nextEdgeId + 1, graph).first;
reverseEdges.push_back(reverseEdge);
reverseEdges.push_back(edge);
capacity.push_back(weight);
capacity.push_back(weight);
}
相关文章:
- <streamsize>C++ 中 numeric_limits::max() 的值
- 黑客级别的Mini-Max Sum
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 关于 std::min, std::max 中的比较运算符的混淆
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- std::max() 函数与定点实现的比较中的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- 从自定义类获取对象向量中的 max 元素
- 如何在C++中递归地找到max元素的索引?
- SFINAE 与 numeric_limits<T>::max() 在 MSVC2017 上
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- 在f2c.h文件中定义min()max()宏时出错
- c++curl返回413请求实体过大,但是post大小远小于max大小
- 在枚举类型上使用std::max是不是一种糟糕的做法
- 为什么Visual Studio 2019不支持用于减少Openmp的关键字"max"?
- 没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
- 如何在 C++11 中将 std::max 与自定义比较器一起使用?
- 为什么在这种情况下不能将 Max 用作函数?
- 为什么 UInt64 变量不能包含大于 UInt32::Max 的值?