你可以使用np.unique https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.unique.html以其新的axis
进行分组的参数功能 -
np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]
样本运行 -
In [56]: im
Out[56]:
array([[[255, 255, 255],
[255, 0, 0]],
[[255, 0, 255],
[255, 255, 255]]])
In [57]: np.c_[np.unique(im.reshape(-1,3), axis=0, return_counts=1)]
Out[57]:
array([[255, 0, 0, 1],
[255, 0, 255, 1],
[255, 255, 255, 2]])