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 3 では、そのエラーが発生するような方法で (int を分割するときに int に丸めないように) 除算が変更されたため、確かに Python 2 を使用していますか? (うーん、報告された正確なエラーは異なるので、そうではないと思います)

(2)widthおよびheight変数を使用しているにもかかわらず、コードの上部にあるコメントは、それが正方行列専用("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 に答える