Python 基于布尔图像掩码值选择图像区域
例如,我有一个rgb图像Python 基于布尔图像掩码值选择图像区域,python,image,image-processing,Python,Image,Image Processing,例如,我有一个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] 对于掩码的每个零值,我想在img rgb中关联一个nan值,从而获得以下结果 img_rgb[:,:,0] = [ 125 nan; nan
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]
对于掩码的每个零值,我想在img rgb中关联一个nan值,从而获得以下结果
img_rgb[:,:,0] = [ 125 nan; nan 125]
img_rgb[:,:,1] = [ 125 nan; nan 125]
img_rgb[:,:,2] = [ 125 nan; nan 125]
由于我的图像阵列非常大(长度大小10000px),所以我希望尽可能快地完成这项工作,从而避免双for循环。在Matlab中,我将使用逻辑运算符
img_rgb(repmat(mask,1,1,3)==0)=nan;
如何在python中执行类似的操作?python v.2.7
提前感谢当您使用数组时,您可以使用类似于python中Matlab的布尔索引
我会帮你保管好这件衣服。因此,您可以只执行以下操作:
import numpy as np
img_rgb[mask == 0] = np.Nan