# [Numpy-discussion] Counting the Colors of RGB-Image

Sun Jan 15 11:40:44 CST 2012

```im_flat = im0[...,0]*65536 + im[...,1]*256 +im[...,2]
colours = np.unique(im_flat)

________________________________
From: numpy-discussion-bounces@scipy.org [numpy-discussion-bounces@scipy.org] On Behalf Of Tony Yu [tsyu80@gmail.com]
Sent: 15 January 2012 18:03
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Counting the Colors of RGB-Image

On Sun, Jan 15, 2012 at 10:45 AM, <apo@pdauf.de<mailto:apo@pdauf.de>> wrote:

Counting the Colors of RGB-Image,
nameit im0 with im0.shape = 2500,3500,3
with this code:

tab0 = zeros( (256,256,256) , dtype=int)
tt = im0.view()
tt.shape = -1,3
for r,g,b in tt:
tab0[r,g,b] += 1

Question:

Is there a faster way in numpy to get this result?

MfG elodw

Assuming that your image is made up of integer values (which I guess they'd have to be if you're indexing into `tab0`), then you could write:

>>> rgb_unique = set(tuple(rgb) for rgb in tt)

I'm not sure if it's any faster than your loop, but I would assume it is.

-Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120115/65eefcac/attachment.html
```