mask是掩码,
A[mask]=v,
通过mask,令A的部分值为v。
A和mask的形状一样,mask只有0或1,或True or False,
mask和A的位置一一对应,当mask的某个位置为1或True时,A的对应位置设为v。
import numpy as np
item = 0
color_mask = np.array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 255, 255, 255],
[0, 0, 255, 255, 255]])
encode_mask = np.zeros((color_mask.shape[0], color_mask.shape[1]))
print("type(item):{}".format(type(item)))
print("color_mask:{}".format(type(color_mask)))
print("color_mask==item:{}".format(color_mask==item))
print("encode_mask:{}".format(encode_mask))
encode_mask[color_mask==item] = 1
print("encode_mask:{}".format(encode_mask))
输出:
type(item):<class 'int'>
color_mask:<class 'numpy.ndarray'>
color_mask==item:[[ True True True True True]
[ True True True True True]
[ True True True True True]
[ True True False False False]
[ True True False False False]]
encode_mask:[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
encode_mask:[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 0. 0. 0.]
[1. 1. 0. 0. 0.]]
img被fgr2有值的值换掉
img[fgr2_mask] = fgr2[fgr2_mask]
fgr2_mask是fgr2 有值的mask
import numpy as np
img = np.zeros((2,2))
fgr2 = np.array([[2,3],[0,0]])
mask_fgr2 = np.zeros((2,2))
mask_fgr2[fgr2>0]=1
mask_fgr2 = mask_fgr2.astype(np.uint8)
print(mask_fgr2)
print(fgr2)
print(img)
img[mask_fgr2] = fgr2[mask_fgr2]
print(img)
output:
[[1 1]
[0 0]]
[[2 3]
[0 0]]
[[0. 0.]
[0. 0.]]
[[2. 3.]
[0. 0.]]