0

I have a stack of RGB images in a 4d numpy array, so the shape is (n_images, height, width, n_channels) where n_channels is 3. How can I get a list of unique RGB values across all the images.

I found this question numpy: unique list of colors in the image for a single image but want to apply it to my stack of images without a for-loop.

BarefootDev
  • 326
  • 3
  • 9

1 Answers1

1

You can use np.unique and set the axis, to look across the images. so basically it will look for unique pixel values.

i am reusing the answer from link you shared for your scenario.

np.unique(img.reshape(-1, img.shape[3]), axis=0)

The above code will result in an array of shape (unique_pixels_len,3)

The original answer had img.shape[2] i.e. the channels and in your case it is img.shape[3] which represents the channels.

venkata krishnan
  • 1,961
  • 1
  • 13
  • 20