-
Notifications
You must be signed in to change notification settings - Fork 18
/
selectGoodFeatures.py
295 lines (236 loc) · 8.87 KB
/
selectGoodFeatures.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from __future__ import print_function
import math, numpy as np
from PIL import Image
from klt import *
from error import *
from convolve import *
from klt_util import *
import goodFeaturesUtils
class selectionMode:
SELECTING_ALL = 1
REPLACING_SOME = 2
KLT_verbose = 1
#*********************************************************************
def _fillFeaturemap(x, y, featuremap, mindist, ncols, nrows):
for iy in range(y - mindist,y + mindist + 1):
for ix in range(x - mindist, x + mindist + 1):
if ix >= 0 and ix < ncols and iy >= 0 and iy < nrows:
featuremap[iy*ncols+ix] = True
return featuremap
#*********************************************************************
#* _enforceMinimumDistance
#*
#* Removes features that are within close proximity to better features.
#*
#* INPUTS
#* featurelist: A list of features. The nFeatures property
#* is used.
#*
#* OUTPUTS
#* featurelist: Is overwritten. Nearby "redundant" features are removed.
#* Writes -1's into the remaining elements.
#*
#* RETURNS
#* The number of remaining features.
#*
def _enforceMinimumDistance(pointlist, featurelist, ncols, nrows, mindist, min_eigenvalue, overwriteAllFeatures):
#int indx; # Index into features
#int x, y, val; # Location and trackability of pixel under consideration
#uchar *featuremap; # Boolean array recording proximity of features
#int *ptr;
# Cannot add features with an eigenvalue less than one
if min_eigenvalue < 1: min_eigenvalue = 1
# Allocate memory for feature map and clear it
#featuremap = (uchar *) malloc(ncols * nrows * sizeof(uchar));
#memset(featuremap, 0, ncols*nrows);
featuremap = [False for i in range(ncols * nrows)]
# Necessary because code below works with (mindist-1)
mindist = mindist - 1
# If we are keeping all old good features, then add them to the featuremap
if not overwriteAllFeatures:
for indx, feat in enumerate(featurelist):
if featurelist[indx].val >= 0:
x = int(featurelist[indx].x)
y = int(featurelist[indx].y)
featuremap = _fillFeaturemap(x, y, featuremap, mindist, ncols, nrows)
# For each feature point, in descending order of importance, do ...
indx = 0
pointlistIndx = 0
while True:
# If we can't add all the points, then fill in the rest
# of the featurelist with -1's */
if pointlistIndx >= len(pointlist):
while indx < len(featurelist):
if overwriteAllFeatures and featurelist[indx].val < 0:
featurelist[indx].x = -1
featurelist[indx].y = -1
featurelist[indx].val = kltState.KLT_NOT_FOUND
featurelist[indx].aff_img = None
featurelist[indx].aff_img_gradx = None
featurelist[indx].aff_img_grady = None
featurelist[indx].aff_x = -1.0
featurelist[indx].aff_y = -1.0
featurelist[indx].aff_Axx = 1.0
featurelist[indx].aff_Ayx = 0.0
featurelist[indx].aff_Axy = 0.0
featurelist[indx].aff_Ayy = 1.0
indx = indx + 1
break
pointdata = pointlist[pointlistIndx]
x = pointdata[1]
y = pointdata[2]
val = pointdata[0]
pointlistIndx += 1
# Ensure that feature is in-bounds
assert x >= 0
assert x < ncols
assert y >= 0
assert y < nrows
while not overwriteAllFeatures and indx < len(featurelist) and featurelist[indx].val >= 0:
indx = indx + 1
if indx >= len(featurelist): break
# If no neighbor has been selected, and if the minimum
# eigenvalue is large enough, then add feature to the current list
if not featuremap[y*ncols+x] and val >= min_eigenvalue:
featurelist[indx].x = x
featurelist[indx].y = y
featurelist[indx].val = int(val)
featurelist[indx].aff_img = None
featurelist[indx].aff_img_gradx = None
featurelist[indx].aff_img_grady = None
featurelist[indx].aff_x = -1.0
featurelist[indx].aff_y = -1.0
featurelist[indx].aff_Axx = 1.0
featurelist[indx].aff_Ayx = 0.0
featurelist[indx].aff_Axy = 0.0
featurelist[indx].aff_Ayy = 1.0
indx = indx + 1
# Fill in surrounding region of feature map, but
# make sure that pixels are in-bounds */
featuremap = _fillFeaturemap(x, y, featuremap, mindist, ncols, nrows);
return featurelist
#*********************************************************************
def _KLTSelectGoodFeatures(tc,img,nFeatures,mode):
featurelist = [KLT_Feature() for i in range(nFeatures)]
#_KLT_FloatImage floatimg, gradx, grady;
#int window_hw, window_hh
#int *pointlist
overwriteAllFeatures = (mode == selectionMode.SELECTING_ALL)
floatimages_created = False
ncols, nrows = img.size
# Check window size (and correct if necessary)
if tc.window_width % 2 != 1:
tc.window_width = tc.window_width+1
KLTWarning("Tracking context's window width must be odd. Changing to {0}.\n".format(tc.window_width))
if tc.window_height % 2 != 1:
tc.window_height = tc.window_height+1
KLTWarning("Tracking context's window height must be odd. Changing to {0}.\n".format(tc.window_height))
if tc.window_width < 3:
tc.window_width = 3
KLTWarning("Tracking context's window width must be at least three. \nChanging to %d.\n".format(tc.window_width))
if tc.window_height < 3:
tc.window_height = 3
KLTWarning("Tracking context's window height must be at least three. \nChanging to %d.\n".format(tc.window_height))
window_hw = tc.window_width/2
window_hh = tc.window_height/2
# Create pointlist, which is a simplified version of a featurelist,
# for speed. Contains only integer locations and values.
#pointlist = [0 for i in range(ncols * nrows * 3)]
# Create temporary images, etc.
if mode == selectionMode.REPLACING_SOME and tc.sequentialMode and tc.pyramid_last != None:
floatimg = tc.pyramid_last.img[0]
gradx = tc.pyramid_last_gradx.img[0]
grady = tc.pyramid_last_grady.img[0]
assert gradx != None
assert grady != None
else:
floatimages_created = True
floatimg = Image.new("F", img.size)
gradx = Image.new("F", img.size)
grady = Image.new("F", img.size)
if tc.smoothBeforeSelecting:
#_KLT_FloatImage tmpimg;
#tmpimg = Image.new("F", img.size)
tmpimg = np.array(img.convert("F"))
floatimg = KLTComputeSmoothedImage(tmpimg, KLTComputeSmoothSigma(tc))
#_KLTFreeFloatImage(tmpimg)
else:
floatimg = np.array(img.convert("F"))
# Compute gradient of image in x and y direction
gradx, grady = KLTComputeGradients(floatimg, tc.grad_sigma)
# Write internal images
if tc.writeInternalImages:
floatimg.save("kltimg_sgfrlf.pgm")
gradx.save("kltimg_sgfrlf_gx.pgm")
grady.save("kltimg_sgfrlf_gy.pgm")
# Compute trackability of each image pixel as the minimum
# of the two eigenvalues of the Z matrix
#register float gx, gy;
#register float gxx, gxy, gyy;
#register int xx, yy;
#register int *ptr;
#float val;
#unsigned int limit = 1;
borderx = tc.borderx; # Must not touch cols
bordery = tc.bordery; # lost by convolution
#int x, y;
#int i;
if borderx < window_hw: borderx = window_hw
if bordery < window_hh: bordery = window_hh
# Find largest value of an int
#for (i = 0 ; i < sizeof(int) ; i++) limit *= 256;
#limit = limit/2 - 1;
#gradxArr = np.array(gradx)
#gradyArr = np.array(grady)
pointlistx,pointlisty,pointlistval=goodFeaturesUtils.ScanImageForGoodFeatures(gradx,\
grady, borderx, bordery, window_hw, window_hh, tc.nSkippedPixels)
# Sort the features
pointlist = list(zip(pointlistval, pointlistx, pointlisty))
pointlist.sort()
pointlist.reverse()
#print(pointlist)
# Check tc.mindist
if tc.mindist < 0:
KLTWarning("(_KLTSelectGoodFeatures) Tracking context field tc.mindist is negative ({0}); setting to zero".format(tc.mindist))
tc.mindist = 0;
# Enforce minimum distance between features
_enforceMinimumDistance(pointlist, \
featurelist, \
ncols, nrows, \
tc.mindist, \
tc.min_eigenvalue, \
overwriteAllFeatures)
# Free memory
# free(pointlist);
# if (floatimages_created) {
# _KLTFreeFloatImage(floatimg);
# _KLTFreeFloatImage(gradx);
# _KLTFreeFloatImage(grady);
# }
return featurelist
#*********************************************************************
#* KLTSelectGoodFeatures
#*
#* Main routine, visible to the outside. Finds the good features in
#* an image.
#*
#* INPUTS
#* tc: Contains parameters used in computation (size of image,
#* size of window, min distance b/w features, sigma to compute
#* image gradients, # of features desired).
#* img: Pointer to the data of an image (probably unsigned chars).
#*
#* OUTPUTS
#* features: List of features. The member nFeatures is computed.
#*
def KLTSelectGoodFeatures(tc, img, nFeatures):
ncols, nrows = img.size
#int ncols, int nrows,
if KLT_verbose >= 1:
print("(KLT) Selecting the {0} best features from a {1} by {2} image... ".format(nFeatures, ncols, nrows))
fl = _KLTSelectGoodFeatures(tc, img, nFeatures, selectionMode.SELECTING_ALL)
if KLT_verbose >= 1:
print("\n\t{0} features found.\n".format(KLTCountRemainingFeatures(fl)))
if tc.writeInternalImages:
print("\tWrote images to 'kltimg_sgfrlf*.pgm'.\n")
return fl