Numpy 基于放置在像素上的小遮罩指定值 问题

Numpy 基于放置在像素上的小遮罩指定值 问题,numpy,Numpy,我有一个任意半径的圆形布尔掩码(始终完全对称): 然后我有一个大的uint8矩阵,图像,在这个矩阵中有一对,可以是它的任何有效标记,点 我想做的是在图像中的这一点上应用这个遮罩,这样我就可以在图像中的这一点上放置一个圆 这在图像的中间是非常简单的。您只需执行以下操作: image[point[0] - radius:point[0] + radius + 1, point[1] - radius:point[1] + radius + 1] = circle_mask 但这自然不会处理边界检查

我有一个任意半径的圆形布尔掩码(始终完全对称):

然后我有一个大的uint8矩阵,
图像
,在这个矩阵中有一对,可以是它的任何有效标记,

我想做的是在图像中的这一点上应用这个遮罩,这样我就可以在
图像中的这一点上放置一个圆

这在图像的中间是非常简单的。您只需执行以下操作:

image[point[0] - radius:point[0] + radius + 1, point[1] - radius:point[1] + radius + 1] = circle_mask
但这自然不会处理边界检查,在本例中,边界检查似乎相当复杂,因为我必须确保我们分配给的
图像
的范围与分配给的掩码的大小相同

例子 如果
(1,1)
且圆遮罩的半径为2,则假设
图像
最初为全零,则最终为:

array([[ 1.,  1.,  1.,  0.,  0.,  ... ,  0.],
       [ 1.,  1.,  1.,  1.,  0.,  ... ,  0.],
       [ 1.,  1.,  1.,  0.,  0.,  ... ,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  ... ,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  ... ,  0.],
       ...,
       [ 0.,  0.,  0.,  0.,  0.,  ... ,  0.]])
我的解决方案 我已经想出了以下代码来实现我想要做的事情:

# Initialization of stuff so this is runnable
point = (1, 1)
radius = 2
image = np.zeros((10, 10))
x, y = np.ogrid[-radius : radius + 1, -radius : radius + 1]
circle_mask = x**2 + y**2 <= radius**2

# My solution to the problem
image_min_row = max(point[0] - radius, 0)
image_min_col = max(point[1] - radius, 0)
image_max_row = min(point[0] + radius + 1, image.shape[0])
image_max_col = min(point[1] + radius + 1, image.shape[1])

mask_min_row = max(radius - point[0], 0)
mask_min_col = max(radius - point[1], 0)
mask_max_row = min(image.shape[0] - point[0] + radius, circle_mask.shape[0])
mask_max_col = min(image.shape[1] - point[1] + radius, circle_mask.shape[1])

temp_mask = circle_mask[mask_min_row:mask_max_row, mask_min_col:mask_max_col]
image[image_min_row:image_max_row, image_min_col:image_max_col][temp_mask] = 1
#初始化东西,使其可运行
点=(1,1)
半径=2
图像=np.零((10,10))
x、 y=np.ogrid[-半径:半径+1,-半径:半径+1]

circle_mask=x**2+y**2可以根据图像的索引直接创建一个掩码,消除绑定检查:

x = np.arange(image.shape[0]) 
y = np.arange(image.shape[1])
image[np.add.outer((x-point[0])**2, (y-point[1])**2) <= radius**2] = 1
x=np.arange(image.shape[0])
y=np.arange(image.shape[1])
image[np.add.outer((x点[0])**2,(y点[1])**2)这是一种处理多个点的方法-

from scipy.ndimage.morphology import binary_dilation as dilate

mask_mapped = np.zeros(image.shape,dtype=bool)
mask_mapped[points[...,0],points[...,1]] = 1
image[dilate(mask_mapped,circle_mask)] = 1 
In [96]: points = np.array([[1,1],[7,3],[8,9]])

In [97]: image = np.zeros((10, 10))

In [98]: mask_mapped = np.zeros(image.shape,dtype=bool)
    ...: mask_mapped[points[...,0],points[...,1]] = 1
    ...: image[dilate(mask_mapped,circle_mask)] = 1
    ...: 

In [99]: print image
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  0.  0.  0.  0.  1.]
 [ 0.  1.  1.  1.  1.  1.  0.  0.  1.  1.]
 [ 0.  0.  1.  1.  1.  0.  0.  1.  1.  1.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  1.  1.]]
样本运行

案例1:一点-

In [88]: points = np.array([1,1])

In [89]: image = np.zeros((10, 10))

In [90]: mask_mapped = np.zeros(image.shape,dtype=bool)
    ...: mask_mapped[points[...,0],points[...,1]] = 1
    ...: image[dilate(mask_mapped,circle_mask)] = 1
    ...: 

In [91]: print image
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  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.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
案例2:多个点-

from scipy.ndimage.morphology import binary_dilation as dilate

mask_mapped = np.zeros(image.shape,dtype=bool)
mask_mapped[points[...,0],points[...,1]] = 1
image[dilate(mask_mapped,circle_mask)] = 1 
In [96]: points = np.array([[1,1],[7,3],[8,9]])

In [97]: image = np.zeros((10, 10))

In [98]: mask_mapped = np.zeros(image.shape,dtype=bool)
    ...: mask_mapped[points[...,0],points[...,1]] = 1
    ...: image[dilate(mask_mapped,circle_mask)] = 1
    ...: 

In [99]: print image
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  0.  0.  0.  0.  1.]
 [ 0.  1.  1.  1.  1.  1.  0.  0.  1.  1.]
 [ 0.  0.  1.  1.  1.  0.  0.  1.  1.  1.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  1.  1.]]
如果从一个
zeros
初始化的数组作为图像开始,您可以简单地使用扩展的二进制数组作为图像,如下所示-

In [102]: print dilate(mask_mapped,circle_mask).astype(float)
[[ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  0.  0.  0.  0.  0.  0.]
 [ 1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  1.  1.  1.  0.  0.  0.  0.  1.]
 [ 0.  1.  1.  1.  1.  1.  0.  0.  1.  1.]
 [ 0.  0.  1.  1.  1.  0.  0.  1.  1.  1.]
 [ 0.  0.  0.  1.  0.  0.  0.  0.  1.  1.]]

发布的解决方案中有哪一个适合你吗?