Python快速对称中心图像下采样算法

时间:2015-04-09 17:35:11

标签: python image performance numpy downsampling

我正在尝试加速Python中一个简单的对称居中图像下采样算法。我已经使用一种天真的方法将其编码为下限基准,但是我想让它更快地工作。

为简单起见,我的图像是一个分辨率为4608x4608的圆圈(我将使用此刻度的分辨率),我希望下采样图像的系数为9(即512x512)。下面是我生成的代码,它以高分辨率创建图像,并将其下采样9倍。

所有这些基本上都是从高分辨率映射一个像素。在低分辨率空间中对称一个空间(围绕质心对称)并将高分辨率中给定区域中的所有像素与低分辨率中的一个像素相加。

import numpy as np
import matplotlib.pyplot as plt
import time

print 'rendering circle at high res'

# image dimensions.
dim = 4608

# generate high sampled image.
xx, yy = np.mgrid[:dim, :dim]
highRes = (xx - dim/2) ** 2 + (yy - dim/2) ** 2
print 'render done'

print 'downsampling'
t0 = time.time()

# center of the high sampled image.
cen = dim/2
ds = 9

# calculate offsets.
x = 0
offset = (x-cen+ds/2+dim)/ds

# calculate the downsample dimension.
x = dim-1
dsdim = 1 + (x-cen+ds/2+dim)/ds - offset

# generate a blank downsampled image.
lowRes = np.zeros((dsdim, dsdim))

for y in range(0, dim):
    yy = (y-cen+ds/2+dim)/ds - offset
    for x in range(0, dim):
        xx = (x-cen+ds/2+dim)/ds - offset
        lowRes[yy, xx] += highRes[y, x]

t1 = time.time()
total = t1-t0
print 'time taken %f seconds' % total 

我在我的机器上安装了BLAS和LAPACK,我知道通过利用这一点可以获得显着的收益,但是我有点坚持如何继续。到目前为止,这是我的进步。

import numpy as np
import matplotlib.pyplot as plt
import time

print 'rendering circle at high res'

# image dimensions.
dim = 4608

# generate high sampled image.
xx, yy = np.mgrid[:dim, :dim]
highRes = (xx - dim/2) ** 2 + (yy - dim/2) ** 2
print 'render done'

print 'downsampling'
t0 = time.time()

# center of the high sampled image.
cen = dim/2
ds = 9

# calculate offsets.
x = 0
offset = (x-cen+ds/2+dim)/ds

# calculate the downsample dimension.
x = dim-1
dsdim = 1 + (x-cen+ds/2+dim)/ds - offset

# generate a blank downsampled image.
lowRes = np.zeros((dsdim, dsdim))

ar = np.arange(0, dim)
x, y = np.meshgrid(ar, ar)

# calculating symettrically centriod positions.
yy = (y-cen+ds/2+dim)/ds - offset
xx = (x-cen+ds/2+dim)/ds - offset

# to-do : code to map xx, yy into lowRes

t1 = time.time()
total = t1-t0

print 'time taken %f seconds' % total

这个当前版本在我的机器上快了大约16倍,但还不完整。我不确定如何从高分辨率映射新的下采样像素。图像有效。

可能有另一种方法可以加快速度吗?不确定......谢谢!

0 个答案:

没有答案