Francisco Vargas
Francisco Vargas

Reputation: 652

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).

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.

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

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]

Upvotes: 0

Views: 279

Answers (1)

Niemerds
Niemerds

Reputation: 942

Not sure this reflects what you need, but following this answer increment-given-indices-in-a-matrix, you should consider using 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]]

Upvotes: 2

Related Questions