I have some memory intensive image filters that I want to call block by block on large images/arrays (because they compute the filter for an entire array, they run out of memory when trying to compute the whole array).
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
My filters are computed in other functions i.e. def filter1(A, filtsize)
, def filter2(A, filtsize, otherparam)
, which have an A
parameter (the input array, given by the block function), and other parameters such as filter size. Some filters have more parameters than others. They return the filtered array.
Two questions
- How do I go about calling one of my filter functions through the block_process function? I don't want to copy the block processing code into each function. In other words, is there a way of specifying which filter to call (and with what parameters) as a parameter of the
block_process()
call? - Is there a better way of coding this?