Skip to content

Commit 40fa4a7

Browse files
author
Tim Sheerman-Chase
committed
Add python 3 support
1 parent dc08768 commit 40fa4a7

9 files changed

+71
-62
lines changed

README.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The code is available under the Simplified BSD License.
1212
Dependencies
1313
============
1414

15-
Python >= 2.6, PIL, Numpy, SciPy >= 0.8, cython
15+
Python >= 2.6 or >=3.4, PIL, Numpy, SciPy >= 0.8, cython
1616

1717
Development
1818
===========
@@ -37,3 +37,4 @@ Usage
3737
python setup.py build_ext --inplace
3838

3939
python example1.py
40+

convolve.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from __future__ import print_function
23
import math, numpy as np
34
from PIL import Image
45

@@ -8,7 +9,7 @@
89
import scipy.ndimage
910
useScipyConvolution = True
1011
except:
11-
print "Warning: Failed to import scipy.ndimage"
12+
print("Warning: Failed to import scipy.ndimage")
1213

1314
class ConvolutionKernel:
1415
def __init__(self, maxKernelWidth = 71):
@@ -35,7 +36,7 @@ def _computeKernels(sigma):
3536

3637
# Compute kernels, and automatically determine widths */
3738

38-
hw = maxKernelWidth / 2
39+
hw = int(maxKernelWidth / 2)
3940
max_gauss = 1.0
4041
max_gaussderiv = float(sigma*math.exp(-0.5))
4142

@@ -62,12 +63,12 @@ def _computeKernels(sigma):
6263

6364
# Shift if width less than maxKernelWidth
6465
for i in range(gauss.width):
65-
gauss.data[i] = gauss.data[i+(maxKernelWidth-gauss.width)/2]
66+
gauss.data[i] = gauss.data[int(i+(maxKernelWidth-gauss.width)/2)]
6667
for i in range(gaussderiv.width):
67-
gaussderiv.data[i] = gaussderiv.data[i+(maxKernelWidth-gaussderiv.width)/2]
68+
gaussderiv.data[i] = gaussderiv.data[int(i+(maxKernelWidth-gaussderiv.width)/2)]
6869

6970
# Normalize gauss and deriv
70-
hw = gaussderiv.width / 2
71+
hw = int(gaussderiv.width / 2)
7172
den = 0.0;
7273
for i in range(gauss.width):
7374
den += gauss.data[i]
@@ -237,7 +238,7 @@ def KLTComputeGradients(img, sigma):
237238
else:
238239
gauss_kernel, gaussderiv_kernel = cachegauss, cachegaussderiv
239240

240-
#print gauss_kernel
241+
#print(gauss_kernel)
241242
#plt.plot(gauss_kernel)
242243
#plt.show()
243244

error.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12

23
#*********************************************************************
34
#* KLTError
@@ -9,7 +10,7 @@
910
#*
1011

1112
def KLTError(err):
12-
print err
13+
print(err)
1314
exit(1)
1415

1516

@@ -24,5 +25,5 @@ def KLTError(err):
2425

2526
def KLTWarning(err):
2627

27-
print err
28+
print(err)
2829

example1.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#**********************************************************************
77

88
#include "pnmio.h"
9+
from __future__ import print_function
910
from klt import *
1011
from PIL import Image
1112
from selectGoodFeatures import *
@@ -27,9 +28,9 @@ def main():
2728

2829
fl = KLTSelectGoodFeatures(tc, img1, nFeatures)
2930

30-
print "\nIn first image:"
31+
print("\nIn first image:")
3132
for i, feat in enumerate(fl):
32-
print "Feature #{0}: ({1},{2}) with value of {3}".format(i, feat.x, feat.y, feat.val)
33+
print("Feature #{0}: ({1},{2}) with value of {3}".format(i, feat.x, feat.y, feat.val))
3334

3435
KLTWriteFeatureListToPPM(fl, img1, "feat1.ppm")
3536
#KLTWriteFeatureList(fl, "feat1.txt", "%3d")
@@ -53,11 +54,11 @@ def main():
5354
KLTTrackFeatures(tc, img1, img2, fl)
5455
KLTTrackFeatures(tc, img2, img1, fl)
5556
count += 2
56-
print (time.clock() - ti) / count
57+
print((time.clock() - ti) / count)
5758

