开发者

padding arrays using numpy

in my program i have a numpy array and do some convolution filtering on it. i am looking for some way to make array padding (and then unpad for output) easily using numpy to avoid boundary checking. i know that scipy can do convolution, but i have reasons to make it by myself. gnuplot.py is used for output.

def touch(field, coords, value):
    field[coord开发者_JS百科s[0], coords[1]] = value
    if coords[0] - 1 > 0:
        field[coords[0] - 1, coords[1]] = value / 2
    if coords[1] - 1 > 0:
        field[coords[0], coords[1] - 1] = value / 2
    if coords[0] < field.shape[0] - 1:
        field[coords[0] + 1, coords[1]] = value / 2
    if coords[1] < field.shape[1] - 1:
        field[coords[0], coords[1] + 1] = value / 2


There's a pad module scheduled for inclusion in Numpy 1.7.0 – see this ticket. For now, just download it and use its with_constant function.

Unpadding is as simple as field[1:-1, 1:-1].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