我基本上有一个语义分割蒙版,我想将图像中除一种颜色(道路 - 紫色)之外的所有颜色更改为黑色。
如何在 numpy 或 opencv 中执行此操作?
import cv2
img = cv2.imread("image.png", cv2.COLOR_RGB2BGR)
colors = [
(100, 100, 150),
(32, 11, 119),
(70, 70, 70),
(100, 60, 0),
(142, 0, 0),
(255, 255, 255)
]
# colors = np.array(colors)
# all other colors
mask = np.zeros(img.shape[:2], dtype=bool)
for color in colors:
mask |= (img == color).all(-1)
img[mask] = (255,255,255)
最佳答案
你可以试试:
# special color
colors = [ (100, 100, 150), (32, 11, 119), (70, 70, 70), (100, 60, 0), (255, 255, 255) ]
# all other colors
mask = np.zeros(img.shape[:2], dtype=bool)
for color in colors:
mask |= (img == color).all(-1)
img[~mask] = (255,255,255)
输出(由 plt.imshow
显示):
关于python - 将图像中的所有其他颜色更改为黑色,除了 Python OpenCV/Numpy 中指定的一种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269937/