회전 배열을 사용하여 각 벡터를 변환하려는 3D 배열 (벡터의 2D 배열)이 있습니다. 회전은 cols
및 rows
이라는 라디안 각도 값의 두 개의 별도 2D 배열에 있습니다.Numpy에서 값 쌍에 대한 행렬을 생성합니다.
저는 NumPy에서 이미 파이썬 루프없이 각도를 계산할 수있었습니다. 이제 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 맞습니다.이 오류를 발견해 주셔서 감사합니다! –