gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:50 24 4
gpt4 key购买 nike

例如,我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com