python - 根据 bool 图像掩模值选择图像区域

标签 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 大小的掩码 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/

相关文章:

Java - 图形到图像

c# - 如何叠加两个图像(作为字节数组)

Python OpenCV 颜色跟踪

ios - 图像中的 UIBezierPath

python - PIL 图片导入错误

python - 聚合多组列

python - 极地 : switching between dtypes within a DataFrame

python 帮助 >> 模块给出段错误 - 如何修复?

java - 更改 JFrame 中的图像?

python - Richardson–Lucy 算法是如何工作的?代码示例?