看看numpy的numpy.unique()函数:
import numpy as np
test = np.array([[255,233,200], [23,66,122], [0,0,123], [233,200,255], [23,66,122]])
print(np.unique(test, axis=0, return_counts = True))
>>> (array([[ 0, 0, 123],
[ 23, 66, 122],
[233, 200, 255],
[255, 233, 200]]), array([1, 2, 1, 1]))
Run Code Online (Sandbox Code Playgroud)
您可以在2D numpy数组中收集RGB numpy数组,然后使用带axis=0参数的numpy.unique()函数来遍历其中的数组.可选参数return_counts=True还将为您提供出现的次数.