【问题标题】:Graph cut with python: How to set up the graph correctly?用 python 切割图形:如何正确设置图形?
【发布时间】:2014-12-05 05:43:48
【问题描述】:

我想在我的项目中对图像使用图形切割算法,我正在使用 python 2.7
我找到了pymaxflow implementation,但文档似乎不太清楚。 我举个例子,这是我的 5*5 矩阵:

>>> A
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

虚拟终端节点,S(source)和T(sink)应该分别用无限权重弧连接到矩阵最左边和最右边列的所有像素. 这是我想要获得的:

这是我获取这个的代码,但它不起作用

left_most = concatenate((np.zeros((1, A.shape[0])), np.arange(A.shape[0]).reshape(1, A.shape[0]))).astype(np.uint64)
left_most = np.ravel_multi_index(left_most, A.shape)
right_most = concatenate((np.ones((1, A.shape[0])) * size(A, 1) - 1, np.arange(A.shape[0]).reshape(1, A.shape[0]))).astype(np.uint64)
right_most = np.ravel_multi_index(right_most, A.shape)
g.add_grid_tedges(left_most, np.ones(left_most.shape) * np.inf, np.zeros(left_most.shape))
g.add_grid_tedges(right_most, np.zeros(right_most.shape), np.ones(right_most.shape) * np.inf)

g.maxflow() 使 python 控制台进入无限循环。 我不确定我的实现:如何制作可用于图切割算法的正确图?

谢谢!

附:如果您知道其他库的解决方案,请告诉我,任何建议将不胜感激。

【问题讨论】:

    标签: python algorithm numpy graph max-flow


    【解决方案1】:

    为了处理图表,我推荐networkx 包。它有很多特性,包括最大流量函数(带有示例):

    http://networkx.github.io/documentation/networkx-1.9.1/reference/generated/networkx.algorithms.flow.maximum_flow_value.html#networkx.algorithms.flow.maximum_flow_value

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 2013-08-08
      • 2014-11-16
      • 1970-01-01
      • 2014-07-30
      • 2015-10-02
      • 1970-01-01
      相关资源
      最近更新 更多