forked from amueller/gco_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgco_python.pyx
130 lines (115 loc) · 5.21 KB
/
gco_python.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
cimport numpy as np
np.import_array()
cdef extern from "GCoptimization.h":
cdef cppclass GCoptimizationGridGraph:
GCoptimizationGridGraph(int width, int height, int n_labels)
void setDataCost(int *)
void setSmoothCost(int *)
void expansion(int n_iterations)
void swap(int n_iterations)
void setSmoothCostVH(int* pairwise, int* V, int* H)
int whatLabel(int node)
cdef cppclass GCoptimizationGeneralGraph:
GCoptimizationGeneralGraph(int n_vertices, int n_labels)
void setDataCost(int *)
void setSmoothCost(int *)
void setNeighbors(int, int)
void expansion(int n_iterations)
void swap(int n_iterations)
int whatLabel(int node)
def cut_simple(np.ndarray[np.int32_t, ndim=3, mode='c'] unary_cost,
np.ndarray[np.int32_t, ndim=2, mode='c'] pairwise_cost, n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to grid graph.
Parameters
----------
unary_cost: ndarray, int32, shape=(width, height, n_labels)
Unary potentials
pairwise_cost: ndarray, int32, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
if unary_cost.shape[2] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1], unary_cost.shape[2],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
cdef int h = unary_cost.shape[1]
cdef int w = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
cdef GCoptimizationGridGraph* gc = new GCoptimizationGridGraph(h, w, n_labels)
gc.setDataCost(<int*>unary_cost.data)
gc.setSmoothCost(<int*>pairwise_cost.data)
if algorithm == 'swap':
gc.swap(n_iter)
elif algorithm == 'expansion':
gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[2]
result_shape[0] = w
result_shape[1] = h
cdef np.ndarray[np.int32_t, ndim=2] result = np.PyArray_SimpleNew(2, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(w * h):
result_ptr[i] = gc.whatLabel(i)
return result
def cut_from_graph(np.ndarray[np.int32_t, ndim=2, mode='c'] edges,
np.ndarray[np.int32_t, ndim=2, mode='c'] unary_cost,
np.ndarray[np.int32_t, ndim=2, mode='c'] pairwise_cost, n_iter=5,
algorithm='expansion'):
"""
Apply multi-label graphcuts to arbitrary graph given by `edges`.
Parameters
----------
edges: ndarray, int32, shape(n_edges, 2)
Rows correspond to edges in graph, given as vertex indices.
unary_cost: ndarray, int32, shape=(n_vertices, n_labels)
Unary potentials
pairwise_cost: ndarray, int32, shape=(n_labels, n_labels)
Pairwise potentials for label compatibility
n_iter: int, (default=5)
Number of iterations
algorithm: string, `expansion` or `swap`, default=expansion
Whether to perform alpha-expansion or alpha-beta-swaps.
"""
if (pairwise_cost != pairwise_cost.T).any():
raise ValueError("pairwise_cost must be symmetric.")
if unary_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("unary_cost and pairwise_cost have incompatible shapes.\n"
"unary_cost must be height x width x n_labels, pairwise_cost must be n_labels x n_labels.\n"
"Got: unary_cost: (%d, %d), pairwise_cost: (%d, %d)"
%(unary_cost.shape[0], unary_cost.shape[1],
pairwise_cost.shape[0], pairwise_cost.shape[1]))
if pairwise_cost.shape[1] != pairwise_cost.shape[0]:
raise ValueError("pairwise_cost must be a square matrix.")
cdef int n_vertices = unary_cost.shape[0]
cdef int n_labels = pairwise_cost.shape[0]
cdef GCoptimizationGeneralGraph* gc = new GCoptimizationGeneralGraph(n_vertices, n_labels)
for e in edges:
gc.setNeighbors(e[0], e[1])
gc.setDataCost(<int*>unary_cost.data)
gc.setSmoothCost(<int*>pairwise_cost.data)
if algorithm == 'swap':
gc.swap(n_iter)
elif algorithm == 'expansion':
gc.expansion(n_iter)
else:
raise ValueError("algorithm should be either `swap` or `expansion`. Got: %s" % algorithm)
cdef np.npy_intp result_shape[1]
result_shape[0] = n_vertices
cdef np.ndarray[np.int32_t, ndim=1] result = np.PyArray_SimpleNew(1, result_shape, np.NPY_INT32)
cdef int * result_ptr = <int*>result.data
for i in xrange(n_vertices):
result_ptr[i] = gc.whatLabel(i)
return result