我正在尝试使用 NumPy 获取与特定颜色不同的图像像素列表。
例如,在处理以下图像时:
我设法使用以下方法获得所有黑色像素的列表:
np.where(np.all(mask == [0,0,0], axis=-1))
但是当我尝试这样做时:
np.where(np.all(mask != [0,0,0], axis=-1))
我得到了一个非常奇怪的结果:
看起来 NumPy 只返回了 R、G 和 B 非 0 的索引
这是我正在尝试做的一个最小的例子:
import numpy as np
import cv2
# Read mask
mask = cv2.imread("path/to/img")
excluded_color = [0,0,0]
# Try to get indices of pixel with different colors
indices_list = np.where(np.all(mask != excluded_color, axis=-1))
# For some reason, the list doesn't contain all different colors
print("excluded indices are", indices_list)
# Visualization
mask[indices_list] = [255,255,255]
cv2.imshow(mask)
cv2.waitKey(0)
Helenr
人到中年有点甜
相关分类