0

我正在尝试为学生项目运行此代码:

作者博客包含完整的 Python 代码

只有那个功能:

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

根据作者的说法,代码应该运行,但我收到此错误:

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

测试:Windows 7 / Mac OS 10.7.3
Python 2.7.3
PIL 1.1.7

任何帮助都会很棒!

干杯,托比

4

1 回答 1

1

(1) 您确定您使用的是 python 2,因为在 python 3 中,除法发生了变化(在除法时不舍入为 int)会导致该错误?(嗯,虽然报告的确切错误是不同的,所以我想不是那个)

(2) 尽管使用widthheight变量,但代码顶部的注释表明它仅适用于方阵(“n*n”)。你有height=width吗?很明显,否则代码将无法正常工作,因为转置已分配给原始矩阵。

对我来说,以下在 python 2.7 中可以正常工作:

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

尽管

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

正如预期的那样给出你的错误。

事实上,一般的代码很奇怪。看起来它是由 fortran 或 c 程序员编写的,因为您根本不需要传递维度。最好有:

def fwt97(s):
    height = len(s)
    width = height
    for row in s:
        assert len(row) == width, "not square"
    ...
于 2012-06-05T10:40:44.950 回答