Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moving generation of image patch out of inner loop

  • Loading branch information...
commit cc32125b5780d8a44073d5b5d13a5d320cfa95e9 1 parent 2762818
Tim Sheerman-Chase authored
Showing with 23 additions and 28 deletions.
  1. +18 −17 trackFeatures.py
  2. +5 −11 trackFeaturesUtils.pyx
View
35 trackFeatures.py
@@ -14,7 +14,7 @@
#*********************************************************************
-def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc):
+def trackFeatureIterateSciPy(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc):
width = tc.window_width # size of window
height = tc.window_height
@@ -28,8 +28,8 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
one_plus_eps = 1.001 # To prevent rounding errors
hw = width/2
hh = height/2
- nc = img1.shape[1]
- nr = img1.shape[0]
+ nc = img2.shape[1]
+ nr = img2.shape[0]
@@ -37,16 +37,14 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
warnings.simplefilter("ignore") #Surpress warnings about max iterations
soln = scipy.optimize.leastsq(func=trackFeaturesUtils.minFunc,
- x0=(x2, y2), args=(img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2),
+ x0=(x2, y2), args=(img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2),
Dfun=trackFeaturesUtils.jacobian,factor=step_factor,maxfev=max_iterations)
status = kltState.KLT_TRACKED
x2 = soln[0][0]
y2 = soln[0][1]
# If out of bounds, set status
- if x1-hw < 0. or nc-( x1+hw) < one_plus_eps or \
- x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
- y1-hh < 0. or nr-( y1+hh) < one_plus_eps or \
+ if x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
y2-hh < 0. or nr-(y2+hh) < one_plus_eps:
status = kltState.KLT_OOB
@@ -54,7 +52,7 @@ def trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, im
#*********************************************************************
-def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc):
+def trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc):
width = tc.window_width # size of window
height = tc.window_height
@@ -68,16 +66,14 @@ def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img
one_plus_eps = 1.001 # To prevent rounding errors
hw = width/2
hh = height/2
- nc = img1.shape[1]
- nr = img1.shape[0]
+ nc = img2.shape[1]
+ nr = img2.shape[0]
# Iteratively update the window position
while True:
# If out of bounds, exit loop
- if x1-hw < 0. or nc-( x1+hw) < one_plus_eps or \
- x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
- y1-hh < 0. or nr-( y1+hh) < one_plus_eps or \
+ if x2-hw < 0. or nc-(x2+hw) < one_plus_eps or \
y2-hh < 0. or nr-(y2+hh) < one_plus_eps:
status = kltState.KLT_OOB
break
@@ -89,8 +85,9 @@ def trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img
#gradx, grady = trackFeaturesUtils._computeGradientSumLightingInsensitive(gradx1, grady1, gradx, grady2, img1, img2, x1, y1, x2, y2, width, height)
else:
imgdiff = trackFeaturesUtils._computeIntensityDifference(img1Patch, img2, x2, y2, width, height)
- gradx = trackFeaturesUtils._computeGradientSum(gradx1, gradx2, x1, y1, x2, y2, width, height)
- grady = trackFeaturesUtils._computeGradientSum(grady1, grady2, x1, y1, x2, y2, width, height)
+
+ gradx = trackFeaturesUtils._computeGradientSum(img1GradxPatch, gradx2, x2, y2, width, height)
+ grady = trackFeaturesUtils._computeGradientSum(img1GradyPatch, grady2, x2, y2, width, height)
# Use these windows to construct matrices
gxx, gxy, gyy = trackFeaturesUtils._compute2by2GradientMatrix(gradx, grady, width, height)
@@ -148,12 +145,16 @@ def _trackFeature(
one_plus_eps = 1.001 # To prevent rounding errors
img1Patch = np.empty((height, width))
+ img1GradxPatch = np.empty((height, width))
+ img1GradyPatch = np.empty((height, width))
for j in range(-hh, hh + 1):
for i in range(-hw, hw + 1):
img1Patch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, img1)
+ img1GradxPatch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, gradx1)
+ img1GradyPatch[j+hh,i+hw] = trackFeaturesUtils.interpolate(x1+i, y1+j, grady1)
- x2, y2, status, iteration = trackFeatureIterateCKLT(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc)
- #x2, y2, status, iteration = trackFeatureIterateSciPy(x1, y1, x2, y2, img1, gradx1, grady1, img1Patch, img2, gradx2, grady2, tc)
+ x2, y2, status, iteration = trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc)
+ #x2, y2, status, iteration = trackFeatureIterateSciPy(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, img2, gradx2, grady2, tc)
# Check whether window is out of bounds
if x2-hw < 0.0 or nc-(x2+hw) < one_plus_eps or y2-hh < 0.0 or nr-(y2+hh) < one_plus_eps:
View
16 trackFeaturesUtils.pyx
@@ -89,9 +89,8 @@ def _computeIntensityDifference(img1Patch, # images
#* overlaid gradients.
#*
-def _computeGradientSum(np.ndarray[np.float32_t,ndim=2] gradx1, # gradient images
+def _computeGradientSum(img1GradxPatch, # gradient images
np.ndarray[np.float32_t,ndim=2] gradx2,
- float x1, float y1, # center of window in 1st img
float x2, float y2, # center of window in 2nd img
int width, int height): # size of window
@@ -101,11 +100,6 @@ def _computeGradientSum(np.ndarray[np.float32_t,ndim=2] gradx1, # gradient imag
cdef int i, j
gradx = []
- img1GradxPatch = np.empty((height, width))
- for j in range(-hh, hh + 1):
- for i in range(-hw, hw + 1):
- img1GradxPatch[j+hh,i+hw] = interpolate(x1+i, y1+j, gradx1)
-
img2Patch = np.empty((height, width))
for j in range(-hh, hh + 1):
for i in range(-hw, hw + 1):
@@ -290,7 +284,7 @@ def _solveEquation(gxx, gxy, gyy,
dy = (gxx*ey - gxy*ex)/det
return kltState.KLT_TRACKED, dx, dy
-def minFunc(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2):
+def minFunc(xData, img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2):
x2, y2 = xData
#print img1, img2, x1, y1, width, height
@@ -303,15 +297,15 @@ def minFunc(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, gra
#print "test", x2, y2, np.array(imgdiff).sum()
return imgdiff
-def jacobian(xData, img1Patch, img1, img2, x1, y1, width, height, tc, gradx1, grady1, gradx2, grady2):
+def jacobian(xData, img1Patch, img1GradxPatch, img1GradyPatch, img2, width, height, tc, gradx2, grady2):
x2, y2 = xData
#print img1, img2, x1, y1, width, height
if tc.lighting_insensitive:
raise Exception("Not implemented")
#gradx, grady = _computeGradientSumLightingInsensitive(gradx1, grady1, gradx, grady2, img1, img2, x1, y1, x2, y2, width, height)
else:
- gradx = _computeGradientSum(gradx1, gradx2, x1, y1, x2, y2, width, height)
- grady = _computeGradientSum(grady1, grady2, x1, y1, x2, y2, width, height)
+ gradx = _computeGradientSum(img1GradxPatch, gradx2, x2, y2, width, height)
+ grady = _computeGradientSum(img1GradyPatch, grady2, x2, y2, width, height)
out = - np.array([gradx, grady]).transpose()
#print out.shape
Please sign in to comment.
Something went wrong with that request. Please try again.