traceback - Wavelet Transform in Python -> IndexError: list assignment index out of range -


i trying run code student project:

the authors blog complete python-code

only function:

def fwt97(s, width, height): ''' forward cohen-daubechies-feauveau 9 tap / 7 tap wavelet transform    performed on columns of 2d n*n matrix signal s via lifting. returned result s, modified input matrix. highpass , lowpass results stored on left half , right half of s respectively, after matrix transposed. '''  # 9/7 coefficients: a1 = -1.586134342 a2 = -0.05298011854 a3 = 0.8829110762 a4 = 0.4435068522  # scale coeff: k1 = 0.81289306611596146 # 1/1.230174104914 k2 = 0.61508705245700002 # 1.230174104914/2 # k used p. getreuer 1.1496043988602418  col in range(width): # 1d transform on cols:     ''' core 1d lifting process in loop. '''     ''' lifting done on cols. '''      # predict 1. y1     row in range(1, height-1, 2):         s[row][col] += a1 * (s[row-1][col] + s[row+1][col])        s[height-1][col] += 2 * a1 * s[height-2][col] # symmetric extension      # update 1. y0     row in range(2, height, 2):         s[row][col] += a2 * (s[row-1][col] + s[row+1][col])     s[0][col] +=  2 * a2 * s[1][col] # symmetric extension      # predict 2.     row in range(1, height-1, 2):         s[row][col] += a3 * (s[row-1][col] + s[row+1][col])     s[height-1][col] += 2 * a3 * s[height-2][col]      # update 2.     row in range(2, height, 2):         s[row][col] += a4 * (s[row-1][col] + s[row+1][col])     s[0][col] += 2 * a4 * s[1][col]  # de-interleave temp_bank = [[0]*width in range(height)] row in range(height):     col in range(width):         # k1 , k2 scale vals         # simultaneously transpose matrix when deinterleaving         if row % 2 == 0: #             temp_bank[col][row/2] = k1 * s[row][col]         else:            # odd             temp_bank[col][row/2 + height/2] = k2 * s[row][col]  # write temp_bank s: row in range(width):     col in range(height):         s[row][col] = temp_bank[row][col]  return s 

according author, code should run, receiving error:

traceback (most recent call last): file “wavelet_02.py”, line 200, in m = fwt97_2d(m, 3) file “wavelet_02.py”, line 27, in fwt97_2d m = fwt97(m, w, h) # cols file “wavelet_02.py”, line 108, in fwt97 temp_bank[col][row/2 + height/2] = k2 * s[row][col] indexerror: list assignment index out of range 

tested on: windows 7 / mac os 10.7.3
python 2.7.3
pil 1.1.7

any great!

cheers, tobi

(1) sure you're using python 2, because in python 3 division changed (to not round int when dividing ints) in way cause error? (hmm, although exact error reported different guess it's not that)

(2) despite using width , height variables comments @ top of code indicate it's square matrices only ("n*n"). have height = width? it's clear code won't work otherwise because transpose assigned original matrix.

for me, following works fine in python 2.7:

print(fwt97([[1,2],[3,4]], 2, 2)) 

while

print(fwt97([[1,2],[3,4]], 2, 1)) 

gives error, expected.

in fact, code in general weird. looks written fortran or c programmer, because don't need pass in dimensions @ all. better have:

def fwt97(s):     height = len(s)     width = height     row in s:         assert len(row) == width, "not square"     ... 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -