3
我现在正在使用numpy和scipy在python中进行图像处理。我有一段可以放大图像的代码,但不知道这是如何工作的。任何人都可以请解释这个python代码如何逐行工作?
所以请一些python/numpy的专家在python中逐行解释一下。我总是渴望学习。
import numpy as N
import os.path
import scipy.signal
import scipy.interpolate
import matplotlib.pyplot as plt
import matplotlib.cm as cm
def enlarge(img, rowscale, colscale, method='linear'):
x, y = N.meshgrid(N.arange(img.shape[1]), N.arange(img.shape[0]))
pts = N.column_stack((x.ravel(), y.ravel()))
xx, yy = N.mgrid[0.:float(img.shape[1]):1/float(colscale),
0.:float(img.shape[0]):1/float(rowscale)]
large = scipy.interpolate.griddata(pts, img.flatten(), (xx, yy), method).T
large[-1,:] = large[-2,:]
large[:,-1] = large[:,-2]
return large
非常感谢。
Python中每个语句的缩进都很重要。请修正您的代码片段的格式。 – Johnsyweb 2011-05-22 05:54:02
感谢和抱歉。我添加了这些导入..以清除它 – 2011-05-22 06:03:50
请注意,在这里使用'griddata'不是最有效的选项,因为网格总是矩形的。更有效的选项是:'scipy.interpolate.RectBivariateSpline'和'scipy.ndimage.zoom'。 – 2011-05-31 08:24:50