Page MenuHomec4science

warpImg.py
No OneTemporary

File Metadata

Created
Tue, Jul 8, 00:30

warpImg.py

import numpy as np
from scipy import ndimage #interpolate
from timeit import default_timer as timer
def warpImg(img, u, v):
xx, yy = np.meshgrid(np.arange(img.shape[0]),np.arange(img.shape[1]))
xw = (xx + v).flatten()
yw = (yy + u).flatten()
#start = timer()
#warpedImg = ndimage.map_coordinates(img, [xw, yw], order=1)
#end = timer()
#print('time map_coordinates take {}'.format(end-start))
#start = timer()
#warpedImg = warpedImg.reshape(img.shape)
#end = timer()
#print('time it took to reshape the wapred image {}'.format(end-start))
#return warpedImg
start = timer()
warp_function = interpolate.interp2d(xx, yy, img, 'linear')
end = timer()
print('Creating interpolation function for warping took {}'.format(end-start))
warpedImg = np.zeros(len(xw))
#TODO find a way to avoid forloop to speed things up
start = timer()
for i in range(len(xw)):
warpedImg[i] = warp_function(yw[i],xw[i])
end = timer()
print('Time of for-loop in warpImg {}'.format(end-start))
start = timer()
warpedImg = np.reshape(warpedImg, img.shape)
end = timer()
print('Repshapeing in warpImg {}'.format(end-start))

Event Timeline