我试图将使用imread()
读入的图像的背景标准化,从而生成NxMx3 ndarray。
为此,我需要从255减去NxMx0轴,从250减去NxMx1轴,从225减去NxMx2轴。
我该怎么做?
答案 0 :(得分:1)
您只需从R,G和B值的数组中减去图像,numpy
将implicitly broadcast them。这是有效的,因为image
的最后一个维度与数组的维度相匹配。
import numpy as np
# Some test data
image = np.random.randint(0, 255, (3,3,3))
# array([[[218, 51, 105],
# [227, 34, 20],
# [ 68, 65, 41]],
#
# [[ 43, 171, 110],
# [166, 253, 191],
# [ 86, 26, 28]],
#
# [[167, 117, 167],
# [ 96, 82, 21],
# [ 45, 232, 127]]])
result = [255, 250, 225] - image
# array([[[ 37, 199, 120],
# [ 28, 216, 205],
# [187, 185, 184]],
#
# [[212, 79, 115],
# [ 89, -3, 34],
# [169, 224, 197]],
#
# [[ 88, 133, 58],
# [159, 168, 204],
# [210, 18, 98]]])