58-
print "\nIn second image:"
59+
print("\nIn second image:")
5960
for i, feat in enumerate(fl):
60-
print "Feature #{0}: ({1},{2}) with value of {3}".format(i, feat.x, feat.y, feat.val)
61+
print("Feature #{0}: ({1},{2}) with value of {3}".format(i, feat.x, feat.y, feat.val))
6162

6263
KLTWriteFeatureListToPPM(fl, img2, "feat2.ppm")
6364
#KLTWriteFeatureList(fl, "feat2.fl", NULL) # binary file

klt.py

+30-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#* Kanade-Lucas-Tomasi tracker
55
#*********************************************************************/
66

7+
from __future__ import print_function
8+
79
#typedef float KLT_locType;
810
#typedef unsigned char KLT_PixelType;
911

@@ -123,7 +125,7 @@ def KLTChangeTCPyramid(self,search_range):
123125
# Then, the value is rounded up to the nearest integer.
124126
val = float (math.log(7.0*subsampling+1.0)/math.log(8.0))
125127
self.nPyramidLevels = int(val + 0.99)
126-
self.subsampling = 8
128+
self.subsampling = 8
127129

128130

129131

@@ -282,33 +284,33 @@ class KLT_FeatureTable:
282284

283285
def KLTPrintTrackingContext(tc):
284286

285-
print tc
286-
print "\n\nTracking context:\n"
287-
print "\tmindist = {0}".format(tc.mindist)
288-
print "\twindow_width = {0}".format(tc.window_width)
289-
print "\twindow_height = {0}".format(tc.window_height)
290-
print "\tsequentialMode = {0}".format(tc.sequentialMode)
291-
print "\tsmoothBeforeSelecting = {0}".format(tc.smoothBeforeSelecting)
292-
print "\twriteInternalImages = {0}".format(tc.writeInternalImages)
293-
294-
print "\tmin_eigenvalue = {0}".format(tc.min_eigenvalue)
295-
print "\tmin_determinant = {0}".format(tc.min_determinant)
296-
print "\tmin_displacement = {0}".format(tc.min_displacement)
297-
print "\tmax_iterations = {0}".format(tc.max_iterations)
298-
print "\tmax_residue = {0}".format(tc.max_residue)
299-
print "\tgrad_sigma = {0}".format(tc.grad_sigma)
300-
print "\tsmooth_sigma_fact = {0}".format(tc.smooth_sigma_fact)
301-
print "\tpyramid_sigma_fact = {0}".format(tc.pyramid_sigma_fact)
302-
print "\tnSkippedPixels = {0}".format(tc.nSkippedPixels)
303-
print "\tborderx = {0}".format(tc.borderx)
304-
print "\tbordery = {0}".format(tc.bordery)
305-
print "\tnPyramidLevels = {0}".format(tc.nPyramidLevels)
306-
print "\tsubsampling = {0}".format(tc.subsampling)
307-
308-
print "\n\tpyramid_last = {0}".format(tc.pyramid_last)
309-
print "\tpyramid_last_gradx = {0}".format(tc.pyramid_last_gradx)
310-
print "\tpyramid_last_grady = {0}".format(tc.pyramid_last_grady)
311-
print "\n"
287+
print(tc)
288+
print("\n\nTracking context:\n")
289+
print("\tmindist = {0}".format(tc.mindist))
290+
print("\twindow_width = {0}".format(tc.window_width))
291+
print("\twindow_height = {0}".format(tc.window_height))
292+
print("\tsequentialMode = {0}".format(tc.sequentialMode))
293+
print("\tsmoothBeforeSelecting = {0}".format(tc.smoothBeforeSelecting))
294+
print("\twriteInternalImages = {0}".format(tc.writeInternalImages))
295+
296+
print("\tmin_eigenvalue = {0}".format(tc.min_eigenvalue))
297+
print("\tmin_determinant = {0}".format(tc.min_determinant))
298+
print("\tmin_displacement = {0}".format(tc.min_displacement))
299+
print("\tmax_iterations = {0}".format(tc.max_iterations))
300+
print("\tmax_residue = {0}".format(tc.max_residue))
301+
print("\tgrad_sigma = {0}".format(tc.grad_sigma))
302+
print("\tsmooth_sigma_fact = {0}".format(tc.smooth_sigma_fact))
303+
print("\tpyramid_sigma_fact = {0}".format(tc.pyramid_sigma_fact))
304+
print("\tnSkippedPixels = {0}".format(tc.nSkippedPixels))
305+
print("\tborderx = {0}".format(tc.borderx))
306+
print("\tbordery = {0}".format(tc.bordery))
307+
print("\tnPyramidLevels = {0}".format(tc.nPyramidLevels))
308+
print("\tsubsampling = {0}".format(tc.subsampling))
309+
310+
print("\n\tpyramid_last = {0}".format(tc.pyramid_last))
311+
print("\tpyramid_last_gradx = {0}".format(tc.pyramid_last_gradx))
312+
print("\tpyramid_last_grady = {0}".format(tc.pyramid_last_grady))
313+
print("\n")
312314

