【问题标题】:Selecting image region based on boolean image mask values根据布尔图像掩码值选择图像区域
【发布时间】:2018-01-15 08:47:32
【问题描述】:

我有一个 rgb 图像,例如

img_rgb[:,:,0] = [ 125 160; 130 125];
img_rgb[:,:,1] = [ 125 160; 130 125];
img_rgb[:,:,2] = [ 125 160; 130 125];

和一个掩码布尔图像,其大小等于 img_rgb 的大小,例如

mask[:,:] = [ 1 0; 0 1]

对于mask的每一个零值,我想在img-rgb中关联一个nan值,从而得到如下

img_rgb[:,:,0] = [ 125 nan; nan 125]
img_rgb[:,:,1] = [ 125 nan; nan 125]
img_rgb[:,:,2] = [ 125 nan; nan 125]

由于我的图像数组非常大(长度大小为 10000 像素),我希望尽可能快地做到这一点,从而避免双倍循环。在 Matlab 中我会使用逻辑运算符

img_rgb(repmat(mask,1,1,3)==0)=nan;

如何在 python 中做类似的事情?蟒蛇v.2.7 提前致谢

【问题讨论】:

    标签: python image image-processing


    【解决方案1】:

    当你使用numpy数组时,你可以使用类似于python中Matlab的布尔索引。

    Broadcasting 将为您处理 repmat。所以你可以这样做:

    import numpy as np
    
    img_rgb[mask == 0] = np.Nan
    

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 2013-01-10
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多