例如,我有一个 RGB 图像
img_rgb[:,:,0] = [ 125 160; 130 125];
img_rgb[:,:,1] = [ 125 160; 130 125];
img_rgb[:,:,2] = [ 125 160; 130 125];
以及一个大小等于 img_rgb 大小的掩码 bool 图像,例如
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]
由于我的图像数组非常大(长度为 10000px),我想尽可能快地做到这一点,从而避免双循环。在 Matlab 中我会使用逻辑运算符
img_rgb(repmat(mask,1,1,3)==0)=nan;
我怎样才能在Python中做类似的事情? python v.2.7 提前致谢
最佳答案
当您使用numpy时数组,您可以使用类似于Python中Matlab的 bool 索引。
Broadcasting我们将为您处理重装事宜。所以你可以这样做:
import numpy as np
img_rgb[mask == 0] = np.Nan
关于python - 根据 bool 图像掩模值选择图像区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259497/