numpy蒙版创建广播

时间:2019-01-04 15:03:40

标签: python numpy image-processing

我的图像大小为n(row,col),浮点数为n(1,1)。

我要做的是创建0和1(行,列)大小的掩码。中间为1,边缘为0。 1的大小取决于重量。

示例

>>> immask = np.zeros((2,8,8))
[[[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]]

>>> multiplier = np.array([16./64,32./64])
[0.25 0.5 ]

#**insert magic here**
# expected result : 
[[[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 1. 0. 0. 0.]
  [0. 0. 0. 1. 1. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]]

有什么办法可以做到广播吗?不使用循环。预先感谢。

3 个答案:

答案 0 :(得分:0)

我想如果您想使用某种矩阵应用到图片上,请执行以下操作:

from scipy.ndimage.filters import convolve

可以为您工作。您可以更深入地了解here的工作原理。

答案 1 :(得分:0)

尝试一下:

In [1]: import numpy as np

In [3]: immask = np.zeros((2,8,8))

In [6]: immask[0, 3:5, 3:5] = 1                                                                          

In [7]: immask[1, 2:6, 2:6] = 1                                                                          

In [8]: immask                                                                                           
Out[8]: 
array([[[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 1., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 1., 1., 1., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]]])

答案 2 :(得分:0)

此函数执行以下操作:

import numpy as np

def make_masks(weights, rows, cols=None):
    if cols is None:
        cols = rows
    # Add two dimensions to weights
    w = np.asarray(weights)[:, np.newaxis, np.newaxis]
    # Open grid of row and column coordinates
    r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
    # Make masks where criteria is met
    return (np.abs(r) <= w) & (np.abs(c) <= w)

# Test
masks = make_masks([0.25, 0.5], 8)
# Convert type as needed
masks_f = masks.astype(np.float32)
print(masks_f)

输出:

[[[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 1. 0. 0. 0.]
  [0. 0. 0. 1. 1. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 1. 1. 1. 1. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]]

编辑

要制作圆形口罩,功能可能如下:

import numpy as np

def make_circle_masks(weights, rows, cols=None):
    if cols is None:
        cols = rows
    # Add two dimensions to weights
    w = np.asarray(weights)[:, np.newaxis, np.newaxis]
    # Open grid of row and column coordinates
    r, c = np.ogrid[-1:1:rows * 1j, -1:1:cols * 1j]
    # Arrays with distances to centre
    d = r * r + c * c
    # Make masks where criteria is met
    return d <= w * w