如何在线性变换后重新分配 numpy 索引
How to reassign numpy indices after linear transformation
假设我已经将 numpy 数组的所有索引旋转了一个角度(矩阵 mult 与旋转矩阵)。
这些旋转后的索引在维度为 (width_img*height_img,2) 的张量中(在这种情况下假设宽度=高度),其中 img 是 numpy 数组。
有没有办法使用这些索引来旋转图像?比如如何重新分配它们?
我尝试以与 np.indices
输出相同的形式重塑为 r, c
形式,但它们以错误的顺序出现,有什么建议吗?
import numpy as np
img = np.random.randn(2,2)
# Altered position of original indicies in img
indices = np.array([[1,1],[0,1],[0,0],[1,1]])
r, c = indices.reshape(2,2,2)
img2 = np.zeros((2,2))
img2 += img[r,c]
不确定这是否反映了您的需求,但请遵循此答案
increment-given-indices-in-a-matrix,你应该考虑使用 ravel:
import numpy as np
m = np.array([0,1,2,3]).reshape(2,2)
indices_r90 = np.array([[0,1], [0,0], [1,1], [1,0]])
indices_r90_t = indices_r90.T
ravel_ind = indices_r90_t[0] + indices_r90_t[1]*m.shape[0]
print m.ravel()[ravel_ind].reshape(2,2)
>>> [[2 0]
>>> [3 1]]
假设我已经将 numpy 数组的所有索引旋转了一个角度(矩阵 mult 与旋转矩阵)。
这些旋转后的索引在维度为 (width_img*height_img,2) 的张量中(在这种情况下假设宽度=高度),其中 img 是 numpy 数组。
有没有办法使用这些索引来旋转图像?比如如何重新分配它们?
我尝试以与 np.indices
输出相同的形式重塑为 r, c
形式,但它们以错误的顺序出现,有什么建议吗?
import numpy as np
img = np.random.randn(2,2)
# Altered position of original indicies in img
indices = np.array([[1,1],[0,1],[0,0],[1,1]])
r, c = indices.reshape(2,2,2)
img2 = np.zeros((2,2))
img2 += img[r,c]
不确定这是否反映了您的需求,但请遵循此答案 increment-given-indices-in-a-matrix,你应该考虑使用 ravel:
import numpy as np
m = np.array([0,1,2,3]).reshape(2,2)
indices_r90 = np.array([[0,1], [0,0], [1,1], [1,0]])
indices_r90_t = indices_r90.T
ravel_ind = indices_r90_t[0] + indices_r90_t[1]*m.shape[0]
print m.ravel()[ravel_ind].reshape(2,2)
>>> [[2 0]
>>> [3 1]]