在python中加速这种插值

时间:2011-11-16 21:26:31

标签: python optimization numpy scipy interpolation

我有一个图像处理问题我正在使用numpy和scipy在python中解决。简而言之,我有一个图像,我想应用许多局部收缩。我的原型代码正在运行,最终的图像看起来很棒。但是,处理时间已经成为我们应用中的一个严重瓶颈。你能帮我加速我的图像处理代码吗?

我试图将我们的代码归结为下面的'卡通'版本。分析表明我将大部分时间花在插值上。是否有明显的方法来加快执行速度?

import cProfile, pstats
import numpy
from scipy.ndimage import interpolation

def get_centered_subimage(
    center_point, window_size, image):
    x, y = numpy.round(center_point).astype(int)
    xSl = slice(max(x-window_size-1, 0), x+window_size+2)
    ySl = slice(max(y-window_size-1, 0), y+window_size+2)
    subimage = image[xSl, ySl]
    interpolation.shift(
        subimage, shift=(x, y)-center_point, output=subimage)
    return subimage[1:-1, 1:-1]

"""In real life, this is experimental data"""
im = numpy.zeros((1000, 1000), dtype=float)
"""In real life, this mask is a non-zero pattern"""
window_radius = 10
mask = numpy.zeros((2*window_radius+1, 2*window_radius+1), dtype=float)
"""The x, y coordinates in the output image"""
new_grid_x = numpy.linspace(0, im.shape[0]-1, 2*im.shape[0])
new_grid_y = numpy.linspace(0, im.shape[1]-1, 2*im.shape[1])


"""The grid we'll end up interpolating onto"""
grid_step_x = new_grid_x[1] - new_grid_x[0]
grid_step_y = new_grid_y[1] - new_grid_y[0]
subgrid_radius = numpy.floor(
    (-1 + window_radius * 0.5 / grid_step_x,
     -1 + window_radius * 0.5 / grid_step_y))
subgrid = (
    window_radius + 2 * grid_step_x * numpy.arange(
        -subgrid_radius[0], subgrid_radius[0] + 1),
    window_radius + 2 * grid_step_y * numpy.arange(
        -subgrid_radius[1], subgrid_radius[1] + 1))
subgrid_points = ((2*subgrid_radius[0] + 1) *
                  (2*subgrid_radius[1] + 1))

"""The coordinates of the set of spots we we want to contract. In real
life, this set is non-random:"""
numpy.random.seed(0)
num_points = 10000
center_points = numpy.random.random(2*num_points).reshape(num_points, 2)
center_points[:, 0] *= im.shape[0]
center_points[:, 1] *= im.shape[1]

"""The output image"""
final_image = numpy.zeros(
    (new_grid_x.shape[0], new_grid_y.shape[0]), dtype=numpy.float)

def profile_me():
    for m, cp in enumerate(center_points):
        """Take an image centered on each illumination point"""
        spot_image = get_centered_subimage(
            center_point=cp, window_size=window_radius, image=im)
        if spot_image.shape != (2*window_radius+1, 2*window_radius+1):
            continue #Skip to the next spot
        """Mask the image"""
        masked_image = mask * spot_image
        """Resample the image"""
        nearest_grid_index = numpy.round(
                (cp - (new_grid_x[0], new_grid_y[0])) /
                (grid_step_x, grid_step_y))
        nearest_grid_point = (
            (new_grid_x[0], new_grid_y[0]) +
            (grid_step_x, grid_step_y) * nearest_grid_index)
        new_coordinates = numpy.meshgrid(
            subgrid[0] + 2 * (nearest_grid_point[0] - cp[0]),
            subgrid[1] + 2 * (nearest_grid_point[1] - cp[1]))
        resampled_image = interpolation.map_coordinates(
            masked_image,
            (new_coordinates[0].reshape(subgrid_points),
             new_coordinates[1].reshape(subgrid_points))
            ).reshape(2*subgrid_radius[1]+1,
                      2*subgrid_radius[0]+1).T
        """Add the recentered image back to the scan grid"""
        final_image[
            nearest_grid_index[0]-subgrid_radius[0]:
            nearest_grid_index[0]+subgrid_radius[0]+1,
            nearest_grid_index[1]-subgrid_radius[1]:
            nearest_grid_index[1]+subgrid_radius[1]+1,
            ] += resampled_image

cProfile.run('profile_me()', 'profile_results')
p = pstats.Stats('profile_results')
p.strip_dirs().sort_stats('cumulative').print_stats(10)

对代码的作用的模糊解释:

我们从像素化的2D图像开始,在我们的图像中一组任意(x,y)点通常不落在整数网格上。对于每个(x,y)点,我想将图像乘以在该点上以精确为中心的小掩模。接下来,在最终将经处理的子图像添加到最终图像之前,我们将遮蔽区域收缩/扩展有限量,最终图像可能与原始图像不具有相同的像素尺寸。 (不是我最好的解释。好吧)。

1 个答案:

答案 0 :(得分:3)

我很确定,正如你所说,大部分计算时间发生在interpolate.map_coordinates(…),在center_points的每次迭代都会调用一次,这里10,000次。通常,使用numpy / scipy堆栈,您希望大型数组上的重复任务发生在本机Numpy / Scipy函数中 - 即在同构数据的C循环中 - 而不是Python中的显式。

可能会加速插值的一种策略是:

,这也会增加使用的内存量
  • 首先,在三维数组(masked_image
  • 中获取所有子图像(此处命名为window_radius x window_radius x center_points.size
  • 使用ufunc创建一个numpy.frompyfunc(读取,它很有用),它包含了必须在每个子图像上完成的工作,它应返回另一个三维数组({{1} })。简而言之,这创建了python函数的矢量化版本,可以在数组上以元素方式进行广播。
  • 通过对第三维进行求和来构建最终图像。

希望能让你更接近目标!