Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Optimise old method

  • Loading branch information...
commit 629876fb80dfcc5cb8e667ae59429e1f568107c2 1 parent 09a7589
Tim Sheerman-Chase authored
Showing with 27 additions and 16 deletions.
  1. +6 −2 example1.py
  2. +6 −3 trackFeatures.py
  3. +15 −11 trackFeaturesUtils.pyx
View
8 example1.py
@@ -11,7 +11,7 @@
from selectGoodFeatures import *
from writeFeatures import *
from trackFeatures import *
-import pickle
+import pickle, time
def main():
tc = KLT_TrackingContext()
@@ -47,9 +47,13 @@ def main():
#fl = pickle.load(open("featurelist.dat"))
#KLTTrackFeatures(tc, img1, img2, fl)
- for i in range(10):
+ count = 0
+ ti = time.clock()
+ for i in range(100):
KLTTrackFeatures(tc, img1, img2, fl)
KLTTrackFeatures(tc, img2, img1, fl)
+ count += 2
+ print (time.clock() - ti) / count
print "\nIn second image:"
for i, feat in enumerate(fl):
View
9 trackFeatures.py
@@ -71,6 +71,7 @@ def trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, i
nc = img2.shape[1]
nr = img2.shape[0]
workingPatch = np.empty((height, width), np.float32)
+ imgdiff = np.zeros((workingPatch.size), np.float32)
jacobianMem = np.zeros((workingPatch.size,2), np.float32)
# Iteratively update the window position
@@ -88,7 +89,7 @@ def trackFeatureIterateCKLT(x2, y2, img1GradxPatch, img1GradyPatch, img1Patch, i
#imgdiff = trackFeaturesUtils._computeIntensityDifferenceLightingInsensitive(img1Patch, img2, x2, y2, workingPatch)
#gradx, grady = trackFeaturesUtils.computeGradientSumLightingInsensitive(gradx1, grady1, gradx, grady2, img1, img2, x1, y1, x2, y2, workingPatch)
else:
- imgdiff = trackFeaturesUtils.computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch)
+ trackFeaturesUtils.computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch, imgdiff)
trackFeaturesUtils.computeGradientSum(img1GradxPatch, gradx2, x2, y2, workingPatch, jacobianMem, 0)
trackFeaturesUtils.computeGradientSum(img1GradyPatch, grady2, x2, y2, workingPatch, jacobianMem, 1)
@@ -173,11 +174,13 @@ def _trackFeature(
workingPatch = np.empty((height, width), np.float32)
# Check whether residue is too large
+ imgdiff = np.zeros((workingPatch.size), np.float32)
+
if status == kltState.KLT_TRACKED and max_residue is not None:
if lighting_insensitive:
- imgdiff = trackFeaturesUtils.computeIntensityDifferenceLightingInsensitive(img1Patch, img2, x1, y1, x2, y2, workingPatch)
+ trackFeaturesUtils.computeIntensityDifferenceLightingInsensitive(img1Patch, img2, x1, y1, x2, y2, workingPatch, imgdiff)
else:
- imgdiff = trackFeaturesUtils.computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch)
+ trackFeaturesUtils.computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch, imgdiff)
if np.abs(np.array(imgdiff)).sum()/(width*height) > max_residue:
status = kltState.KLT_LARGE_RESIDUE
View
26 trackFeaturesUtils.pyx
@@ -62,15 +62,15 @@ cdef _computeIntensityDifference(np.ndarray[np.float32_t,ndim=2] img1Patch, #
np.ndarray[np.float32_t,ndim=2] img2,
float x2,
float y2, # center of window in 2nd img
- np.ndarray[np.float32_t,ndim=2] workingPatch # temporary memory for patch storage, size determines window size
- ):
+ np.ndarray[np.float32_t,ndim=2] workingPatch, # temporary memory for patch storage, size determines window size
+ np.ndarray[np.float32_t,ndim=1] out):
cdef int hw = workingPatch.shape[1]/2
cdef int hh = workingPatch.shape[0]/2
cdef float g1, g2
- cdef int i, j
+ cdef int i, j, ind = 0
- imgdiff = []
+ #imgdiff = []
#imgl1 = img1.load()
#imgl2 = img2.load()
@@ -81,16 +81,20 @@ cdef _computeIntensityDifference(np.ndarray[np.float32_t,ndim=2] img1Patch, #
for i in range(-hw, hw + 1):
g1 = img1Patch[j + hh, i + hw]
g2 = workingPatch[j + hh, i + hw]
- imgdiff.append(g1 - g2)
-
- return np.array(imgdiff,np.float32)
+ #imgdiff.append(g1 - g2)
+ out[ind] = g1 - g2
+ ind += 1
+
+ return None
def computeIntensityDifference(np.ndarray[np.float32_t,ndim=2] img1Patch, # images
np.ndarray[np.float32_t,ndim=2] img2,
float x2,
float y2, # center of window in 2nd img
- np.ndarray[np.float32_t,ndim=2] workingPatch): # temporary memory for patch storage, size determines window size
- return _computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch)
+ np.ndarray[np.float32_t,ndim=2] workingPatch,
+ np.ndarray[np.float32_t,ndim=1] out): # temporary memory for patch storage, size determines window size
+
+ return _computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch, out)
#*********************************************************************
#* _computeGradientSum
@@ -334,10 +338,10 @@ def minFunc(np.ndarray[double,ndim=1] xData,
raise Exception("Not implemented")
#imgdiff = _computeIntensityDifferenceLightingInsensitive(img1, img2, x1, y1, x2, y2, workingPatch)
else:
- imgdiff = _computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch)
+ _computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch, jacobianMem[:,0])
#print "test", x2, y2, np.array(imgdiff).sum()
- return imgdiff
+ return jacobianMem[:,0]
def jacobian(np.ndarray[double,ndim=1] xData,
np.ndarray[np.float32_t,ndim=2] img1Patch,
Please sign in to comment.
Something went wrong with that request. Please try again.