简体   繁体   English

多个数组中的numpy计数频率

[英]numpy count frequency in more than one array

I have 3300 128x128 array(images), I would like to count the unique colours in these image. 我有3300个128x128数组(图像),我想计算这些图像中的唯一颜色。

np.unique(task, return_counts = True)

could give me the unique colour and the counts for one single image. 可以给我提供独特的颜色和一张图像的计数。

But in order to get the representation of all these 3300 images. 但是为了获得所有这3300张图像的表示。 I would like to get a unique result of all these images at the counts as well. 我也想获得所有这些图像的独特结果。

It's easy to get the unique colour, but the counts is difficult to get. 获得独特的颜色很容易,但是计数却很难获得。

Someone could help? 有人可以帮忙吗?

Your question isn't totally clear, but I think you are asking about the best way to get a count of the occurrences of unique colors across all the images, essentially making a histogram of all the colors. 您的问题尚不完全清楚,但我认为您正在询问最佳方法,以对所有图像中出现的唯一颜色进行计数,实质上是对所有颜色进行直方图绘制。 In this case the histogram would have 3000 x 128 x 128 data points, correct? 在这种情况下,直方图将具有3000 x 128 x 128个数据点,对吗?

If so, the easiest implementation is to use a dictionary (or defaultdict) as the data structure, with the colors as the keys and the count of each color as the value. 如果是这样,最简单的实现是使用字典(或defaultdict)作为数据结构,颜色作为键,每种颜色的计数作为值。 Or you could use the Counter module. 或者,您可以使用Counter模块。

You should be able to find examples such as: 您应该能够找到以下示例:

How can I count the occurrences of a list item? 如何计算列表项的出现?

You can pass a list of arrays to np.unique : 您可以将数组列表传递给np.unique

In [11]: a = np.array([[1, 2], [2, 3]])

In [12]: b = np.array([[5, 6], [1, 3]])

In [13] np.unique([a, b], return_counts=True)
Out[13]: (array([1, 2, 3, 5, 6]), array([2, 2, 2, 1, 1]))

Note: The output of np.unique is sorted. 注意:np.unique的输出已排序。

    colors = torch.tensor([])
for i in range(len(Image_list)):

    img_dir = My_directory.format(sub_idx)

    img_str = parc1a_list[i]

    img_arr = io.imread(os.path.join(parc1a_dir, parc1a_str))

    img_tensor = torch.from_numpy(img_arr)

    unique_color = torch.unique(img_tensor).type(torch.FloatTensor)
    colors = torch.cat((colors,unique_color))
colors = torch.unique(colors)
print(colors)
sorted_color, indices = torch.sort(colors)

Sorry My question wasn't clear. 对不起,我的问题不清楚。 I want to count 3000 images unique color. 我想计算3000张图像的独特颜色。 I create a for loop looping through all the 128x128 images and count the number. 我创建了一个for循环遍历所有128x128图像并计算数量。 Thanks for all your help. 感谢你的帮助。

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

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