指定中心和半径,用python生成一个圆(球)的Mask数组

三维球体Mask的生成:

def generate_mask(img_height,img_width,img_depth,radius,center_x,center_y,center_z):

    x = np.array(list(range(img_height))).reshape([img_height,1,1])

    y = np.array(list(range(img_width))).reshape([1,img_width,1])

    z = np.array(list(range(img_depth))).reshape([1,1,img_depth])

    # circle mask

    mask = (x-center_x)**2+(y-center_y)**2+(z-center_z)**2<=radius**2  

    return mask

二维圆形Mask的生成:

def generate_mask(img_height,img_width,radius,center_x,center_y):

    y,x=np.ogrid[0:img_height,0:img_width]

    # circle mask

    mask = (x-center_x)**2+(y-center_y)**2<=radius**2

    return mask

 

关于np.ogrid,只适用于二维情况,其会生成一个纵向数组一个横向数组。

print(np.ogrid[0:5,0:6])
'''
 [array([[0],
       [1],
       [2],
       [3],
       [4]]), array([[0, 1, 2, 3, 4, 5]])]
'''

# 上述代码等价于 
# x = np.array(list(range(5))).reshape([5,1])
# y = np.array(list(range(6))).reshape([1,6])
# print([x,y])

另外,如果想计算两个Mask的交集面积,参考代码如下:

S_overlap = len(np.where(mask2[np.where(mask1)])[0])

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值