<html dir="ltr"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="MSHTML 8.00.6001.19170">
<style id="owaTempEditStyle"></style><style title="owaParaStyle"><!--P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
--></style>
</head>
<body ocsi="x">
<div style="FONT-FAMILY: Tahoma; DIRECTION: ltr; COLOR: #000000; FONT-SIZE: x-small">
<div>im_flat = im0[...,0]*65536 + im[...,1]*256 +im[...,2]</div>
<div><font face="tahoma">colours = np.unique(im_flat)</font></div>
<div><font face="tahoma"></font> </div>
<div><font face="tahoma">   Nadav</font></div>
<div dir="ltr"><font color="#000000" size="2" face="Tahoma"></font> </div>
<div style="DIRECTION: ltr" id="divRpF292860">
<hr tabindex="-1">
<font color="#000000" size="2" face="Tahoma"><b>From:</b> numpy-discussion-bounces@scipy.org [numpy-discussion-bounces@scipy.org] On Behalf Of Tony Yu [tsyu80@gmail.com]<br>
<b>Sent:</b> 15 January 2012 18:03<br>
<b>To:</b> Discussion of Numerical Python<br>
<b>Subject:</b> Re: [Numpy-discussion] Counting the Colors of RGB-Image<br>
</font><br>
</div>
<div></div>
<div><br>
<br>
<div class="gmail_quote">On Sun, Jan 15, 2012 at 10:45 AM, <span dir="ltr"><<a href="mailto:apo@pdauf.de">apo@pdauf.de</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<br>
Counting the Colors of RGB-Image,<br>
nameit im0 with im0.shape = 2500,3500,3<br>
with this code:<br>
<br>
tab0 = zeros( (256,256,256) , dtype=int)<br>
tt = im0.view()<br>
tt.shape = -1,3<br>
for r,g,b in tt:<br>
 tab0[r,g,b] += 1<br>
<br>
Question:<br>
<br>
Is there a faster way in numpy to get this result?<br>
<br>
<br>
MfG elodw<br>
</blockquote>
<div><br>
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:<br>
<br>
>>> rgb_unique = set(tuple(rgb) for rgb in tt)<br>
<br>
I'm not sure if it's any faster than your loop, but I would assume it is.<br>
<br>
-Tony<br>
</div>
</div>
</div>
</div>
</body>
</html>