NumPy广播允许您使用形状与图像不同的蒙版。例如,
import numpy as npimport matplotlib.pyplot as plt# Construct a random 50x50 RGB image image = np.random.random((50, 50, 3))# Construct mask according to some condition;# in this case, select all pixels with a red value > 0.3mask = image[..., 0] > 0.3# Set all masked pixels to zeromasked = image.copy()masked[mask] = 0# Display original and masked images side-by-sidef, (ax0, ax1) = plt.subplots(1, 2)ax0.imshow(image)ax1.imshow(masked)plt.show()



