Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moving generation of image patch out of inner loop

  • Loading branch information...
commit cc32125b5780d8a44073d5b5d13a5d320cfa95e9 1 parent 2762818
Tim Sheerman-Chase authored

Showing 2 changed files with 23 additions and 28 deletions. Show diff stats Hide diff stats

  1. +18 17 trackFeatures.py
  2. +5 11 trackFeaturesUtils.pyx
35 trackFeatures.py
@@ -14,7 +14,7 @@
14 14
15 15 #*********************************************************************
16 16
17   -def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc):
  17 +def trackFeatureIterateSciPy(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc):
18 18
19 19 width = tc.window_width # size of window
20 20 height = tc.window_height
@@ -28,8 +28,8 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
28 28 one_plus_eps = 1.001 # To prevent rounding errors
29 29 hw = width/2
30 30 hh = height/2
31   - nc = img1.shape[1]
32   - nr = img1.shape[0]
  31 + nc = img2.shape[1]
  32 + nr = img2.shape[0]
33 33
34 34
35 35
@@ -37,16 +37,14 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
37 37 warnings.simplefilter("ignore") #Surpress warnings about max iterations
38 38
39 39 soln = scipy.optimize.leastsq(func=trackFeaturesUtils.minFunc,
40   - x0=(x2, y2), args=(img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2),
  40 + x0=(x2, y2), args=(img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2),
41 41 Dfun=trackFeaturesUtils.jacobian,factor=step_factor,maxfev=max_iterations)
42 42 status = kltState.KLT_TRACKED
43 43 x2 = soln[0][0]
44 44 y2 = soln[0][1]
45 45
46 46 # If out of bounds, set status
47   - if x1-hw < 0. or nc-( x1+hw) < one_plus_eps or \
48   - x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
49   - y1-hh < 0. or nr-( y1+hh) < one_plus_eps or \
  47 + if x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
50 48 y2-hh < 0. or nr-(y2+hh) < one_plus_eps:
51 49 status = kltState.KLT_OOB
52 50
@@ -54,7 +52,7 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
54 52
55 53 #*********************************************************************
56 54
57   -def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc):
  55 +def trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc):
58 56
59 57 width = tc.window_width # size of window
60 58 height = tc.window_height
@@ -68,16 +66,14 @@ def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img
68 66 one_plus_eps = 1.001 # To prevent rounding errors
69 67 hw = width/2
70 68 hh = height/2
71   - nc = img1.shape[1]
72   - nr = img1.shape[0]
  69 + nc = img2.shape[1]
  70 + nr = img2.shape[0]
73 71
74 72 # Iteratively update the window position
75 73 while True:
76 74
77 75 # If out of bounds, exit loop
78   - if x1-hw < 0. or nc-( x1+hw) < one_plus_eps or \
79   - x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
80   - y1-hh < 0. or nr-( y1+hh) < one_plus_eps or \
  76 + if x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
81 77 y2-hh < 0. or nr-(y2+hh) < one_plus_eps:
82 78 status = kltState.KLT_OOB
83 79 break
@@ -89,8 +85,9 @@ def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img
89 85 #gradx, grady = trackFeaturesUtils._computeGradientSumLightingInsensitive(gradx1, grady1, gradx, grady2, img1, img2, x1, y1, x2, y2, width, height)
90 86 else:
91 87 imgdiff = trackFeaturesUtils._computeIntensityDifference(img1Patch, img2, x2, y2, width, height)
92   - gradx = trackFeaturesUtils._computeGradientSum(gradx1, gradx2, x1, y1, x2, y2, width, height)
93   - grady = trackFeaturesUtils._computeGradientSum(grady1, grady2, x1, y1, x2, y2, width, height)
  88 +
  89 + gradx = trackFeaturesUtils._computeGradientSum(img1GradxPatch, gradx2, x2, y2, width, height)
  90 + grady = trackFeaturesUtils._computeGradientSum(img1GradyPatch, grady2, x2, y2, width, height)
