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

标签 python numpy opencv python-imaging-library

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

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

谢谢!

最佳答案

只需部分展平并使用 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)

或者作为一行:

colors, counts = np.unique(image.reshape(-1, 3), 
                           return_counts = True, 
                           axis = 0)

关于python - 在 Python 中计算具有特定值的图像的每个像素的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644348/

相关文章:

c++ - 为 Python 扩展设置 C/C++ 编译器

python - scipy curve_fit 无法正常工作

python - Python如何转换Excel中的日期值

android - 如何在 Android 应用程序中使用 OpenCV 以纵向模式打开相机

python - 在 Python 中创建子类时将子类添加到父类(super class)列表?

python - Python 类属性上的 While 循环

python - 在给定条件的情况下查找列表的最小和最大索引

c++ - 使用 openCV 从 xml 文件读取和显示矩阵

python - OpenCV:selectROI返回零元组

python - pandas:groupby 两列 nunique