-
Notifications
You must be signed in to change notification settings - Fork 2
/
shape_context.py
272 lines (229 loc) · 9.4 KB
/
shape_context.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import numpy as np
import cv2
import math
from scipy.spatial.distance import cdist, cosine
from scipy.optimize import linear_sum_assignment
import matplotlib.pyplot as plt
class ShapeContext(object):
def __init__(self, nbins_r=5, nbins_theta=12, r_inner=0.1250, r_outer=2.0):
# number of radius zones
self.nbins_r = nbins_r
# number of angles zones
self.nbins_theta = nbins_theta
# maximum and minimum radius
self.r_inner = r_inner
self.r_outer = r_outer
def _hungarian(self, cost_matrix):
"""
Here we are solving task of getting similar points from two paths
based on their cost matrixes.
This algorithm has dificulty O(n^3)
return total modification cost, indexes of matched points
"""
row_ind, col_ind = linear_sum_assignment(cost_matrix)
total = cost_matrix[row_ind, col_ind].sum()
indexes = zip(row_ind.tolist(), col_ind.tolist())
return total, indexes
def get_points_from_img(self, image, simpleto=100):
"""
This is much faster version of getting shape points algo.
It's based on cv2.findContours algorithm, which is basically return shape points
ordered by curve direction. So it's gives better and faster result
"""
if len(image.shape) > 2:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# print(image.shape)
# plt.imshow(image)
# plt.show()
cnts = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# print(cnts)
points = np.array(cnts[1][0]).reshape((-1, 2))
if len(cnts[1]) > 1:
points = np.concatenate([points, np.array(cnts[1][1]).reshape((-1, 2))], axis=0)
points = points.tolist()
# print("length = " + str(len(points)))
# print("length = " + str(simpleto))
step = len(points) / simpleto
# print("length = " + str(step))
points = [points[i] for i in xrange(0, len(points), step)][:simpleto]
if len(points) < simpleto:
points = points + [[0, 0]] * (simpleto - len(points))
return points
# def get_points_from_img(self, image, threshold=50, simpleto=100, radius=2):
# """
# That is not very good algorithm of choosing path points, but it will work for our case.
# Idea of it is just to create grid and choose points that on this grid.
# """
# if len(image.shape) > 2:
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# plt.imshow(image)
# plt.show()
# dst = cv2.Canny(image, threshold, threshold * 3, 3)
# py, px = np.gradient(image)
# # px, py gradients maps shape can be smaller then input image shape
# points = [index for index, val in np.ndenumerate(dst)
# if val == 255 and index[0] < py.shape[0] and index[1] < py.shape[1]]
# h, w = image.shape
# _radius = radius
# while len(points) > simpleto:
# newpoints = points
# xr = range(0, w, _radius)
# yr = range(0, h, _radius)
# for p in points:
# if p[0] not in yr and p[1] not in xr:
# newpoints.remove(p)
# if len(points) <= simpleto:
# T = np.zeros((simpleto, 1))
# for i, (y, x) in enumerate(points):
# radians = math.atan2(py[y, x], px[y, x])
# T[i] = radians + 2 * math.pi * (radians < 0)
# return points, np.asmatrix(T)
# _radius += 1
# T = np.zeros((simpleto, 1))
# for i, (y, x) in enumerate(points):
# radians = math.atan2(py[y, x], px[y, x])
# T[i] = radians + 2 * math.pi * (radians < 0)
# return points, np.asmatrix(T)
def _cost(self, hi, hj):
cost = 0
for k in xrange(self.nbins_theta * self.nbins_r):
if (hi[k] + hj[k]):
cost += ((hi[k] - hj[k])**2) / (hi[k] + hj[k])
return cost * 0.5
def cost_by_paper(self, P, Q, qlength=None):
p, _ = P.shape
p2, _ = Q.shape
d = p2
if qlength:
d = qlength
C = np.zeros((p, p2))
for i in xrange(p):
for j in xrange(p2):
C[i, j] = self._cost(Q[j] / d, P[i] / p)
return C
def compute(self, points):
"""
Here we are computing shape context descriptor
"""
t_points = len(points)
# getting euclidian distance
r_array = cdist(points, points)
# getting two points with maximum distance to norm angle by them
# this is needed for rotation invariant feature
am = r_array.argmax()
max_points = [am / t_points, am % t_points]
# normalizing
r_array_n = r_array / r_array.mean()
# create log space
r_bin_edges = np.logspace(np.log10(self.r_inner), np.log10(self.r_outer), self.nbins_r)
r_array_q = np.zeros((t_points, t_points), dtype=int)
# summing occurences in different log space intervals
# logspace = [0.1250, 0.2500, 0.5000, 1.0000, 2.0000]
# 0 1.3 -> 1 0 -> 2 0 -> 3 0 -> 4 0 -> 5 1
# 0.43 0 0 1 0 2 1 3 2 4 3 5
for m in xrange(self.nbins_r):
r_array_q += (r_array_n < r_bin_edges[m])
fz = r_array_q > 0
# getting angles in radians
theta_array = cdist(points, points, lambda u, v: math.atan2((v[1] - u[1]), (v[0] - u[0])))
norm_angle = theta_array[max_points[0], max_points[1]]
# making angles matrix rotation invariant
theta_array = (theta_array - norm_angle * (np.ones((t_points, t_points)) - np.identity(t_points)))
# removing all very small values because of float operation
theta_array[np.abs(theta_array) < 1e-7] = 0
# 2Pi shifted because we need angels in [0,2Pi]
theta_array_2 = theta_array + 2 * math.pi * (theta_array < 0)
# Simple Quantization
theta_array_q = (1 + np.floor(theta_array_2 / (2 * math.pi / self.nbins_theta))).astype(int)
# building point descriptor based on angle and distance
nbins = self.nbins_theta * self.nbins_r
descriptor = np.zeros((t_points, nbins))
for i in xrange(t_points):
sn = np.zeros((self.nbins_r, self.nbins_theta))
for j in xrange(t_points):
if (fz[i, j]):
sn[r_array_q[i, j] - 1, theta_array_q[i, j] - 1] += 1
descriptor[i] = sn.reshape(nbins)
return descriptor
def cosine_diff(self, P, Q):
"""
Fast cosine diff.
"""
P = P.flatten()
Q = Q.flatten()
assert len(P) == len(Q), 'number of descriptors should be the same'
return cosine(P, Q)
def diff(self, P, Q, qlength=None):
"""
More precise but not very speed efficient diff.
if Q is generalized shape context then it compute shape match.
if Q is r point representative shape contexts and qlength set to
the number of points in Q then it compute fast shape match.
"""
result = None
C = self.cost_by_paper(P, Q, qlength)
result = self._hungarian(C)
return result
@classmethod
def tests(cls):
# basics tests to see that all algorithm invariants options are working fine
self = cls()
def test_move():
p1 = np.array([
[0, 100],
[200, 60],
[350, 220],
[370, 100],
[70, 300],
])
# +30 by x
p2 = np.array([
[0, 130],
[200, 90],
[350, 250],
[370, 130],
[70, 330]
])
c1 = self.compute(p1)
c2 = self.compute(p2)
assert (np.abs(c1.flatten() - c2.flatten())
).sum() == 0, "Moving points in 2d space should give same shape context vector"
def test_scale():
p1 = np.array([
[0, 100],
[200, 60],
[350, 220],
[370, 100],
[70, 300],
])
# 2x scaling
p2 = np.array([
[0, 200],
[400, 120],
[700, 440],
[740, 200],
[149, 600]
])
c1 = self.compute(p1)
c2 = self.compute(p2)
assert (np.abs(c1.flatten() - c2.flatten())
).sum() == 0, "Scaling points in 2d space should give same shape context vector"
def test_rotation():
p1 = np.array(
[(144, 196), (220, 216), (330, 208)]
)
# 90 degree rotation
theta = np.radians(90)
c, s = np.cos(theta), np.sin(theta)
R = np.matrix('{} {}; {} {}'.format(c, -s, s, c))
p2 = np.dot(p1, R).tolist()
c1 = self.compute(p1)
c2 = self.compute(p2)
assert (np.abs(c1.flatten() - c2.flatten())
).sum() == 0, "Rotating points in 2d space should give same shape context vector"
test_move()
test_scale()
test_rotation()
print 'Tests PASSED'
if __name__ == "__main__":
ShapeContext.tests()