313315
#*********************************************************************
314316
#* KLTCountRemainingFeatures

pyramid.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ def Compute(self, img, sigma_fact):
6262

6363
# Subsample
6464
oldncols = ncols
65-
ncols /= subsampling
66-
nrows /= subsampling
65+
ncols = int(ncols / subsampling)
66+
nrows = int(nrows / subsampling)
6767
#subsampImg = Image.new("F",(ncols,nrows))
6868
subsampImg = np.empty((nrows,ncols), np.float32)
6969
#subsampImgL = subsampImg.load()
7070
for y in range(nrows):
7171
for x in range(ncols):
72-
subsampImg[y,x] = tmpimg[subsampling*y+subhalf, subsampling*x+subhalf]
72+
subsampImg[y,x] = tmpimg[int(subsampling*y+subhalf), int(subsampling*x+subhalf)]
7373

7474
self.img[i] = subsampImg
7575

selectGoodFeatures.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from __future__ import print_function
23
import math, numpy as np
34
from PIL import Image
45
from klt import *
@@ -89,7 +90,7 @@ def _enforceMinimumDistance(pointlist, featurelist, ncols, nrows, mindist, min_e
8990
featurelist[indx].aff_Ayx = 0.0
9091
featurelist[indx].aff_Axy = 0.0
9192
featurelist[indx].aff_Ayy = 1.0
92-
indx = indx + 1
93+
indx = indx + 1
9394
break
9495

9596
pointdata = pointlist[pointlistIndx]
@@ -230,11 +231,11 @@ def _KLTSelectGoodFeatures(tc,img,nFeatures,mode):
230231
grady, borderx, bordery, window_hw, window_hh, tc.nSkippedPixels)
231232

232233
# Sort the features
233-
pointlist = zip(pointlistval, pointlistx, pointlisty)
234+
pointlist = list(zip(pointlistval, pointlistx, pointlisty))
234235
pointlist.sort()
235236
pointlist.reverse()
236237

237-
#print pointlist
238+
#print(pointlist)
238239

239240
# Check tc.mindist
240241
if tc.mindist < 0:
@@ -281,14 +282,14 @@ def KLTSelectGoodFeatures(tc, img, nFeatures):
281282

282283
#int ncols, int nrows,
283284
if KLT_verbose >= 1:
284-
print "(KLT) Selecting the {0} best features from a {1} by {2} image... ".format(nFeatures, ncols, nrows)
285+
print("(KLT) Selecting the {0} best features from a {1} by {2} image... ".format(nFeatures, ncols, nrows))
285286

286287
fl = _KLTSelectGoodFeatures(tc, img, nFeatures, selectionMode.SELECTING_ALL)
287288

288289
if KLT_verbose >= 1:
289-
print "\n\t{0} features found.\n".format(KLTCountRemainingFeatures(fl))
290-
if tc.writeInternalImages:
291-
print "\tWrote images to 'kltimg_sgfrlf*.pgm'.\n"
290+
print("\n\t{0} features found.\n".format(KLTCountRemainingFeatures(fl)))
291+
if tc.writeInternalImages:
292+
print("\tWrote images to 'kltimg_sgfrlf*.pgm'.\n")
292293

293294
return fl
294295

trackFeatures.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#*
44
#*********************************************************************/
55