94 91
95 92 # Use these windows to construct matrices
96 93 gxx, gxy, gyy = trackFeaturesUtils._compute2by2GradientMatrix(gradx, grady, width, height)
@@ -148,12 +145,16 @@ def _trackFeature(
148 145 one_plus_eps = 1.001 # To prevent rounding errors
149 146
150 147 img1Patch = np.empty((height, width))
  148 + img1GradxPatch = np.empty((height, width))
  149 + img1GradyPatch = np.empty((height, width))
151 150 for j in range(-hh, hh + 1):
152 151 for i in range(-hw, hw + 1):
153 152 img1Patch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, img1)
  153 + img1GradxPatch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, gradx1)
  154 + img1GradyPatch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, grady1)
154 155
155   - x2, y2, status, iteration = trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc)
156   - #x2, y2, status, iteration = trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc)
  156 + x2, y2, status, iteration = trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc)
  157 + #x2, y2, status, iteration = trackFeatureIterateSciPy(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc)
157 158
158 159 # Check whether window is out of bounds
159 160 if x2-hw < 0.0 or nc-(x2+hw) < one_plus_eps or y2-hh < 0.0 or nr-(y2+hh) < one_plus_eps:
16 trackFeaturesUtils.pyx
@@ -89,9 +89,8 @@ def _computeIntensityDifference(img1Patch, # images
89 89 #* overlaid gradients.
90 90 #*
91 91
92   -def _computeGradientSum(np.ndarray[np.float32_t,ndim=2] gradx1, # gradient images
  92 +def _computeGradientSum(img1GradxPatch, # gradient images
93 93 np.ndarray[np.float32_t,ndim=2] gradx2,
94   - float x1, float y1, # center of window in 1st img
95 94 float x2, float y2, # center of window in 2nd img
96 95 int width, int height): # size of window
97 96
@@ -101,11 +100,6 @@ def _computeGradientSum(np.ndarray[np.float32_t,ndim=2] gradx1, # gradient imag
101 100 cdef int i, j
102 101 gradx = []
103 102
104   - img1GradxPatch = np.empty((height, width))
105   - for j in range(-hh, hh + 1):
106   - for i in range(-hw, hw + 1):
107   - img1GradxPatch[j+hh,i+hw] = interpolate(x1+i, y1+j, gradx1)
108   -
109 103 img2Patch = np.empty((height, width))
110 104 for j in range(-hh, hh + 1):
111 105 for i in range(-hw, hw + 1):
@@ -290,7 +284,7 @@ def _solveEquation(gxx, gxy, gyy,
290 284 dy = (gxx*ey - gxy*ex)/det
291 285 return kltState.KLT_TRACKED, dx, dy
292 286
293   -def minFunc(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2):
  287 +def minFunc(xData, img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2):
294 288 x2, y2 = xData
295 289
296 290 #print img1, img2, x1, y1, width, height
@@ -303,15 +297,15 @@ def minFunc(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, gra
303 297 #print "test", x2, y2, np.array(imgdiff).sum()
304 298 return imgdiff
305 299
306   -def jacobian(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2):
  300 +def jacobian(xData, img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2):
307 301 x2, y2 = xData
308 302 #print img1, img2, x1, y1, width, height
309 303 if tc.lighting_insensitive:
310 304 raise Exception("Not implemented")
311 305 #gradx, grady = _computeGradientSumLightingInsensitive(gradx1, grady1, gradx, grady2, img1, img2, x1, y1, x2, y2, width, height)
312 306 else:
313   - gradx = _computeGradientSum(gradx1, gradx2, x1, y1, x2, y2, width, height)
314   - grady = _computeGradientSum(grady1, grady2, x1, y1, x2, y2, width, height)
  307 + gradx = _computeGradientSum(img1GradxPatch, gradx2, x2, y2, width, height)
  308 + grady = _computeGradientSum(img1GradyPatch, grady2, x2, y2, width, height)
315 309 out = - np.array([gradx, grady]).transpose()
316 310 #print out.shape
317 311

0 comments on commit cc32125

Please sign in to comment.
Something went wrong with that request. Please try again.