【发布时间】:2011-06-24 22:12:13
【问题描述】:
我有一些内存密集型图像过滤器,我想在大型图像/数组上逐块调用它们(因为它们会为整个数组计算过滤器,因此在尝试计算整个数组时会耗尽内存)。
def block_process(Ic, blocksize):
B = numpy.empty(Ic.shape)
colstart = 0
while colstart < Ic.shape[1]:
BlockWidth = blocksize
if (colstart + blocksize) > Ic.shape[1]:
BlockWidth = Ic.shape[1] - colstart
rowstart = 0
while rowstart < Ic.shape[0]:
BlockHeight = blocksize
if (rowstart + blocksize) > Ic.shape[0]:
BlockHeight = Ic.shape[0] - rowstart
B[colstart:colstart+BlockWidth, rowstart:rowstart+BlockHeight] = filter1(params) # One of many available filters
rowstart += BlockHeight
colstart += BlockWidth
return B # The complete filtered array
我的过滤器是在其他函数中计算的,即def filter1(A, filtsize)、def filter2(A, filtsize, otherparam),它们有一个A 参数(输入数组,由块函数给出)和其他参数,例如过滤器大小。一些过滤器的参数比其他过滤器多。他们返回过滤后的数组。
两个问题
- 如何通过 block_process 函数调用我的过滤器函数之一?我不想将块处理代码复制到每个函数中。换句话说,有没有一种方法可以指定调用哪个过滤器(以及使用哪些参数)作为
block_process()调用的参数? - 有没有更好的编码方式?
【问题讨论】:
标签: python image-processing workflow numpy