2

画像の各ピクセルのデータを含むこのデータ キューブがあります (ハイパースペクトル イメージングによく似ています)。効率的な方法で、画像の各ピクセルに線を当てはめようとしています。今、私は次のようにしています:

私のデータキューブは 6X1024x1024 numpy 配列で、データの独立変数を含む別の変数があります。

map = np.zeros((1024,1024))
for i in np.mgrid[1:1024]:
    for j in np.mgrid[1:1024]:
        x = independent_variable # This is my independent variable
        y = spec_cube[:,i,j] # The Y data to be fitted is the power at each scale, for a pixel
        index = polyfit(x,y,1) # Outputs the slope and the offset
        map[i,j] = index[0] # The pixel value is the index

入れ子になった for ループは、一般的には最悪の行為であることはわかっていますが、それ以上の方法は思いつきません。

次のことを試しましたが、次のエラーが表示されます:「ValueError: unpack する値が多すぎます」

map = np.zeros((1024,1024))
for i,j in map:
    x = independent_variable # This is my independent variable
    y = spec_cube[:,i,j] # The Y data to be fitted is the power at each scale, for a pixel
    index = polyfit(x,y,1) # Outputs the slope and the offset
    map[i,j] = index[0] # The pixel value is the index
4

2 に答える 2

5

高速化する方法の 1 つ: 次を使用しますitertools.product

for (i, j) in itertools.product(np.mgrid[1:1024], np.mgrid[1:1024]):
    ... stuff ...

改善 (Python 2.7.1):

[2]: def multiline():
   ...: np.mgrid[1:1024] の i の場合:
   ...: np.mgrid[1:1024] の j の場合:
   ...: 合格
   ...:        

[3]: def single_line():
   ...: for i, j in product(np.mgrid[1:1024], np.mgrid[1:1024]):
   ...: 合格
   ...:    

[4]: itertools import プロダクトから

[5]: %timeit multiline()
10 ループ、ベストオブ 3: ループあたり 138 ミリ秒

[6]: %timeit single_line()
10 ループ、ベストオブ 3: ループあたり 75.6 ミリ秒
于 2012-06-07T02:15:14.190 に答える
3

ループ内の操作は直線の傾きを見つけることだったので、精度の低い方法を使用しましたが、配列操作を使用しました。基本的に、私が行った勾配を見つけるために、隣接する各ポイントのデルタY /デルタXを実行し、次にすべての勾配を平均しました。

ほんの一瞬かかることがわかりました。

新しいコードは次のとおりです。

map = np.zeros((spec_cube.shape[1],spec_cube.shape[2])) # This will be the power index map
x = scale_array
for i in np.mgrid[1:spec_cupe.shape[0]]:
  spec_cube[i-1] = (spec_cube[i]-spec_cube[i-1])/(scale_array[i]-scale_array[i-1])
  map += spec_cube[i-1]
map /= (spec_cube.shape[0]-1)

私のスクリプトは420秒から9.66秒になりました!

于 2012-06-08T11:51:27.253 に答える