numpy - Applying a Fast Coordinate Transformation in Python -


i have simple 2x2 transformation matrix, s, encodes liner transformation of coordinates such x' = sx.

i have generated set of uniformley distributed coordinates on grid using np.meshgrid() function , @ moment traverse each coordinate , apply transformation @ coordinate coordinate level. unfortunately, slow large arrays. there fast ways of doing this? thanks!

import numpy np  image_dimension = 1024 image_index = np.arange(0,image_dimension,1)  xx, yy = np.meshgrid(image_index,image_index)  # pre-calculated transformation matrix. s = np.array([[ -2.45963439e+04,  -2.54997726e-01], [  3.55680731e-02, -2.48005486e+04]])  xx_f = xx.flatten() yy_f = yy.flatten()  x_t in range(0, image_dimension*image_dimension):      # current (x,y) coordinate.     x_y_in = np.matrix([[xx_f[x_t]],[yy_f[x_t]]])      # perform transformation x.     optout =  s * x_y_in      # store new coordinate.     xx_f[x_t] = np.array(optout)[0][0]     yy_f[x_t] = np.array(optout)[1][0]  # reshape output xx_t = xx_f.reshape((image_dimension, image_dimension)) yy_t = yy_f.reshape((image_dimension, image_dimension)) 

loops slow in python. better use vectorization. in nutshell, idea let numpy loops in c, faster.

you can express problem matrix multiplications x' = sx, put points in x , transform them 1 call numpy's dot product:

xy = np.vstack([xx.ravel(), yy.ravel()]) xy_t = np.dot(s, xy) xx_t, yy_t = xy_t.reshape((2, image_dimension, image_dimension)) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -