简体   繁体   English

线性变换后如何重新分配numpy索引

[英]How to reassign numpy indices after linear transformation

Suppose I have rotated all the indices of a numpy array by an angle (matrix mult with rotation matrix). 假设我已将一个numpy数组的所有索引旋转了一个角度(矩阵多为旋转矩阵)。

These rotated indices are in a tensor of dimensions (width_img*height_img,2) (assume width= height for this case) where img is the numpy array. 这些旋转索引的尺寸为张量(width_img * height_img,2)(在这种情况下,假设width = height),其中img是numpy数组。

Is there a way of of using these indices to rotate the image ? 有没有一种使用这些索引旋转图像的方法? as in reasigning them some how ? 如在重新分配他们一些方法?

I tried reshaping to the form r, c in the same form as np.indices outputs but they come out in the wrong order any suggestions ?? 我试图以与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]

Not sure this reflects what you need, but following this answer increment-given-indices-in-a-matrix , you should consider using ravel: 不知道这反映了你所需要的,但下面这个答案增量赋予指数中-IN-A-矩阵 ,你应该考虑使用拉威尔:

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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM