1

I have 3300 128x128 array(images), I would like to count the unique colours in these image.

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. 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?

xysong
  • 63
  • 2
  • 6

3 Answers3

0

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?

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. Or you could use the Counter module.

You should be able to find examples such as:

How can I count the occurrences of a list item?

AirSquid
  • 10,214
  • 2
  • 7
  • 31
0

You can pass a list of arrays to 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.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
0
    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. I create a for loop looping through all the 128x128 images and count the number. Thanks for all your help.

xysong
  • 63
  • 2
  • 6