2014-10-10 97 views
3

我想对我的项目中的图像使用图形切割算法,我正在使用python 2.7
我发现pymaxflow implementation,但文档似乎并不那么清楚。 我让一个例子,这是我的5×5矩阵:使用python切割图形:如何正确设置图形?

>>> 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]]) 

虚拟终端节点,小号(源)和Ť(汇)应具有无限重量电弧以的所有像素被连接矩阵的最左边和最右边的列。 这里是想我获得:

Graph rapresentation to obtain

这里是我的代码,以获得这一点,但它不工作

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()使蟒蛇控制台在一个无限循环。 我不确定我的实现:在图切割算法中使用正确的图可以使用什么方法?

谢谢!

P.s.如果你知道另一个库的解决方案告诉我,任何建议将非常感激。

回答