私は回転行列を使って各ベクトルを変換したい3D配列(ベクトルの2次元配列)を持っています。回転は、cols
とrows
と呼ばれるラジアン角度値の2つの別々の2D配列にあります。Numpyで値のペアのための行列を生成する
私は既にNumPyにPythonループを使わずに私の角度を計算させることができました。今私は、NumPyに回転行列を生成させる方法を探しています。うまくいけば、パフォーマンスが大幅に向上します。
size = img.shape[:2]
# Create an array that assigns each pixel the percentage of
# the correction (value between -1 and 1, distributed linearly).
cols = np.array([np.arange(size[1]) for __ in range(size[0])]) /(size[1] - 1) * 2 - 1
rows = np.array([np.arange(size[0]) for __ in range(size[1])]).T/(size[0] - 1) * 2 - 1
# Atan distribution based on F-number and Sensor size.
cols = np.arctan(sh * cols/(2 * f))
rows = np.arctan(sv * rows/(2 * f))
### This is the loop that I would like to remove and find a
### clever way to make NumPy do the same operation natively.
for i in range(size[0]):
for j in range(size[1]):
ah = cols[i,j]
av = rows[i,j]
# Y-rotation.
mat = np.matrix([
[ np.cos(ah), 0, np.sin(ah)],
[0, 1, 0],
[-np.sin(ah), 0, np.cos(ah)]
])
# X-rotation.
mat *= np.matrix([
[1, 0, 0],
[0, np.cos(av), -np.sin(av)],
[0, np.sin(av), np.cos(av)]
])
img[i,j] = img[i,j] * mat
return img
NumPy操作でループを書き直す方法はありますか?
一つを 'av'を使用する必要がありますか? – kennytm
@kennytmこれは正しいです、このエラーを発見してくれてありがとう! –