我有一个由 7 种不同可能颜色组成的 RGB 图像。 我想以一种有效的方式计算图像中每种像素类型的数量。所以如果可能的话,不要在每个像素上循环,至少不要手动(numpy 操作没问题,因为它更快)
我尝试将它加载到一个 numpy 数组中,它给了我一个 N*M*3 数组,但我想不出一种方法来计算特定值的像素... 有什么想法吗?
谢谢!
最佳答案
只需部分展平并使用 np.unique
和 return_counts = True
和 axis = 0
flat_image = image.reshape(-1, 3) # makes one long line of pixels
colors, counts = np.unique(flat_image, return_counts = True, axis = 0)
或者作为一行:
colors, counts = np.unique(image.reshape(-1, 3),
return_counts = True,
axis = 0)
关于python - 在 Python 中计算具有特定值的图像的每个像素的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644348/