简体   繁体   English

使用opencv和python的图像中所有颜色的列表

[英]List of all colors in an image using opencv and python

Python beginner here. Python初学者在这里。 I'm using python 3.6 and opencv and I'm trying to create a list of rgb values of all colors present in an image. 我正在使用python 3.6和opencv,并尝试创建图像中存在的所有颜色的rgb值列表。 I can read the rgb values of one pixel using cv2.imread followed by image[px,px]. 我可以使用cv2.imread和image [px,px]读取一个像素的rgb值。 The result is a numpy array. 结果是一个numpy数组。 What I want is list of rgb tuples of unique colors present in an image and am not sure how to do that. 我想要的是图像中存在的唯一颜色的rgb元组列表,并且不确定如何执行此操作。 Any help is appreciated. 任何帮助表示赞赏。 TIA TIA

Check out numpy's numpy.unique() function: 查看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]))

You can collect the RGB numpy arrays in a 2D numpy array and then use the numpy.unique() funtion with the axis=0 parameter to traverse the arrays within. 您可以在2D numpy数组中收集RGB numpy数组,然后将numpy.unique()函数与axis=0参数一起使用以遍历其中的数组。 Optional parameter return_counts=True will also give you the number of occurences. 可选参数return_counts=True还将为您提供出现次数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM