3

I want to use the graph cut algorithm on images in my project, I'm using python 2.7.
I found the pymaxflow implementation, but the documentation doesn't seems so clear. I make an example, here is my 5*5 matrix:

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

Virtual terminal nodes, S (source) and T (sink) should be connected with infinite weight arcs to all pixels of the leftmost and rightmost columns of the matrix respectively. Here is what I'd like to obtain:

Graph rapresentation to obtain

Here is my code to obtain this, but it doesn't work

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() makes the python console in an infinite loop. I'm not sure about my implementation: What is the way to make a correct graph that can be used in the graph cut algorithm?

Thanks!

P.s. If you know the solution with another library tell me, any suggestion will be really appreciated.

Rowandish
  • 2,655
  • 3
  • 31
  • 52

1 Answers1

0

For working with graphs, I'd recommend the networkx package. It has many features, including a max-flow function (with example):

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

chepyle
  • 976
  • 1
  • 9
  • 16