-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpyramid.py
83 lines (62 loc) · 2.11 KB
/
pyramid.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
#*********************************************************************
#* pyramid.py
#*
#*********************************************************************
from error import *
from convolve import *
import numpy as np
#*********************************************************************
#*
#*
class KLTPyramid:
def __init__(self, ncols, nrows, subsampling, nlevels):
if subsampling != 2 and subsampling != 4 and \
subsampling != 8 and subsampling != 16 and subsampling != 32:
KLTError("(_KLTCreatePyramid) Pyramid's subsampling must " + \
"be either 2, 4, 8, 16, or 32")
# Set parameters
self.subsampling = subsampling;
self.nLevels = nlevels;
self.img = []
self.ncols = []
self.nrows = []
# Allocate memory for each level of pyramid and assign pointers
for i in range(nlevels):
self.img.append(None)
self.ncols.append(ncols)
self.nrows.append(nrows)
ncols /= subsampling
nrows /= subsampling
def Compute(self, img, sigma_fact):
nrows = img.shape[0]
ncols = img.shape[1]
subsampling = self.subsampling
subhalf = subsampling / 2
sigma = subsampling * sigma_fact; # empirically determined
#int oldncols;
#int i, x, y;
if subsampling != 2 and subsampling != 4 and \
subsampling != 8 and subsampling != 16 and subsampling != 32:
KLTError("(_KLTComputePyramid) Pyramid's subsampling must " + \
"be either 2, 4, 8, 16, or 32")
assert self.ncols[0] == ncols
assert self.nrows[0] == nrows
# Copy original image to level 0 of pyramid
self.img[0] = img
currimg = img
for i in range(1,self.nLevels):
tmpimg = KLTComputeSmoothedImage(currimg, sigma)
#tmpimgL = tmpimg.load()
# Subsample
oldncols = ncols
ncols = int(ncols / subsampling)
nrows = int(nrows / subsampling)
#subsampImg = Image.new("F",(ncols,nrows))
subsampImg = np.empty((nrows,ncols), np.float32)
#subsampImgL = subsampImg.load()
for y in range(nrows):
for x in range(ncols):
subsampImg[y,x] = tmpimg[int(subsampling*y+subhalf), int(subsampling*x+subhalf)]
self.img[i] = subsampImg
# Reassign current image
currimg = self.img[i]