从逐块处理函数中调用过滤器函数

发布于 2024-10-17 02:52:36 字数 1174 浏览 1 评论 0原文

我有一些内存密集型图像过滤器,我想在大型图像/数组上逐块调用它们(因为它们计算整个数组的过滤器,当尝试计算整个数组时它们会耗尽内存)。

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 参数(输入数组,由块函数给出),以及其他参数,例如过滤器大小。有些过滤器比其他过滤器具有更多参数。他们返回过滤后的数组。

两个问题

  1. 我如何通过 block_process 函数调用我的过滤器函数之一?我不想将块处理代码复制到每个函数中。换句话说,是否有一种方法可以指定要调用哪个过滤器(以及使用哪些参数)作为 block_process() 调用的参数?
  2. 有更好的编码方法吗?

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

  1. 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?
  2. Is there a better way of coding this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-10-24 02:52:36

您可以这样做:

def block_process(a, blocksize, filt, args):
    b = numpy.empty(a.shape)
    for row in xrange(0, a.shape[0], blocksize):
        for col in xrange(0, a.shape[1], blocksize):
            b[row:row + blocksize, col:col + blocksize] = (
                filt(a[row:row + blocksize, col:col + blocksize], *args))
    return b

无需纠正图像右边缘和下边缘的不完整块 - 这会自动发生。您可以简单地传入过滤函数和参数元组。要调用过滤器 filter1(a, filtsize),请使用

block_process(a, blocksize, filter1, (filtsize,))

上面的代码假设过滤器的第一个参数是要过滤的数组,并且过滤器返回相同形状的过滤数组。

您的过滤器也有可能以不使用太多内存的方式重写,因此块处理将变得不必要。

You can do it like this:

def block_process(a, blocksize, filt, args):
    b = numpy.empty(a.shape)
    for row in xrange(0, a.shape[0], blocksize):
        for col in xrange(0, a.shape[1], blocksize):
            b[row:row + blocksize, col:col + blocksize] = (
                filt(a[row:row + blocksize, col:col + blocksize], *args))
    return b

There is no need to correct for incomplete blocks at the right and lower edge of the image -- this will happen automatically. You can simply pass in the filter function and the tuple of arguments. To call the filter filter1(a, filtsize), use

block_process(a, blocksize, filter1, (filtsize,))

The code above assumes that the first parameter to a filter is the array to be filtered and that a filter returns a filtered array of the same shape.

It is also possible that your filters can be rewritten in a way that they don't use as much memory, so the blockprocessing would become unnecessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文