6+
from __future__ import print_function
67
from selectGoodFeatures import KLT_verbose
78
from klt import *
89
from error import *
@@ -117,7 +118,7 @@ def _trackFeature(
117118
if status == kltState.KLT_TRACKED and max_residue is not None:
118119
if lighting_insensitive:
119120
trackFeaturesUtils.computeIntensityDifferenceLightingInsensitive(img1Patch, img2, x1, y1, x2, y2, workingPatch, imgdiff)
120-
else:
121+
else:
121122
trackFeaturesUtils.computeIntensityDifference(img1Patch, img2, x2, y2, workingPatch, imgdiff)
122123

123124
if np.abs(np.array(imgdiff)).sum()/(width*height) > max_residue:
@@ -218,8 +219,8 @@ def KLTTrackFeatures(tc, img1, img2, featurelist):
218219
DEBUG_AFFINE_MAPPING = False
219220

220221
if KLT_verbose >= 1:
221-
print "(KLT) Tracking {0} features in a {1} by {2} image... ".format( \
222-
KLTCountRemainingFeatures(featurelist), ncols, nrows)
222+
print("(KLT) Tracking {0} features in a {1} by {2} image... ".format( \
223+
KLTCountRemainingFeatures(featurelist), ncols, nrows))
223224

224225
# Check window size (and correct if necessary)
225226
if tc.window_width % 2 != 1:
@@ -403,9 +404,9 @@ def KLTTrackFeatures(tc, img1, img2, featurelist):
403404
tc.pyramid_last_grady = pyramid2_grady
404405

405406
if KLT_verbose >= 1:
406-
print "\n\t{0} features successfully tracked.".format(KLTCountRemainingFeatures(featurelist))
407+
print("\n\t{0} features successfully tracked.".format(KLTCountRemainingFeatures(featurelist)))
407408
if tc.writeInternalImages:
408-
print "\tWrote images to 'kltimg_tf*.pgm'."
409+
print("\tWrote images to 'kltimg_tf*.pgm'.")
409410

410411

411412

writeFeatures.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
from __future__ import print_function
23
from selectGoodFeatures import KLT_verbose
34
from klt import *
45

@@ -16,7 +17,7 @@ def KLTWriteFeatureListToPPM(featurelist, greyimg, filename):
1617
ncols, nrows = greyimg.size
1718

1819
if KLT_verbose:
19-
print "(KLT) Writing {0} features to PPM file: '{1}'".format(KLTCountRemainingFeatures(featurelist), filename)
20+
print("(KLT) Writing {0} features to PPM file: '{1}'".format(KLTCountRemainingFeatures(featurelist), filename))
2021

2122
tmp = greyimg.copy()
2223
tmp = tmp.convert("RGB")
@@ -46,7 +47,7 @@ def KLTWriteFeatureListToPPM(featurelist, greyimg, filename):
4647
#* INPUTS
4748
#* fname: name of file to write data; if NULL, then print to stderr
4849
#* fmt: format for printing (e.g., "%5.1f" or "%3d");
49-
#* if NULL, and if fname is not NULL, then write to binary file.
50+
#* if NULL, and if fname is not NULL, then write to binary file.
5051
#*
5152

5253
def KLTWriteFeatureList(fl, fname, fmt):
@@ -58,11 +59,11 @@ def KLTWriteFeatureList(fl, fname, fmt):
5859

5960
fmtStr = "binary" if fmt is None else "text"
6061
if KLT_verbose >= 1 and fname is not None:
61-
print "(KLT) Writing feature list to {0} file: '{1}'".format(fmtStr, fname)
62+
print("(KLT) Writing feature list to {0} file: '{1}'".format(fmtStr, fname))
6263

6364
if fmt is not None: # text file or stderr
64-
fp = _printSetupTxt(fname, fmt, format, type);
65-
_printHeader(fp, format, FEATURE_LIST, 0, fl.nFeatures);
65+
fp = _printSetupTxt(fname, fmt, format, type)
66+
_printHeader(fp, format, FEATURE_LIST, 0, fl.nFeatures)
6667

6768
#for (i = 0 ; i < fl->nFeatures ; i++):
6869
# fprintf(fp, "%7d | ", i)

0 commit comments

Comments
 (0)