在Python中计算特定值图像的每个像素的有效方法?

Adr*_*oli 1 python opencv numpy python-imaging-library

我有一个由7种不同颜色组成的RGB图像.我想以有效的方式计算图像中存在的每种像素类型的数量.因此,如果可能的话,不要在每个像素上进行循环,至少不要手动(numpy操作可以,因为它的速度更快)

我尝试将它加载到一个numpy数组中,它给了我一个N*M*3数组,但我无法想出一种计算特定值的像素的方法......任何想法?

谢谢 !

Dan*_*l F 8

只是部分地变平,并使用np.uniquereturn_counts = Trueaxis = 0

flat_image = image.reshape(-1, 3)  # makes one long line of pixels
colors, counts = np.unique(flat_image, return_counts = True, axis = 0)
Run Code Online (Sandbox Code Playgroud)

或者作为一行:

colors, counts = np.unique(image.reshape(-1, 3), 
                           return_counts = True, 
                           axis = 0)
Run Code Online (Sandbox Code Playgroud)