0

I am trying to run this code for a student project:

The authors blog with complete Python-code

Only that function:

def fwt97(s, width, height):
''' Forward Cohen-Daubechies-Feauveau 9 tap / 7 tap wavelet transform   
performed on all columns of the 2D n*n matrix signal s via lifting.
The returned result is s, the modified input matrix.
The highpass and lowpass results are stored on the left half and right
half of s respectively, after the matrix is 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
# Another k used by P. Getreuer is 1.1496043988602418

for col in range(width): # Do the 1D transform on all cols:
    ''' Core 1D lifting process in this loop. '''
    ''' Lifting is done on the cols. '''

    # Predict 1. y1
    for 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
    for 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.
    for 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.
    for 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 for i in range(height)]
for row in range(height):
    for col in range(width):
        # k1 and k2 scale the vals
        # simultaneously transpose the matrix when deinterleaving
        if row % 2 == 0: # even
            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 to s:
for row in range(width):
    for col in range(height):
        s[row][col] = temp_bank[row][col]

return s

According to the author, the code should run, but I am receiving this 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 help would be great!

Cheers, Tobi

1 Answers1

1

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

(2) despite using width and height variables the comments at the top of the code indicate that it's for square matrices only ("n*n"). do you have height = width? it's clear the code won't work otherwise because the transpose is assigned to the original matrix.

for me, the 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 your error, as expected.

in fact, the code in general is weird. looks like it was written by a fortran or c programmer, because you don't need to pass in the dimensions at all. it would be better to have:

def fwt97(s):
    height = len(s)
    width = height
    for row in s:
        assert len(row) == width, "not square"
    ...
andrew cooke
  • 45,717
  • 10
  • 93
  • 143
  • Thanks for your reply! I am storing an image inside of the matrix. So, now i found out that my error is caused by a not-square image. You can find the whole code on that page [The authors blog with complete Python-code](http://www.olhovsky.com/2009/03/2d-cdf-97-wavelet-transform-in-python/) Looks like I have to modify the entire code.. sigh.. – digitobe Jun 07 '12 at 14:58
  • most will probably carry through. but when you calculate the transpose, don't try to stick each element in-place in the original matrix; generate a new matrix (with a different shape). good luck... – andrew cooke Jun 07 '12 at 15:00
  • Cheers Andrew, I'll dig into it next week!! So far I love the PIL library. -Tobi – digitobe Jun 19 '12 at 23:07