def imresize(img, sz):
"""
Resize image
Input:
img: A grayscale image
sz: A tuple with the new size (rows, cols)
Output:
Ir: Resized image
"""
if np.all(img.shape==sz):
return img;
factors = (np.array(sz)).astype('f') / (np.array(img.shape)).astype('f')
if np.any(factors < 1): # smooth before downsampling
sigmas = (1.0/factors)/3.0
#print img.shape, sz, sigmas
I_filter = ndimage.filters.gaussian_filter(img,sigmas)
else:
I_filter = img
u,v = np.meshgrid(np.arange(0,sz[1]).astype('f'), np.arange(0,sz[0]).astype('f'))
fx = (float(img.shape[1])) / (sz[1]) # multiplicative factors mapping new coords -> old coords
fy = (float(img.shape[0])) / (sz[0])
u *= fx; u += (1.0/factors[1])/2 - 1 + 0.5 # sample from correct position
v *= fy; v += (1.0/factors[0])/2 - 1 + 0.5
# bilinear interpolation
Ir = ndimage.map_coordinates(I_filter, np.vstack((v.flatten().transpose(),u.flatten().transpose())), order=1, mode='nearest')
Ir = np.reshape(Ir, (sz[0], sz[1]))
return Ir
评论列表
文章目录